D2 questions

Steven Schveighoffer schveiguy at yahoo.com
Fri Nov 5 07:06:48 PDT 2010


On Fri, 05 Nov 2010 05:00:10 -0400, Lars T. Kyllingstad  
<public at kyllingen.nospamnet> wrote:

> On Fri, 05 Nov 2010 00:00:53 -0400, ponce wrote:
>> 4.
>>
>> inout(T) myFunction(inout(T) x)
>> {
>>     inout(T) a;
>> }
>>
>> Can we declare a inout(T) variable in an inout(T) function ?
>
> Currently you can, but I don't know whether that's intended or not.
> (AFAIK, inout is only halfway implemented in the compiler.)

Yes, it's intended.  You can only declare inout variables on the stack,  
you cannot declare them as members of a struct/class.

However, note that you can only assign inout(T) to inout(T), it doesn't  
accept any other types.

>
>
>> 5.
>>
>> T func(T x)
>> {
>>     // blah
>> }
>>
>> const(T) func(const(T) x)
>> {
>>     // blah
>> }
>>
>> immutable(T) func(immutable(T) x)
>> {
>>     // blah
>> }
>>
>> inout(T) func(inout(T) x)
>> {
>>     // blah
>> }
>>
>> How is the overloading resolved when calling func(x) where x is of type
>> T/const(T)/immutable(T)/inout(T) ?
>
> The closest match is always chosen, and inout(T) is last resort.
>
> When x is mutable T, the compiler looks for, in this order,
>   1. func(T)
>   2. func(const T)
>   3. func(inout T)
>
> When x is const(T), the compiler looks for
>   1. func(const T)
>   2. func(inout T)
>
> When x is immutable(T), the compiler looks for
>   1. func(immutable T)
>   2. func(const T)
>   3. func(inout T)

This is correct.  The idea is to handle the common case, and then if  
specializations are desired, they can be implemented separately.

-Steve


More information about the Digitalmars-d-learn mailing list