opDispatch, duck typing, and error messages

Robert Jacques sandford at jhu.edu
Thu Apr 21 18:53:56 PDT 2011


On Thu, 21 Apr 2011 18:24:55 -0400, Adam D. Ruppe  
<destructionator at gmail.com> wrote:
[snip]
> Or, there's a whole new approach:
>
> e) Duck typing for ranges in to!() might be a bad idea. Again, remember,
> a class might legitimately offer a range interface, so it would
> trigger this message without opDispatch.
>
> If ranges are meant to be structs, maybe isInputRange should check
> is(T == struct)? This doesn't sit right with me though. The real
> problem is to!() - other range functions probably don't overload
> on classes separately than ranges, so it won't matter there.
>
>
> I think the best thing to do is simply to prefer Object over range.
>
> toImpl(T) if (isInputRange!(T) && (!is(T : Object)))
>
> Or something along those lines. Why? If the object has it's own
> toString/writeTo methods, it seems fairly obvious to me anyway that
> to!string ought to simply call them, regardless or what else there is.

There's actually a bug report regarding the toString vs range semantics  
issue, it's issue 5354 (  
http://d.puremagic.com/issues/show_bug.cgi?id=5354 ). Also note that  
classes (but not structs as of yet, see bug 5719) can provide their own  
to!T conversions.

However, what you ran into deserves a new bug report, since to!string  
should always be able to fall back to toString and it didn't.



More information about the Digitalmars-d mailing list