About Monkey 2 › Forums › Monkey 2 Development › Operator From()
This topic contains 7 replies, has 2 voices, and was last updated by
Mark Sibly
1 year, 5 months ago.
-
AuthorPosts
-
November 15, 2017 at 10:37 am #11709
Is it a good idea to have From() operator (along with To())?
I want to union similar conversions To/From in one place, it can be possible with From operator.
Maybe I don’t know some limitation of that?Also I want to use From as co-operator for assignment operator.
For example:
Monkey1234567891011121314151617Class Wrap<T>Field val:TMethod New( val:T )Self.val=valEndOperator From:Wrap<T>( val:T )Self.val=valReturn SelfEndOperator To:T()Return valEndEnd' usageLocal i1:=New Wrap<Int>( 777 )i1=i1+222Print i1 ' expecting 999 hereIn this example we have boxed int which behave like plain int type – and without creating new instance when assigning a value.
PS. assignment operator will use From and just reassign variable to self if we don’t create instance inside of From.
November 16, 2017 at 2:56 am #11734I don’t like having 1001 ways to convert values as it can easily get too confusing how things work when the compiler does a ton of implicit/magic stuff behind your back. C++ is a bit like this – there’re copy ctors, assignments and cast operators (at least, probably more since c++11) all of which can result in type conversions but it’s not always obvious which one gets called or even when.
So the monkey2 equivalent might be: if ‘operator from’ WAS supported, and a dst type implemented ‘operator from’ and a src type implemented ‘operator to’, which would get used when converting from src type to dst type?
IMO, operator to plus type extensions allows you to do pretty much anything without everything getting too confusing. If there’s something you can’t do this way, let me know and we can try to come up with a solution.
For example, it allows me use std.geom types like Vec3f with assimp and bullet and have them automatically convert correctly from the native assimp/bullet geom types like aiVector3D.
And there is no overloadable assignment operator in monkey2 (or copy ctor).
November 16, 2017 at 7:41 am #11736The truth is, I don’t want to have such magic in monkey2.
I agree – operator To + extensions = powerful coding.
So the monkey2 equivalent might be: if ‘operator from’ WAS supported, and a dst type implemented ‘operator from’ and a src type implemented ‘operator to’, which would get used when converting from src type to dst type?
Left-sided vars have a bigger priority, I think…
If there’s something you can’t do this way, let me know and we can try to come up with a solution.
I’m triyng to do observable variables – sort of plain values wrapper which would notify listeners about value changing.
And I want to use this wrapper as a plain type, but I can’t catch value assignment without overriding of assign operator.
Variant type have a magic with value assigning – we can assign anything.
I want to do something like that but with strong types checking via generics.
My example:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950Namespace myappFunction Main()Local i1:=New Changable<Int>( 10 )i1.Changed+=Lambda()Print "changed: "+i1Endi1=7 ' can't assign'i1=i1+15EndClass Changable<T>Field Changed:Void()Method New()EndMethod New( value:T )_value=valueEndOperator =( value:T ) 'trouble is here_value=valueChanged()EndOperator To:T()Return _valueEndOperator To:String()Return ""+_valueEndProperty Value:T()Return _valueEndPrivateField _value:TEndNovember 16, 2017 at 6:43 pm #11750No, I will not supporting assignment overloading in the near future, if ever.
There are a lot of complications involved with assignment overloading which I’m not going to go into right now (email me if you want to discuss further…) but that I don’t want to weigh the language down with.
November 16, 2017 at 11:36 pm #11755No, I will not supporting assignment overloading in the near future, if ever.
I agree, and I never used it in c++.
Any ideas how to get what I want?
My idea is to have special base class with Assigned event. So we can’t break anything by overloading but can react on assignment:
Monkey1234567Class Assignable<T>ProtectedField Assigned:Void( value:T ).....EndMaybe this class will extends Variant but with generic type.
And usage:
Monkey12345678910111213Class Changable<T> Extends Assignable<T>Field Changed:Void()Method New()Assigned=Lambda( value:T )_value=valueChanged()EndEnd.........EndNovember 17, 2017 at 12:17 am #11757Any ideas how to get what I want?
I’m not actually sure what you want.
If you want a function to be called when a value is modified, you’ll need a ‘Set’ style method for assignments because, well, there is no assignment overloading. This is pretty trivial to implement though so I suspect you mean something else (or that you really *do* want assignment overloading!).
November 17, 2017 at 1:47 am #11759If you want a function to be called when a value is modified, you’ll need a ‘Set’ style method for assignments
Yes, I want this! But don’t want to use Set or properties.:)
Saying about Class Assignable<T> above I meant to have it as a core type of monkey2 like a Variant is.
Variant have an assignment magic on a c++ side. I need right the same plus event Assigned to be able to react on value changes.
I know that this is very specific thing, but maybe it sounds useful not for me only.
November 17, 2017 at 7:01 am #11764>Yes, I want this! But don’t want to use Set or properties.:)
So you do want assignment overloading…as I’ve said above, this is not happening sorry!
-
AuthorPosts
You must be logged in to reply to this topic.