Unions, anon Structure, fixed-length Strings
Community Forums/Monkey2 Talk/Unions, anon Structure, fixed-length Strings
| ||
Will MX2 support Unions within Structures? Anonymous Structures within Structures? Fixed-Length Strings (in Structure fields)? typedef struct _something { char name[100]; // fixed-length string long version; union { // union struct { // anonymous structure short a; short b; short c; }; struct { long l1; long l2; }; long long q; }; short x; short y; } SOMETHING; Structure Something name:String{100} // fixed-length string version:Int Union // union Structure // anonymous structure a:Short b:Short c:Short End Structure Structure l1:Int l2:Int End q:Quad EndUnion x:Short y:Short EndStructure |
| ||
I hope we will get something easy like javascript/php:Local test = {} test.a = "bla" test['b'] = "bla" test.c = 1 test.d = aObjectRef foreach(test as key=>val){ if(typeof(test[key]) = string){ print test[key] } } print test.a print test.c+1 method bla(){ local test2 = {} test2.a = 1 test2.b = 2 return test2 } Local test3 = bla() Local test4:Int = bla().a Just typing the code, and not defining everything. |
| ||
Probably not, in a strict type-safe language. But you can use Maps already:Import Map Function bla:StringMap<Int>() Local test2:= New StringMap<Int>() test2.Add("a", 1) test2.Add("b", 2) return test2 End Function Main() Local test:StringMap<Object> = New StringMap<Object>() test.Add("a", BoxString("bla")) test.Add("b", BoxString("bla")) test.Add("c", BoxInt(1)) For Local key:= EachIn test.Keys Print key Next Print UnboxString(test.Get("a")) Print UnboxInt(test.Get("c")) + 1 Local test4:Int = bla().Get("a") Print test4 End SizeOf(), TypeOf(), InstanceOf() seems to be missing, though. Maybe possible using reflection. SizeOf() is also often required to init a structure member with SizeOf(Structure) in external API's, and to check SizeOf(Int) on different platforms (32/64bit). |
| ||
Some kind of operator overloading would be cool, though. Somap.Add("a", 1) x := map.Get("a") could become map["a"] = 1 x := map["a"] Especially operator overloading for [] and () allows many nice shortcuts. Dunno if emscripten supports this... Local test2 := New StringMap<Int>() test2["x"] = 1 ' ERROR: Cannot convert from String to Int. test2("x") = 1 ' ERROR: Identifier 'test2' not found. Already wondered about this errors today, until I realized that we have to use Add/Get explicitely. ;) |
| ||
Operator overloading would be really nice.foo["bar"] = 123 --> foo.Set(123) Print foo["bar"] --> Print foo.Get("bar") foo("bar") --> foo.Invoke("bar") In this topic I made a suggestion for operator overloading and it looks like Mark will implement it in Monkey2. |