Template return values?

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Dec 5 13:06:33 PST 2012


12/5/2012 11:31 PM, Jonathan M Davis пишет:
> 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.

Simply put it's the API that is more flexible the your concrete code. 
But there is a point in forcing the expected types as soon as possible.

In other words it's a sugar for calling Varaint.get for the type on the 
right. That's where the point that you don't like it while I think it 
could be handy.

>
>>> 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.

This is a sort of case against Varaint at large.

> 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.

It's not. If the only reason was that I'd use an opaque "box" type.

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list