Template question

Michiel nomail at please.com
Thu Feb 22 13:32:28 PST 2007


Frits van Bommel wrote:

>> But the user of the function should also have the option to use it like
>> this:
>>
>> auto x = set!(int);
>>
>> To create the empty set (that can hold int). How can I implement this?
> 
> What's wrong with "auto x = new Set!(int);"? It's how every other class
> type is instantiated.

In this case I'm trying to create set literals. They're very pretty if
you have at least one parameter. I just wanted the empty set to not be
an exception to the syntax.

> An alternative would be to make your set type a struct instead of a
> class, then you could just have "Set!(int) x;" declare an empty set.
> You'd have to be careful about the copying behavior though, structs are
> copied by value, not reference. But if the struct contained a
> reference/pointer to the actual data, it could work.

Hm.. I'd rather use 'new' as an exception for the empty set than go
through that, I think.

>> PS: I would have liked to skip the type altogether for the empty set,
>> but the problem appears when you want to put something inside that set.
>> I've considered using a dummy type and to somehow construct the correct
>> set-type after the first insert, but it wouldn't be consistent with
>> normal sets and altogether too much trouble.
> 
> This would probably be a bad idea in a statically typed language. It
> could be made to work, but that would require you to specify the type
> whenever you wanted to extract an element from the set...

That way it would become like Java data structures before generics. :) I
knew it wouldn't be a good idea the minute I thought of it.

-- 
Michiel



More information about the Digitalmars-d mailing list