Template return values?

Jonathan M Davis jmdavisProg at gmx.com
Tue Dec 4 17:40:35 PST 2012


On Tuesday, December 04, 2012 23:28:25 Dmitry Olshansky wrote:
> 12/4/2012 10:40 PM, Jonathan M Davis пишет:
> > On Tuesday, December 04, 2012 21:43:09 Dmitry Olshansky wrote:
> >> Well TDPL claims multiple alias this is allowed so in some distant
> >> future it maybe possible for Varaint to alias this to all built-in types.
> > 
> > That would be pretty hideous IMHO. There's a reason that D eschews
> > implicit
> > conversions in general. And in this case, you'd very quickly end up with
> > accidental conversions which resulted in exceptions being thrown by
> > Variant.
> Examples?

Unlike C++, no implicit conversion occurs via constructors. In C++, you get up 
to three implicit conversions when matching function parameters. In D, without 
alias this, you get 0. C++ has way more places where it allows implicit 
conversions than D does. D has very few.

If you start letting Variants implicitly convert, then you get problems like

int i = v;

throwing an exception because v holds a string - or a user defined type which 
can't convert to int.

> > I think that it's _much_ better to require explicit conversions from
> > Variant.
> Nope. The point of Variant is to feel natural.

There's nothing natural about Variant. It's attempting to add dynamic typing 
to a statically typed language. And as you can't know what it's type really is 
at compile time, all conversions require type checking at runtime. And 
implicit conversions must be determined at compile time. So really, the 
implicit conversions make no sense at all. And I would generally expect that 
code would have to check the type of a Variant before doing a conversion - 
otherwise it's bound to throw an exception a good portion of the time - so 
implicit conversions make that much less sense.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list