About Monkey 2 › Forums › Monkey 2 Development › Maybe something wrong with Generics?
This topic contains 4 replies, has 2 voices, and was last updated by
Jesse
2 years, 9 months ago.
-
AuthorPosts
-
July 13, 2016 at 11:41 pm #2108
the Code below was working with the previous version of Mx2 but now it doesn’t recognize the generic class new. As I am no expert I don’t know if it’s my code or a bug with generics. by the way tha’s Mark’s Deque module:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213Class Deque<T>Field _data:T[]Field _capacity:intField _first:IntField _last:IntMethod New()_data = New T[4]EndMethod New( arr:T[] )_data=arr_capacity=_data.Length_last=_capacityEndMethod Clear:Void()If _first<=_lastFor Local i:=_first Until _last_data[i]=NILNextElseFor Local i:=0 Until _last_data[i]=NILNextFor Local i := _first Until _capacity_data[i]=NILNextEndif_first=0_last=0EndMethod Length:Int()If _last>=_first Return _last-_firstReturn _capacity-_first+_lastEndMethod IsEmpty:Bool()Return _first=_lastEndMethod ToArray:T[]()Local data:T[] = new T[Length()]If _first<=_lastFor Local i:=_first Until _lastdata[i-_first]=_data[i]NextElseLocal n:=_capacity-_firstFor Local i:=0 Until ndata[i]=_data[_first+i]NextFor Local i:=0 Until _lastdata[n+i]=_data[i]NextEndifReturn dataEndMethod ObjectEnumerator:Enumerator<T>()Return New Enumerator<T>( Self )EndMethod Get:T( index:Int )#If CONFIG="debug"If index<0 Or index>=Length Error "Illegal deque index"#EndifReturn _data[(index+_first)Mod _capacity]EndMethod Set:Void( index:Int,value:T )#If CONFIG="debug"If index<0 Or index>=Length Error "Illegal deque index"#Endif_data[(index+_first)Mod _capacity]=valueEndMethod PushFirst:Void( value:T )If Length()+1>=_capacity Grow()_first-=1If _first<0 _first=_capacity-1_data[_first]=valueEndMethod PushLast:Void( value:T )If Length()+1>=_capacity Grow()_data[_last]=value_last+=1If _last=_capacity _last=0EndMethod PopFirst:T()#If CONFIG="debug"If IsEmpty Error "Illegal operation on empty deque"#EndifLocal v:=_data[_first]_data[_first]=NIL_first+=1If _first=_capacity _first=0Return vEndMethod PopLast:T()#If CONFIG="debug"If IsEmpty Error "Illegal operation on empty deque"#EndifIf _last=0 _last=_capacity_last-=1Local v:=_data[_last]_data[_last]=NILReturn vEndMethod First:T()#If CONFIG="debug"If IsEmpty Error "Illegal operation on empty deque"#EndifReturn _data[_first]EndMethod Last:T()#If CONFIG="debug"If IsEmpty Error "Illegal operation on empty deque"#EndifReturn _data[(_last-1)Mod _capacity]EndPrivateGlobal NIL:TMethod Grow:Void()Local data:=New T[_capacity*2+10]If _first<=_lastFor Local i:=_first Until _lastdata[i-_first]=_data[i]Next_last-=_first_first=0ElseLocal n:=_capacity-_firstFor Local i:=0 Until ndata[i]=_data[_first+i]NextFor Local i:=0 Until _lastdata[n+i]=_data[i]Next_last+=n_first=0Endif_capacity=data.Length_data=dataEndEndClass Enumerator<T>Method New( deque:Deque<T> )_deque=dequeEndMethod HasNext:Bool()Return _index<_deque.Length()-1EndMethod NextObject:T()_index+=1Return _deque.Get( _index )EndPrivateField _deque:Deque<T>Field _index:Int=-1EndClass IntDeque Extends Deque<Int>Method New()EndMethod New( data:Int[] )Super.New( data )EndEndClass FloatDeque Extends Deque<Float>Method New()EndMethod New( data:Float[] )Super.New( data )EndEndClass StringDeque Extends Deque<String>Method New()EndMethod New( data:String[] )Super.New( data )EndEndJuly 14, 2016 at 1:22 am #2112What error do you get?
How do I reproduce the error?
Also, there’s a version of Deque coming to std soon..
July 14, 2016 at 3:02 am #2113Sorry Mark it tells me method super.new() in the extended type is undeclared:
/Users/ayoitseve/Desktop/monkeypatrol/src/deque.monkey2 [209] : Error : Can’t find overload for ‘new(…)’ with argument types (string[])
/Users/ayoitseve/Desktop/monkeypatrol/src/deque.monkey2 [198] : Error : Can’t find overload for ‘new(…)’ with argument types (float[])
/Users/ayoitseve/Desktop/monkeypatrol/src/deque.monkey2 [187] : Error : Can’t find overload for ‘new(…)’ with argument types (int[])July 14, 2016 at 3:15 am #2114I am trying to isolate the code to post it but everything seems to work good. I can’t duplicate the error with the code I wrote. It’s working fine. I can’t understand is why the error now and why in those files because I am not using Int, Float or string Deque and it shouldn’t be a reason to point out those errors.
July 14, 2016 at 4:23 am #2115well, I deleted the int, float and string classes and I was able to keep using the module. But I still don’t know why the error. as all I did was just remove those three classes and it worked. no other code edit on any of the other files.
-
AuthorPosts
You must be logged in to reply to this topic.