ToString() method by default

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.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #3527

    nerobot
    Participant

    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.

    and we need to write Cast<String>(b) – unwanted thing.

    #3528

    Jesse
    Participant

    you don’t need to use cast for primitives. you can do this:

    [/crayon]
    #3529

    nerobot
    Participant

    I want to write just

    Print “b: “+b

    #3530

    Jesse
    Participant

    the only thing that doesn’t work that way is Bool. Integers, Floats and shorts work. I thin Mark needs to fix that.

    #3696

    Arjailer
    Participant

    Bool 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  🙁

    #3701

    nerobot
    Participant

    Oh.. so will use Int(boolVal) until better times.

    #3712

    Mark Sibly
    Keymaster

    The 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.

    #3720

    dmaz
    Participant

    The 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’.

    #3723

    nerobot
    Participant

    We 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”.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.