About Monkey 2 › Forums › Monkey 2 Programming Help › Class' Where conditions
This topic contains 4 replies, has 2 voices, and was last updated by
nerobot 1 year, 5 months ago.
-
AuthorPosts
-
November 14, 2017 at 11:21 am #11695
What would be the syntax for the new where for class conditions? And what is the purpose of this feature?
Where conditions for classes. Class declaration can now include a ‘where condition’ at the end of the regular class declaration. If the where condition evaluates to false when the class is instantiated, a compile time error occurs.
In this example I get no compilation error. But I supposed ‘Where False’ would induce the compilation error..
Monkey123456789101112131415161718192021222324252627Namespace myapp#Import "<std>"Using std..Class Foo Where FalseField i:=0Method inc()i+=1Print iEndEndFunction Main()Print "Hello World"Local f:=New Foof.inc()EndNovember 16, 2017 at 1:37 pm #11743And I don’t get how a compilation error can occur while the class is instantiated, wouldn’t it be a runtime error?
November 16, 2017 at 3:15 pm #11745What would be the syntax for the new where for class conditions? And what is the purpose of this feature?
Don’t use it just because you can if you don’t really need that.
And I don’t get how a compilation error can occur while the class is instantiated, wouldn’t it be a runtime error?
Not a runtime error but compile time error!
mx2cc checks variables types at compile time.
See my example below. There is an compilation error. Just comment where condition and you can compile it.
Both classes have DoSomething methods => we can use them as generic type.
But if we want to use ComponentHolder with components only – we add Where condition to deny other types.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748Namespace myapp#Import "<std>"Using std..Function Main()Local h1:=New ComponentHolder<Component>( New Component )Local h2:=New ComponentHolder<NotComponent>( New NotComponent )h1.Exec()h2.Exec()EndClass ComponentMethod DoSomething()Print "I'm a component"EndEndClass NotComponentMethod DoSomething()Print "I'm not a component"EndEndClass ComponentHolder<T> Where T Extends ComponentMethod New( comp:T)_component=compEndMethod Exec()_component.DoSomething()EndPrivateField _component:TEndNovember 16, 2017 at 4:20 pm #11746Ok I though this was not new and that the new feature announced in the blog post was not for generics.
I still don’t get when the class has been instantiated if the program could not run. Afaik you have to call New() to get an instance. Or Mark meant instantiated in the compiler logic?
Anyway, nevermind, if it’s a generics thing I know the purpose of it
thanks
November 17, 2017 at 3:08 am #11761Ok I though this was not new and that the new feature announced in the blog post was not for generics.
New part of Where is abilitity to combine conditions by And / Or / Not operators.
You can write Where T Extends Component Or T=MyUpdater. Yes, we also can use = and <> in conditions.
Combined condition is needed also for multi-generic classes, for example:
Monkey123Class Dictionary<K,V> Where K Implements IKey And V Implements IValue.......EndI still don’t get when the class has been instantiated if the program could not run. Afaik you have to call New() to get an instance. Or Mark meant instantiated in the compiler logic?
Here: “instantiated” means “code part where we call New()”. Monkey2’s compiler check all New() calls to check are they correct accordingly with Where. C++ compiler do its own checks but doesn’t allow us to deny using of unwanted types as a generics (allow to deny, sounds good:) ).
-
AuthorPosts
You must be logged in to reply to this topic.