Author Topic: Cast syntax  (Read 4245 times)

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Cast syntax
« on: October 27, 2018, 01:31:13 PM »
Currently the cast syntax is:

cast<new_type>(variable)

As Bas already mentioned, this looks out of place with the rest of the syntax.

I'm therefore going to list a few alternatives (including the current). For these examples I'm casting "foo" to an i32.
  • cast<i32>(foo)
  • @cast<i32>(foo)
  • cast(foo as i32)
  • @cast(foo as i32)
  • foo as i32
  • foo to i32
  • cast(foo, i32)
  • @cast(foo, i32)
  • foo->i32
  • (foo:i32)
  • cast(foo:i32)
  • @cast(foo:i32)
  • (foo::i32)
  • cast(foo::i32)
  • @cast(foo::i32)
  • i32(foo)
  • foo.as(i32)
  • cast(i32)foo
  • @cast(i32)foo
  • cast[i32](foo)
  • @cast[i32](foo)
  • [i32]foo
  • (i32)(foo)
  • foo :> i32


Those are the ones I've found (and I've done the @ variation for the keyword based ones.


lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Re: Cast syntax
« Reply #1 on: October 27, 2018, 01:47:44 PM »
Personally I think the C style casts are hard to visually parse, so for me 18/19/22/23 feel like very little progress. I never remember the correct precedence rules :(

"foo as i32" is getting some traction due to some new languages using it, but you almost always want to wrap that in ( ) anyway if you do a . invocation: (foo as some_struct).a even if foo as some_struct.a isn't ambiguous. Anything that can be chained easily is better, which would exclude 5, 6. The "type(variable)" cast is ok for built in types, but start to look weird for things like structs, not to mention pointers and functions. I'd eliminate 16 as well for that reason. 20, 21 uses the [ ] which at this point feels like the wrong syntax direction, just like < >

We can eliminate 1, 2 as too close for the current syntax. If I also remove the dupes that have "@" for prefix (that could be introduced later if we wanted to) I see the following alternatives as possible:

a. cast(foo as i32)
b. cast(foo, i32)
c. foo->i32
d. (foo:i32) alt. cast(foo:i32)
e. (foo::i32) alt. cast(foo::i32)
f. foo.as(i32)
g. foo :> i32