About Monkey 2 › Forums › Monkey 2 Development › Reflection questions
This topic contains 12 replies, has 3 voices, and was last updated by
nerobot 2 years, 4 months ago.
-
AuthorPosts
-
December 10, 2016 at 7:38 pm #5694
Don’t know is it bug or impossible to implement..
I need to get name of template type, to do it I wrote this code:
Monkey1234Method RemoveComponent<T>:Void() Where T Extends ComponentLocal name:=Typeof( T ).Name 'errorRemoveComponent( name ) 'internalEndand I got “Error : Identifier ‘T’ not found”.
Theory. When we call this method then ‘T’ *is* one of concrete type, so Typeof() *can* get it type. But error is at semant time not at compile.
For my luck, I find a solution – by adding optional parameter that we don’t need to use – it works with null-value.
Monkey1234Method RemoveComponent<T>:Void( c:T=Null ) Where T Extends ComponentLocal name:=Typeof( c ).Name 'worksRemoveComponent( name )EndDecember 10, 2016 at 7:56 pm #5695Also I intrested in further improvement of reflection (generics, instance creation).
Is it possible to add into reflection module function like NewInstance( stringNameOfType:String )?
and use like this
Monkey12Local rect:=NewInstance( "Recti" )rect.x=50 ; rect.y = 20 ; .....it looked not easy…
With this stuff we can get object-serializer.
December 10, 2016 at 9:46 pm #5696Use Typeof<> for ‘type expressions’ (eg: Typeof<mojo.Canvas>) and GetDecl( “New” )/Invoke() to create a class instance at runtime.
Check out my reflection blog post and the reflectiontest banana which should cover the basics, including creating a new class instance at runtime.
December 11, 2016 at 6:13 am #5701Really.. thanks!
New question. Mark, please check this code. If we comment import of reflection – code works, else – don’t work.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667Namespace test.stack#Import "<std>"#Import "<mojo>"#Import "<reflection>"Using std..Using mojo..Function Main()Local awake:=New AwakeLocal awk:=Cast<IBehAwake>( awake )Local stack:=New Stack<BehWrapper<IBehAwake>>RemoveFromStack( Varptr stack,awk,BehWrapper<IBehAwake>.Equals )EndInterface IBehAwakeMethod Awake()EndInterface IEnabledProperty isEnabled:Bool()EndClass BehWrapper<T>Field state:IEnabledField source:TMethod New ( source:T,state:IEnabled )Self.source=sourceSelf.state=stateEndFunction Equals:Bool( lhs:BehWrapper<T>,rhs:T )Return lhs.source=rhsEndEndClass Awake Implements IBehAwakeMethod Awake()EndEndFunction RemoveFromStack<T,V>:Bool( stackPtr:Stack<T> Ptr,value:V,equals:Bool( lhs:T,rhs:V ) )Local stack:=stackPtr[0]For Local i:=0 Until stack.LengthIf equals( stack[i],value )stack.Erase( i )Return TrueEndifNextReturn FalseEndDecember 11, 2016 at 11:36 am #5706just a question
[/crayon]Monkey123[crayon-5cba85f219287819248445 inline="true" ]Local awk:=Cast<IBehAwake>( awake )you are casting something to an interface?
December 11, 2016 at 3:49 pm #5709Yes. Why not.
December 11, 2016 at 8:01 pm #5715Can you try and narrow it down?
Have no idea what it’s supposed to do and am feeling too lazy to try harder…
December 11, 2016 at 10:49 pm #5723Ok, definitely seems to be related to the stack ptr param for RemoveFromStack, just passing a plain stack works fine.
This should not of course cause a c++ error and I’ll check it out, but at the same time what’s up with the pointer?
Object Ptr’s (and array and function…well, ALL ptrs!) are dangerous. They can mean there’s nothing’s keeping an object alive so the object can be GC’d unexpectedly. They should only be used in extreme situations – generally dealing with extern libs or something.
I have kind of struggled with whether to or allow ptrs to GC-able objects at all or not but they’re in now so are staying, but be aware that if you use them needlessly in code posted here (without explanation) you will likely receive this lecture every time!
December 12, 2016 at 2:48 am #5737I didn’t explain, sorry.
All I need is remove value from stack. But as stack is struct it passed in function as copy-of-stack, and my remove have no effect for original stack. I tried to use inerator, but I got error and did not understand this. And using of reference (via ptr) seems to be the easiest way for me. But now I tried iterator again and understand what I was doing wrong.
So, new code:
Monkey1234567891011Function RemoveFromStack<T,V>:Bool( iter:Stack<T>.Iterator,value:V,equals:Bool( lhs:T,rhs:V ) )While Not iter.AtEndIf equals( iter.Current,value )iter.Erase()Return TrueEndifiter.Bump()WendReturn FalseEndAnd usage:
Monkey1RemoveFromStack<BehWrapper<IBehAwake>,IBehAwake>( stack.All(),awk,BehWrapper<IBehAwake>.Equals )Yesterday I forgot to add prefix here ^
Monkey1<BehWrapper<IBehAwake>,IBehAwake>and got unresolved types error (“Can’t find overload for …. with argument types ….)
December 12, 2016 at 7:25 am #5742Stack is not a struct!
Stack, List, Map and Deque are all classes so instances are always passed by reference.
Also, I recently added a RemoveIf to stack (and list) which may be useful…
December 12, 2016 at 7:31 am #5743Oh.. really. I keep opened stack.monkey2 file and don’t see that it’s a class, omg..
I found another error in my code, which I began to doubt in the stack work.
Big Thanks, Mark!
December 12, 2016 at 8:29 am #5744I’m quite impressed you’ve done ted2go without knowing that!
December 12, 2016 at 8:41 am #5745I know.. just need to be more attentive.
-
AuthorPosts
You must be logged in to reply to this topic.