www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D2 questions

reply ponce <spam spam.com> writes:
In D2:

1. Is A!T implicitely convertible to A!(const(T)) ?

2. Is A!(immutable(T)) implicitely convertible to A!(const(T)) ?

3. Is A!(immutable(T)) implicitely convertible to A!(const(T)) ?

4. 

inout(T) myFunction(inout(T) x)
{
    inout(T) a;
}

Can we declare a inout(T) variable in an inout(T) function ?

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) ?


Thanks :)
Nov 04 2010
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Fri, 05 Nov 2010 00:00:53 -0400, ponce wrote:

 In D2:
 
 1. Is A!T implicitely convertible to A!(const(T)) ?

That depends on A. // yes template A(T) { alias T[] A; } // no struct A(T) { T t; }
 2. Is A!(immutable(T)) implicitely convertible to A!(const(T)) ?

 3. Is A!(immutable(T)) implicitely convertible to A!(const(T)) ?

See 1. (Those were the same question, by the way.)
 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.)
 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) -Lars
Nov 05 2010
next sibling parent ponce <spam spam.org> writes:
Thanks Lars. Makes sense. I included redundant questions to make sure you're
not a bot. :)
Nov 05 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 05 Nov 2010 05:00:10 -0400, Lars T. Kyllingstad  
<public 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
Nov 05 2010