Author Topic: [Email suggestion No. 3] type casts in C2 + bugs  (Read 1697 times)

magnusi

  • Newbie
  • *
  • Posts: 10
    • View Profile
[Email suggestion No. 3] type casts in C2 + bugs
« on: July 19, 2016, 10:26:37 AM »
The current cast<type>(what); does not really fit into C2 syntax wise, since it implies generics and that is not something that fits in C2 as it is now. Bas proposed this syntax in an email a few months ago:

Code: [Select]
(->type)what
This could be much better and due to the arrow, more understandable to those who don't know how casts work beforehand.

casting bug 1: C2C parser completely ignores invalid C-style casts and interprets statements that use it as ones using implicit conversion

example:

Code: [Select]
int32 i = 10;
char c = (char)i;

I think that this syntax should result in an error or at-least warning about C-style casts. Frankly, I know that C-style casts are hard to parse, so I am not sure how easy or hard that is to implement

casting bug 2: C2C does not work properly when I do intentionally a bad thing like this:

Code: [Select]
int32 i = 10;
char d = cast<char>(d); //undeclared identifier error

I think that the proper behavior should be issuing a message/error/warning about using uninitialized variable or casting to itself.

admin

  • Administrator
  • Newbie
  • *****
  • Posts: 11
    • View Profile
Re: [Email suggestion No. 3] type casts in C2 + bugs
« Reply #1 on: July 21, 2016, 08:03:47 AM »
I couldn't agree more. The <> syntax just looks like C++ templates is is just plain ugly..

However ugly, it does have the advantage of requiring parentheses around the thing being
cast. With the c-style cast, it's not always obvious what exactly is cast. This has to do with
operator precendence (and could be a source of bugs).

C-style cast are currently not working on purpose. Casts need to be fixed first.

The 2nd 'bug' is also done on purpose for now, since we don't have to check circular initializations now ;)
So the declaration is only added to the scope after it's been done, so after the initialization. But for better
error messages, it might be needed to change this

magnusi

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: [Email suggestion No. 3] type casts in C2 + bugs
« Reply #2 on: July 21, 2016, 02:34:16 PM »
I couldn't agree more. The <> syntax just looks like C++ templates is is just plain ugly..

However ugly, it does have the advantage of requiring parentheses around the thing being
cast. With the c-style cast, it's not always obvious what exactly is cast. This has to do with
operator precendence (and could be a source of bugs).

C-style cast are currently not working on purpose. Casts need to be fixed first.


The 2nd 'bug' is also done on purpose for now, since we don't have to check circular initializations now ;)
So the declaration is only added to the scope after it's been done, so after the initialization. But for better
error messages, it might be needed to change this

Yes, I understand it, what I mean is that the compiler is ignoring them, rather than issuing a proper warning about C-style casts or throwing an error.

admin

  • Administrator
  • Newbie
  • *****
  • Posts: 11
    • View Profile
Re: [Email suggestion No. 3] type casts in C2 + bugs
« Reply #3 on: July 22, 2016, 09:22:47 AM »
Yes, the parser ignores them currently, I'll put a task on my backlog to fix this properly

lerno

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [Email suggestion No. 3] type casts in C2 + bugs
« Reply #4 on: June 22, 2018, 11:16:09 AM »
C/C++/Java style casts of the form (Bar)foo is notoriously hard to visually parse because the precedence rules are rarely clear. The cast<Bar>(foo) is easier to visually parse since the object of the cast is clear. Another way is to introduce a post-fix notation with an "AS" operator or similar, e.g. foo as Bar where as binds directly to the object to the left:

Code: [Select]
foo as Bar // == cast<Bar>(foo)
foo.baz as Buz // == cast<Buz>(foo.baz)
(foo as Bar).fooBar // == cast<Bar>(foo).fooBar
i as u8 // == cast<u8>(i)
cc = ((c << 2) & 0xFF) as u8 // == cast<u8>((c << 2) & 0xFF)