safer casts - take II

Dee Girl deegirl at noreply.com
Tue May 13 13:29:37 PDT 2008


terranium Wrote:

> Dee Girl Wrote:
> 
> > But cast that throws is not good primitive. Because you need expensive operation to implement cast that returns null on failure.
> 
> There is only minor need for this silent cast, I believe, so 1) since this is rarely needed, performance penalty is acceptable, 2) if you don't want performance penalty, avoid need for silent cast.

In my code I use often capability test. So cast that returns null is good for me. If I want to make sure I check on my side. In those cases I assign it to a variable that lives longer. So there are few casts that should throw in my code. If you want to make general statement about frequency of cast at best you collect data from programs.

> > If you use cast that throws in D then you are forced in that pattern.
> 
> The unwanted one (and forced to) should be unsafe functionality, rather than safe. The type switch from your example can be implemented in a more efficient way with a type hashtable.

This is incomplete answer. In my compiler implementation for switch if-else test is fast for 4 elements or less. Then linear search is fast up to 32 elements. Only then hash is best. But many variables influence this. Dee Girl



More information about the Digitalmars-d mailing list