About Monkey 2 › Forums › Monkey 2 Programming Help › Trying to understand a few things…
This topic contains 2 replies, has 3 voices, and was last updated by
Danilo
3 months, 1 week ago.
-
AuthorPosts
-
January 5, 2019 at 2:53 am #15850Monkey123456789101112#Import "<std>"Using std..Global test:= New Map<String,String>test["test"]="Hello"Function Main()Print test["test"]End
I’m trying to learn the language but I’m not understanding why this won’t build. I’m assuming that we can’t define variables outside of a function? I hope this isn’t the case as it would make things extremely ugly. I’m trying to port some old code and I need to define a map of a few dozen items. Having to do it inside of a function just seems strange to me. Is there something that I’m missing?
January 5, 2019 at 12:36 pm #15852That’s basically right, though simple assignments can be done if you can define them on the same line, eg. Global a:=1.
You can of course assign them in Main, which, if you think about it, is really what defining variables ‘outside of functions’ was in Blitz & co. Monkey/Monkey2 just adopted the more formal approach of having an explicit Main…
Monkey1234567891011121314151617#Import "<std>"Using std..Global a:= 1Global test:= New Map<String,String>Function Main()test["test"]="Hello"test["temp"]="Bye"Print test["test"]Print test["temp"]EndI tried a few variations but I don’t think there’s a way to initiate the string data on the global line in this case. (Could be wrong… )
January 5, 2019 at 7:59 pm #15856It is possible if you use your own class that extends Map.
With simple-type Arrays it is possible to initialize when declaring it:
Monkey123456789101112131415161718192021222324252627282930313233343536#Import "<std>"Using std..Global intArr1D := New Int[] ( 9, 8, 7, 6 )Global intArr2D := New Int[][] (New Int[]( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ),New Int[]( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 0, 8, 8, 0, 0, 0, 0 ),New Int[]( 0, 0, 0, 8, 8, 8, 8, 0, 0, 0 ),New Int[]( 0, 0, 8, 0, 0, 0, 0, 8, 0, 0 ),New Int[]( 0, 8, 0, 0, 0, 0, 0, 0, 8, 0 ),New Int[]( 8, 0, 0, 0, 0, 0, 0, 0, 0, 8 ) )Global strArr1D := New String[] ( "s1", "s2", "s3", "s4" )Global strArr2D := New String[][] (New String[]("a1", "a2"),New String[]("b1", "b2"),New String[]("c1", "c2") )Function Main()Print intArr1D[ 2 ] ' 7Print intArr2D[ 0 ][ 7 ] ' 7Print intArr2D[ 1 ][ 1 ] ' 8Print strArr1D[ 2 ] ' s3Print strArr2D[ 2 ][ 1 ] ' c2EndNow we can use that principle and write a generic class that extends Map<K,V>, let’s call it MapEx<K,V>.
We then add a constructor (“New”) that takes an array of Value+Key pairs for initializing the Map.
That would look like this:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#Import "<std>"Using std..Global test:= New MapEx<String,String>(New MapExPair<String,String> [](New MapExPair<String,String>("test1","Hello 1"),New MapExPair<String,String>("test2","Hello 2") ) )Function Main()Print test["test1"]Print test["test2"]Print test["test3"]Local newMap1 := MapEx<String,Int>.Create()Local newMap2 := MapEx<String,Int>.Create(New MapExPair<String,Int>[](New MapExPair<String,Int>("value 1",12) ) )Print newMap2["value 1"]End'' MapExPair is a pair of Key + Value'Class MapExPair<K,V>Field key:KField value:VMethod New( _key:K, _value:V )key = _keyvalue = _valueEndEnd'' Extended Map class with Initializers'Class MapEx<K,V> Extends Map<K,V>Method New()Super.New()EndMethod New( values:MapExPair<K,V>[] )For Local value := Eachin valuesSelf[value.key] = value.valueNextEndFunction Create:MapEx<K,V>()Return New MapEx<K,V>()EndFunction Create:MapEx<K,V>( values:MapExPair<K,V>[] )Return New MapEx<K,V>( values )EndEndOkay, that works. But that constructs for initializing the Map are very big.
By using two ‘Alias’ we can shorten it:
Monkey12Alias MyMap:MapEx<String,String>Alias MyPair:MapExPair<String,String>With those two Alias, we can now write a simpler global Map initialization:
Monkey12345Global test:= New MyMap( New MyPair[] (New MyPair("test1","Hello 1"),New MyPair("test2","Hello 2"),New MyPair("test3","Hello 3"),New MyPair("test4","Hello 4") ) )Looks better, doesn’t it?
Here the final code:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#Import "<std>"Using std..Alias MyMap:MapEx<String,String>Alias MyPair:MapExPair<String,String>Global test:= New MyMap( New MyPair[] (New MyPair("test1","Hello 1"),New MyPair("test2","Hello 2"),New MyPair("test3","Hello 3"),New MyPair("test4","Hello 4") ) )Function Main()Print test["test1"]Print test["test2"]Print test["test3"]Print test["test4"]Local newMap1 := MyMap.Create()Local newMap2 := MyMap.Create(New MyPair[](New MyPair("value 1","string 1") ) )Print "-----"Print newMap2["value 1"]End'' MapExPair is a pair of Key + Value'Class MapExPair<K,V>Field key:KField value:VMethod New( _key:K, _value:V )key = _keyvalue = _valueEndEnd'' Extended Map class with Initializers'Class MapEx<K,V> Extends Map<K,V>Method New()Super.New()EndMethod New( values:MapExPair<K,V>[] )For Local value := Eachin valuesSelf[value.key] = value.valueNextEndFunction Create:MapEx<K,V>()Return New MapEx<K,V>()EndFunction Create:MapEx<K,V>( values:MapExPair<K,V>[] )Return New MapEx<K,V>( values )EndEnd -
AuthorPosts
You must be logged in to reply to this topic.