Vala to_string () method (original) (raw)

February 28, 2025, 10:39pm 1

/* bug.vala 

I find the behavior given in the example very strange... 

It is logical to expect a call of to_string () in each of cases.

Isn't it?

*/

/// 
public class Tst : Object {

    /// 
    public string to_string () {
        return "Tst";
    }

} // class


/// 
public class App : Object {
    /// 
    public static int main (string [] args) {

        var t = new Tst ();

        // 1
        stderr.puts (@"1: \"$( t )\"\n"); // OK


        // 2
        //stderr.printf ("%s\n", t); // ERR

        // Compiler error:
        // Argument 2: Cannot convert from `Tst' to `string'
        
        // Why not to call t.to_string () automatically,
        // like it done in (1) ?


        // 3
        stderr.printf ("3: \"%s\"\n", t.to_string ()); // OK

        
        // 4
        stderr.printf ("4: \"%s\"\n\n", (string) t); // (?!)

        /* Output for
        two different versions of valac, a few runs.
        Note to "random" value at line 4.

        Vala 0.48.17
         
        $ /usr/bin/valac-0.48 bug.vala; ./bug; ./bug; ./bug
        1: "Tst"
        3: "Tst"
        4: "+"

        1: "Tst"
        3: "Tst"
        4: "j"

        1: "Tst"
        3: "Tst"
        4: "{k"
        
        
        Vala 0.57.0.307-d7d33-dirty

        $ /usr/bin/bin/valac-0.58 bug.vala; ./bug; ./bug; ./bug
        1: "Tst"
        3: "Tst"
        4: "+"

        1: "Tst"
        3: "Tst"
        4: "+B"

        1: "Tst"
        3: "Tst"
        4: "kR"

        */


        return 0;
    }

}

system (system) Closed March 30, 2025, 10:39pm 2

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.