Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's why D has a cast keyword:

    ubyte c = cast(ubyte)i;
instead of:

    unsigned char c = (unsigned char)i;
Casts are a blunt instrument that subvert the type system, and so they need to be greppable.

Having the cast keyword also removes the grammatical ambiguities in the expression syntax.



Do you often grep for casts? I never do that.


I regard every cast as a bug in my own code and try to refactor it so there aren't any. I can't get rid of all of them, but they're always worth a second look.

I don't normally grep for them, but others have told me they did.

P.S. one thing about D is you can do things like this:

    ubyte b = i;            // error, losing bits
    ubyte b = cast(ubyte)i; // ugly cast
    ubyte b = i & 0xFF;     // no cast, no error!
It's just one of the nice little details that making programming in D a pleasure.


These things are better checked automatically with a static linter, that presumably already has a full parser and AST representation of the language and knows what is a cast and not.

Nobody should keep a checklist of "100 things to grep for when doing a code review".


Try to think about why you might want to do that. It makes a lot of sense, but if you're not doing it, that might be enlightening...


When doing appsec review in C or C++, yes!


Honestly I know I don’t do that either. I mean if there was some special case where I remembered “oh yeah I had to cast that variable in this special case”. In general I avoid casting as much as I can in C/C++, but especially in C.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: