About Monkey 2 › Forums › Monkey 2 Development › ToString() method by default
This topic contains 8 replies, has 5 voices, and was last updated by
nerobot 2 years, 7 months ago.
-
AuthorPosts
-
August 28, 2016 at 2:59 pm #3527
Hi Mark. Is it possible to add “default” ToString() method like in Java or C#?
Need to add this method to base Object class, so that it will be available in all classes.
Default ToString() will return class type and memory hash like “mojo.app.View@433463″.
And need to compiler call it itself when using “+” operator with strings – to avoid unwanted typing of Cast<String>(myObj).
And I saw that Bool type does not cast to string automatically, like Int.
Monkey1234Local b := TruePrint "b: "+b' Error: Value cannot be implicitly cast from type 'bool' to type 'string'and we need to write Cast<String>(b) – unwanted thing.
August 28, 2016 at 3:14 pm #3528you don’t need to use cast for primitives. you can do this:
[/crayon]Monkey123[crayon-5cb9d0d77f3b0231009781 inline="true" ]Local b := TruePrint "b: "+String(b)August 28, 2016 at 3:35 pm #3529I want to write just
Print “b: “+b
August 28, 2016 at 5:18 pm #3530the only thing that doesn’t work that way is Bool. Integers, Floats and shorts work. I thin Mark needs to fix that.
September 6, 2016 at 9:41 am #3696Bool worked that way in v1.03
I’ve just switched from 1.03 to 1.05 and String(booleanValue) worked in 1.03 but has stopped working in 1.05 (and presumably 1.04)
Cast<String>(booleanValue) isn’t working either
September 6, 2016 at 1:32 pm #3701Oh.. so will use Int(boolVal) until better times.
September 6, 2016 at 8:56 pm #3712The problem with bool->string conversion is that since string->bool is true if string is non-empty (which IMO is the right way to do it), then for Bool(String(Bool)) to produce the correct result (ie: should give same value as the original bool) bool->string needs to produce “” for false. Which means Print Bool( blah ) could print ‘nothing’ which I’m not comfortable with.
Or…if bool->string produced “true”/”false” or something, then Bool(String(Bool)) would always return true!
I have never really come up with a good solution for this (mx1 did it too) but I DO know I much prefer the simple ‘non-empty-string-is-true’ string->bool conversion as it’s easy to remember and IMO more useful in practice, than attempting to parse “1” or “true” or “True” etc as true.
September 7, 2016 at 12:48 am #3720The problem with bool->string conversion is that since string->bool is true if string is non-empty (which IMO is the right way to do it), then for Bool(String(Bool)) to produce the correct result (ie: should give same value as the original bool) bool->string needs to produce “” for false.
we don’t expect the original start value when going from a double to int and back. I think it’s a mistake to compare them to primitive number types. It’s pretty standard for string conversion of bool to produce ‘true’ / ‘false’.
September 7, 2016 at 1:43 am #3723We not expected to convert string->bool like
Local b := “true” (1) or b := Bool(string) (2)
For this case other langs usually have Bool.Parse() func with different params.
However, for monkey case (2) also looks like good.
In mx2 it would be great to override To:String operator for Bool and return ‘true’/’false’.
But for IF’s str->bool conversions we expect use of rule “null or empty-string is false”.
-
AuthorPosts
You must be logged in to reply to this topic.