Template return values?

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 5 11:31:52 PST 2012


On Wednesday, December 05, 2012 22:07:48 Dmitry Olshansky wrote:
> 12/5/2012 5:40 AM, Jonathan M Davis пишет:
> > There's nothing natural about Variant. It's attempting to add dynamic
> > typing to a statically typed language.
> 
> Bleh that smells religious. There are cases where one may just go for it
> to simplify matters. Spreadsheets, Db rows and whatnot often operate in
> Variants. Simply because one can't know ahead of time what the content
> is in a arbitrary DB.

Yes, but that inevitably forces you to check the type in order to handle it 
correctly, which means that implicit conversion just doesn't work. In order 
for implicit conversion to work, you have to be able to assume that you're 
dealing with a particular type, and if you can do that, why are you using a 
Variant in the first place? Just use a common type.

> > 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 -
> 
> to do what? throw an Exception?? :)

To know what on earth you're suposed to do with the variable. If it's a type 
that you can't handle, then yes, you'd probably have to throw an exception, 
but you generally use a Variant because you need to be able to return multiple 
types from the same function, and each is going to be treated differently, 
otherwise you could have just used a common type to begin with.

I just don't see how it makes any sense to implicitly convert a Variant when 
the only reason to use a Variant is if the types involved have no common type. 
Since if they no common type, you're going to have to check what it is to know 
what you can validly do with it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list