Template return values?

Jesse Phillips Jessekphillips+D at gmail.com
Mon Dec 10 11:22:21 PST 2012


On Thursday, 6 December 2012 at 00:31:53 UTC, Jonathan M Davis 
wrote:

> Pretty much the only kind of situation that I remember running 
> into where I
> would consider Variant to be a good solution is one where you 
> literally have
> to return a type from a function where you can't know that type 
> at compile
> time, and there is no common type to use. And the only time 
> that I recall
> running into that recently was in writing a lexer (the values 
> of literals
> ended up having to be put in a variant type). There are 
> obviously other use
> cases besides that (database-related stuff and spreadsheets 
> like you mentioned
> are other possibilities), but they are extremely rare in my 
> experience. In
> almost all cases, you can and should know the type at compile 
> time, in which
> case, using Variant makes no sense.
>
> - Jonathan M Davis

My experience with Variant has come from integration in LuaD 
(Probably like what I'd want from JSON).

My main use has been an Algebraic type of string, string[], and 
string[][string]. (Lua does not have arrays or dictionaries, it 
has a table).

In general when I request data I know which of these it is, 
however I do have some generic code to operate on any of these 
types, thus:

     if(myVar.peek!(string[][string]))
         ... myVar.get!(string[][string])

Does get repetitive and messy to read.

On another note, it is sad I can't have that defined as 
MyType[MyType].


More information about the Digitalmars-d-learn mailing list