If you look at the generated code, you will see:

_tmp1_ = g_strconcat ("foobar", (const gchar*) test, NULL);

for line 4, which is not what you want. When you use an explicit cast on
any type other than a GObject, Vala doesn't know how to do a conversion, so
it does whatever conversion C would do. That usually means converting
integers from one type to another, but it can also mean converting an
integer to a pointer, as in this case.

If you do:
    string x = "foobar" + x.to_string();

it will compile to:

test = (guint64) 1;
_tmp1_ = g_strdup_printf ("%" G_GUINT64_FORMAT, test);
_tmp2_ = _tmp1_;
_tmp3_ = g_strconcat ("foobar", _tmp2_, NULL);

Which is probably what you want when using the + operator.

If you use an interpolated string:

     string x = @"foobar$(test)";

You will get the same code as Vala will automatically call `to_string` on
any non-strings that are embedded.

The reason the direct cast behaviour is needed is there are legitimately
non-string things that are strings. For instance, if you have an array of
uint8[], a direct cast is desirable.

    uint8 data[12];
    data[0] = 'a';
    data[1] = '\0';
    string x = "foobar" + (string) data; // Just fine and exactly what I
intended.

I agree it's not what a typical VM language would do, but Vala is trying to
have C compatibility.

On 6 February 2015 at 06:01, Daniel Brendle <grindh...@skarphed.org> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello, fellow Valaists
>
> I have the following example code which segfaults:
>
> public int main(string[] argv){
>     stdout.printf("hello world\n");
>     uint64 test = 1;
>     string x = "foobar"+(string)test;
>     stdout.printf(x+"\n");
>     return  0;
> }
>
> compiled with my valac 0.26.1, the result segfaults in line 4.
> I can't imagine, this is intended behaviour.
>
> Of course, the proper way to go for line 4 would be something like
>
>     string x = "foobar%lld".printf(test);
>
> Nevertheless i wonder, why the code segfaults, shouldn't there rather
> be a type conversion error at compile time?
>
> Sorry, i don't have a more recent 0.27 or 0.26.2 compiler at hand.
>
>
> TIA, Grindhold
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1
>
> iQIcBAEBAgAGBQJU1J8SAAoJEBuqgJs6izSE+JMQAJApxYTnuGmriXvbR5KLOAcv
> ZTzJ1PbUKuGs76EewJYOS5lPX997y4DRBHPTCy7cALwaAhpNcKYAI+jb6QisUbP0
> Fu5JLRFv7xjoTC560r3yuRNPRgeMTd9esE6siGTm1oMB35buxt1XOQ9bz1iZ28n2
> vnFhaZZ3paOFBpAwVyn6lyNGmWnjquAfCc0Km1h/w94PGgFf3wAqSQugiG9dPG9U
> afOiDG1R8JFLpdQEEHCZfSd6ohq9CPjBIgncHKuRxsrfFRkmJ6CuyMdMKvzqvXeq
> PgqIQ2RiAm76lVFMH4XCfsfhupVz8oDNACqaI1AEnKNRizdWBi8kqsuBZBJtlmz6
> mJC2fim8ZIDUJo75/aJnUPTHFKrHFp307kkkp5AW4ihGQtn8Ef4q9Z0aLcAO4sSC
> vGSdnG3hysrlUxLwozoIx1cv47IHJ+A/Nn8tO//riFsrwa821dh2wioWVdq58e7b
> T5wQ+Q+nsb21LoQpd+bw2vDlRhmjJcAWXoXG/6YFPIU1zi2n9jTZ3/75m4btG6P1
> sS3L99w22KldN1JDlCCWkH+6bnrQfwOP0YArQbtP7zVWEZCmKtH3ncXrAdrkjhIp
> hcHAzgtsrH2fggQ9VGPMGrVVz5ALTxOMt2qUKtXSoC4LtlNDVFEXAtxvYIj5REjD
> L2rx0BsJxwRq+OQfRsal
> =H26t
> -----END PGP SIGNATURE-----
> _______________________________________________
> vala-list mailing list
> vala-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/vala-list
>



-- 
--Andre Masella<an...@masella.name>
http://www.masella.name/
_______________________________________________
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to