Forgot your password?
typodupeerror

Comment Re:Comparison to Objective-C? (Score 1) 570

Note that in your Java examples, you are checking, then casting anyway. The D version does both the check, and subsequent conditional cast, all at one go. Aesthetics, I suppose.

But you don't understand, I make no point about casts but about operations which instead of throwing return a bogus value when they fail. I don't know D, don't know if there are other such operations so I write about casts.
As far as in-the-language operations go... Array bounds checking is enabled for debug builds (on all three array types), and throws an exception. Asserts throw -- although generally one shouldn't catch these except in unit tests where failure was the desired result. Running "out" of memory throws. (Attempting to access members of nulls gives an Access Violation currently. I admit that, in debug builds, a NullReferanceException/Error wouldn't be a bad idea.) So the general rule of thumb seems to be: if you expect failure a notable portion of the time (about half or so is my personal choice, although its difficult to quantify) return a default or "failed" value; otherwise throw an exception, and be sure to document it.

"Casting often is a sign of a bad design."
That is true, but often it is a sign of a language's bad design (think Java pre-1.5 collections) and you just have to cast.
I'd rather not, if I can help it. But I do hear you. Luckily true/explicit casting is rarely neccessary in D. Given a D array that needs to become a pointer, there's a ".ptr" property which directly exposes the raw pointer. The usual implicit upcasts of the C/C++/Java/etc world are still there (short -> int, for example). And we do have a fairly nice templates system in place, one which is by-design suitable for meta-programming, rather than having to be "abused" for such practices as C++ templates often are. (I've seen some interesting things done with this, such as compile-time regexp engines, and a parser generator on a subset of BNF. I'm almost ashamed not to have made more use of it myself.) In short I've not often seen this problem in D, but it might just be a result of personal style.

Slashdot Top Deals

The reward for working hard is more hard work.

Working...