www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: inner member classes in final outer class

reply coxalan <coxalan web.de> writes:
Bruno Medeiros Wrote:

 I was thinking the following:
 
    class Outer {
    }
 
    class Inner(alias outer) {
      static assert(is(outer : Outer)); // Just a check
    }
 
    final Outer o1 = new Outer();
    final Outer o2 = new Outer();
 
    void main() {
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);
 
        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }

Thanks. That looks quite promising. But it does not compile for me: test.d(12): class test.Inner(alias outer) is used as a type test.d(12): variable test.main.i1 voids have no value
 Having each Inner class parameterized with an alias, is like having a 
 compile-time constant member (the outer variable). This poses some 
 restrictions though: the outer classes variables cannot be declared 
 inside functions (because variables inside functions cannot be used as 
 alias parameters),

I'm do not exactly understand that. Where can I find documentation on that?
 and the Inner classes are no longer compatible 
 (covariant) with each other, although you can create a common Inner 
 superclass.

That's ok. I always had the perception that o1.Inner and o2.Inner are different types. coxalan
Sep 16 2007
next sibling parent reply Matti Niemenmaa <see_signature for.real.address> writes:
coxalan wrote:
 Bruno Medeiros Wrote:
 
 I was thinking the following:

    class Outer {
    }

    class Inner(alias outer) {
      static assert(is(outer : Outer)); // Just a check
    }

    final Outer o1 = new Outer();
    final Outer o2 = new Outer();

    void main() {
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);

        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }

Thanks. That looks quite promising. But it does not compile for me: test.d(12): class test.Inner(alias outer) is used as a type test.d(12): variable test.main.i1 voids have no value

Probably because he uses "Inner i[1234]" where he should use "Inner!(o[1234]) i[1234]". The easiest solution is to use "auto" on the left-hand side of the assignments. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Sep 16 2007
parent reply coxalan <coxalan web.de> writes:
Matti Niemenmaa Wrote:

 coxalan wrote:
 Bruno Medeiros Wrote:
 
 I was thinking the following:

    class Outer {
    }

    class Inner(alias outer) {
      static assert(is(outer : Outer)); // Just a check
    }

    final Outer o1 = new Outer();
    final Outer o2 = new Outer();

    void main() {
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);

        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }

Thanks. That looks quite promising. But it does not compile for me: test.d(12): class test.Inner(alias outer) is used as a type test.d(12): variable test.main.i1 voids have no value

Probably because he uses "Inner i[1234]" where he should use "Inner!(o[1234]) i[1234]". The easiest solution is to use "auto" on the left-hand side of the assignments.

Thanks. Now I get: test.d(5): static assert is false Disclaimer: I'm quite new to D. coxalan
Sep 16 2007
parent reply Matti Niemenmaa <see_signature for.real.address> writes:
coxalan wrote:
 Matti Niemenmaa Wrote:
 coxalan wrote:
 Bruno Medeiros Wrote:

 I was thinking the following:

    class Outer {
    }

    class Inner(alias outer) {
      static assert(is(outer : Outer)); // Just a check
    }

    final Outer o1 = new Outer();
    final Outer o2 = new Outer();

    void main() {
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);

        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }

But it does not compile for me: test.d(12): class test.Inner(alias outer) is used as a type test.d(12): variable test.main.i1 voids have no value

i[1234]". The easiest solution is to use "auto" on the left-hand side of the assignments.

Thanks. Now I get: test.d(5): static assert is false Disclaimer: I'm quite new to D.

Alright, I took a closer look and this compiles: class Outer { } class Inner(alias outer) { static assert(is(typeof(outer) : Outer)); // Just a check } final Outer o1; final Outer o2; static this() { o1 = new Outer; o2 = new Outer; } void main() { auto i1 = new Inner!(o1); auto i2 = new Inner!(o1); auto i3 = new Inner!(o2); auto i4 = new Inner!(o2); } -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Sep 16 2007
next sibling parent coxalan <coxalan web.de> writes:
Matti Niemenmaa Wrote:

 coxalan wrote:
 Matti Niemenmaa Wrote:
 coxalan wrote:
 Bruno Medeiros Wrote:

 I was thinking the following:

    class Outer {
    }

    class Inner(alias outer) {
      static assert(is(outer : Outer)); // Just a check
    }

    final Outer o1 = new Outer();
    final Outer o2 = new Outer();

    void main() {
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);

        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }

