About Monkey 2 › Forums › Monkey 2 Development › Is Variant.ToString() possible?
This topic contains 5 replies, has 3 voices, and was last updated by
Amon
1 year, 1 month ago.
-
AuthorPosts
-
February 28, 2018 at 6:34 am #13756
I want to print a Map<String,Variant> type in such form:
Monkey123key1 => value1key2 => value2......I wrote To:String() operator:
Monkey123456789Operator To:String()Local s:=""For Local key:=Eachin _items.KeysIf s Then s+="~n"s+=key+"=>"+Cast<String>( _items[key] )NextReturn sEndBut we can cast a variant to string if only(!) it contains a string.
Maybe it’s possible to do in c++ side?
February 28, 2018 at 8:18 am #13758I’m doing it this way. If you import reflection there should be a nicer way to do that but I’ve never used reflection so far. So that’s the way I could make it work…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657Namespace myapp#Import "<std>"Using std..Function Main()Local map:=New StrVarMap()map["key_one"]=15map["key_two"]=15.15map["key_three"]="fifteen"map["key_four"]=Truemap["Vec_a"]=New Vec2f(0,0.1)map["Vec_b"]=New Vec2i(0,0)map["Stack_a"]=New IntStack() 'this one will be unkownPrint mapEndClass StrVarMap Extends StringMap<Variant>Operator To:String()'recording types of interrest (without the use of reflecion, there is probably a better way to do that?)Local variantVec2f:Variant=New Vec2f (0.1,0)Local variantVec2i:Variant=New Vec2i (0,0)Local s:=""For Local node:=Eachin SelfIf s Then s+="~n"s+=node.Key+" =>"If node.Value.Type.Name="Int"s+="(Int): "+Cast <Int>(node.Value)Elseif node.Value.Type.Name="Float"s+="(Float): "+Cast <Float>(node.Value)Elseif node.Value.Type.Name="Bool"s+="(Bool): "+Cast <Bool>(node.Value)Elseif node.Value.Type.Name="String"s+="(String): "+Cast <String>(node.Value)Elseif node.Value.Type=variantVec2f.Types+="(Vec2f): "+Cast <Vec2f>(node.Value)Elseif node.Value.Type=variantVec2i.Types+="(Vec2i): "+Cast <Vec2i>(node.Value)Elses+=node.Value.Type.NameEndNextReturn s+"~n"EndEndFebruary 28, 2018 at 8:29 am #13760It’s good when you have a constatnt range of types.
But you should extend this if-else block for any new type.
February 28, 2018 at 8:57 am #13763Yes and it is not extending Variant so it’s not a Variant:To_String operator, which I have no clue how to get working. But it works fine for my use. As I said, using reflection could help here?
But do you really need to print a lot of other types that these ones? It does not not represent a lot of code in a whole app code.February 28, 2018 at 9:06 am #13764I think I can use your realization, for my purposes printing of variant.type.name is enough. Thanks!
February 28, 2018 at 9:23 am #13766I totally got all of that.
-
AuthorPosts
You must be logged in to reply to this topic.