digitalmars.D.learn - D2 questions
- ponce (29/29) Nov 04 2010 In D2:
- Lars T. Kyllingstad (24/61) Nov 05 2010 That depends on A.
- ponce (1/1) Nov 05 2010 Thanks Lars. Makes sense. I included redundant questions to make sure yo...
- Steven Schveighoffer (9/56) Nov 05 2010 Yes, it's intended. You can only declare inout variables on the stack, ...
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
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
Thanks Lars. Makes sense. I included redundant questions to make sure you're not a bot. :)
Nov 05 2010
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: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.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.)This is correct. The idea is to handle the common case, and then if specializations are desired, they can be implemented separately. -Steve5. 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)
Nov 05 2010