const

Michel Fortin michel.fortin at michelf.com
Sun Mar 30 07:30:36 PDT 2008


On 2008-03-29 02:45:29 -0400, Walter Bright <newshound1 at digitalmars.com> said:

>>> typeof(a) foo(const(T) a) { ... }
>> 
>> That's a very specific problem that I know has been under your radar, and I
>> assume with time it'll get solved in a nice and clean way.
>> 
>> 
>>> I'm worried it may have covariant/contravariant problems, but perhaps we
>>> can work that out.
>> 
>> I don't know what you mean by that.
> 
> Imagine T is a base class, and a is a derived class. What is the typeof(a) ?

Good question indeed. I'd say the base class with the constness of the 
caller, but that's pretty ackward.

I think the best solution would be to add generics to the language, so 
you can write your function like a template but it would only generate 
the code once:

	Derived foo(generic Derived : const(T))(Derived a) { ... }

Obviously, generics would impose more limitations to what you can do 
than than regular templates.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list