But it does not compile for me: test.d(12): class test.Inner(alias outer) is used as a type test.d(12): variable test.main.i1 voids have no value

i[1234]". The easiest solution is to use "auto" on the left-hand side of the assignments.

Thanks. Now I get: test.d(5): static assert is false Disclaimer: I'm quite new to D.

Alright, I took a closer look and this compiles: class Outer { } class Inner(alias outer) { static assert(is(typeof(outer) : Outer)); // Just a check } final Outer o1; final Outer o2; static this() { o1 = new Outer; o2 = new Outer; } void main() { auto i1 = new Inner!(o1); auto i2 = new Inner!(o1); auto i3 = new Inner!(o2); auto i4 = new Inner!(o2); }

Thanks!
Sep 16 2007
prev sibling parent reply coxalan <coxalan web.de> writes:
Matti Niemenmaa Wrote:

 Alright, I took a closer look and this compiles:
 
 class Outer {
 }
 
 class Inner(alias outer) {
 	static assert(is(typeof(outer) : Outer)); // Just a check
 }
 
 final Outer o1;
 final Outer o2;
 
 static this() {
 	o1 = new Outer;
 	o2 = new Outer;
 }
 
 void main() {
 
 	auto i1 = new Inner!(o1);
 	auto i2 = new Inner!(o1);
 
 	auto i3 = new Inner!(o2);
 	auto i4 = new Inner!(o2);
 }
 
 -- 
 E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi

One last question on this: Is it possible to do the same with D version 1? The current code comples with DMD v2.004, but with DMD 1.020 I get: forum.d(16): Error: cannot modify final variable 'o1' forum.d(17): Error: cannot modify final variable 'o2' coxalan
Sep 20 2007
parent reply Matti Niemenmaa <see_signature for.real.address> writes:
coxalan wrote:
 Matti Niemenmaa Wrote:
 class Outer {
 }

 class Inner(alias outer) {
 	static assert(is(typeof(outer) : Outer)); // Just a check
 }

 final Outer o1;
 final Outer o2;

 static this() {
 	o1 = new Outer;
 	o2 = new Outer;
 }

 void main() {

 	auto i1 = new Inner!(o1);
 	auto i2 = new Inner!(o1);

 	auto i3 = new Inner!(o2);
 	auto i4 = new Inner!(o2);
 }

Is it possible to do the same with D version 1? The current code comples with DMD v2.004, but with DMD 1.020 I get: forum.d(16): Error: cannot modify final variable 'o1' forum.d(17): Error: cannot modify final variable 'o2'

Make o1 and o2 const instead of final. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Sep 20 2007
parent coxalan <coxalan web.de> writes:
Matti Niemenmaa Wrote:

 coxalan wrote:
 Matti Niemenmaa Wrote:
 class Outer {
 }

 class Inner(alias outer) {
 	static assert(is(typeof(outer) : Outer)); // Just a check
 }

 final Outer o1;
 final Outer o2;

 static this() {
 	o1 = new Outer;
 	o2 = new Outer;
 }

 void main() {

 	auto i1 = new Inner!(o1);
 	auto i2 = new Inner!(o1);

 	auto i3 = new Inner!(o2);
 	auto i4 = new Inner!(o2);
 }

Is it possible to do the same with D version 1? The current code comples with DMD v2.004, but with DMD 1.020 I get: forum.d(16): Error: cannot modify final variable 'o1' forum.d(17): Error: cannot modify final variable 'o2'

Make o1 and o2 const instead of final. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi

Thanks! ~coxalan
Sep 20 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"coxalan" <coxalan web.de> wrote in message 
news:fcji0p$1tos$1 digitalmars.com...
 Bruno Medeiros Wrote:
        Inner i1 = new Inner!(o1);
        Inner i2 = new Inner!(o1);

        Inner i3 = new Inner!(o2);
        Inner i4 = new Inner!(o2);
    }


 But it does not compile for me:
 test.d(12): class test.Inner(alias outer) is used as a type
 test.d(12): variable test.main.i1 voids have no value

Change those declarations of i1, i2... to something like: auto i1 = new Inner!(o1); auto i2 = new Inner!(o2);
Sep 16 2007