2 ms·
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 t
by WalterBright 2y ago
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.
- jenadine 2y agoDo you often grep for casts? I never do that.
- aa-jv 2y agoTry 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...
- WalterBright 2y agoI 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.
- Too 2y agoThese 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".
- SnowflakeOnIce 2y agoWhen doing appsec review in C or C++, yes!
- EasyMark 2y agoHonestly 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.