A case for opImplicitCast: making string search work better

Steven Schveighoffer schveiguy at yahoo.com
Fri May 15 06:01:47 PDT 2009


On Fri, 15 May 2009 06:07:10 -0400, downs <default_357-line at yahoo.de>  
wrote:

> Consider this type:
>
> struct StringPosition {
>   size_t pos;
>   void opImplicitCast(out size_t sz) {
>     sz = pos;
>   }
>   void opImplicitCast(out bool b) {
>     b = pos != -1;
>   }
> }
>
> Wouldn't that effectively sidestep most problems people have with find  
> returning -1?
>
> Or am I missing something?
>
> Of course, this would require a way to resolve ambiguities, i.e.  
> functions/statements with preferences - for instance, if() would  
> "prefer" bool over int. I don't know if this is possible.

No, I want the length of the string if it is not found, not -1.

It's not a question of -1 vs. false, it's a question of usability.  -1 can  
be tested as well as string.length, but -1 cannot be seamlessly forwarded  
to slicing operations.  Most of the time, you want to USE the index  
returned, not just check if it is valid.

-Steve



More information about the Digitalmars-d mailing list