D2 questions

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Fri Nov 5 02:00:10 PDT 2010


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


More information about the Digitalmars-d-learn mailing list