www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this a bug?

reply Eric <eric x.com> writes:
   1 alias J = const C;
   2
   3 void main(string[] args)
   4 {
   5     J a = new C();
   6     I!(J) i = a;
   7 }
   8
   9 interface I(V) { }
  10
  11 class F(V) if (is(V : I!(V))) { }
  12
  13 class C : I!(J)
  14 {
  15     F!(J) m;
  16 }

The above code gives the following compile error:
Error:
template instance F!(const(C)) does not match template 
declaration F(V) if (is(V : I!V))
on line 15.

If I change line 1 to "alias J = C;" the code will compile.  The 
problem seems to be
the template constraint on line 11.  I think the constraint says, 
"If V can be automatically
converted to I!(V) then this template can be used".  However, 
line 6 does not give
an error, and it seems to be automaticaly converting J to I!(J).

-Eric
Apr 15 2016
parent reply ag0aep6g <anonymous example.com> writes:
On 15.04.2016 19:13, Eric wrote:
    1 alias J = const C;
    2
    3 void main(string[] args)
    4 {
    5     J a = new C();
    6     I!(J) i = a;
    7 }
    8
    9 interface I(V) { }
   10
   11 class F(V) if (is(V : I!(V))) { }
   12
   13 class C : I!(J)
   14 {
   15     F!(J) m;
   16 }

 The above code gives the following compile error:
 Error:
 template instance F!(const(C)) does not match template declaration F(V)
 if (is(V : I!V))
 on line 15.

 If I change line 1 to "alias J = C;" the code will compile.  The problem
 seems to be
 the template constraint on line 11.  I think the constraint says, "If V
 can be automatically
 converted to I!(V) then this template can be used".  However, line 6
 does not give
 an error, and it seems to be automaticaly converting J to I!(J).
Line 6 isn't accepted either. If you remove the constraint, the compiler complains about it. So it's just the next error in line. And really const C can't be converted to I!(const C) implicitly. The former is const, the latter is mutable => no go.
Apr 15 2016
parent reply Eric <eric x.com> writes:
On Friday, 15 April 2016 at 17:43:59 UTC, ag0aep6g wrote:
 On 15.04.2016 19:13, Eric wrote:
    1 alias J = const C;
    2
    3 void main(string[] args)
    4 {
    5     J a = new C();
    6     I!(J) i = a;
    7 }
    8
    9 interface I(V) { }
   10
   11 class F(V) if (is(V : I!(V))) { }
   12
   13 class C : I!(J)
   14 {
   15     F!(J) m;
   16 }
Line 6 isn't accepted either. If you remove the constraint, the compiler complains about it. So it's just the next error in line. And really const C can't be converted to I!(const C) implicitly. The former is const, the latter is mutable => no go.
Thanks. I see that now. Is there any way I can make the compiler understand that the interface is const? -Eric
Apr 15 2016
parent reply Eric <eric x.com> writes:
On Friday, 15 April 2016 at 18:22:02 UTC, Eric wrote:
 On Friday, 15 April 2016 at 17:43:59 UTC, ag0aep6g wrote:
 On 15.04.2016 19:13, Eric wrote:
    1 alias J = const C;
    2
    3 void main(string[] args)
    4 {
    5     J a = new C();
    6     I!(J) i = a;
    7 }
    8
    9 interface I(V) { }
   10
   11 class F(V) if (is(V : I!(V))) { }
   12
   13 class C : I!(J)
   14 {
   15     F!(J) m;
   16 }
Line 6 isn't accepted either. If you remove the constraint, the compiler complains about it. So it's just the next error in line. And really const C can't be converted to I!(const C) implicitly. The former is const, the latter is mutable => no go.
Thanks. I see that now. Is there any way I can make the compiler understand that the interface is const? -Eric
line 6 can be fixed like this: "const I!(J) i = a;" Now if I can just figure out how to fix line 15...
Apr 15 2016
parent reply Eric <eric x.com> writes:
On Friday, 15 April 2016 at 18:28:58 UTC, Eric wrote:
 line 6 can be fixed like this: "const I!(J) i = a;"
 Now if I can just figure out how to fix line 15...
This works: 1 alias J = const C; 2 3 void main(string[] args) 4 { 5 J a = new C(); 6 const (I!(J)) i = a; 7 } 8 9 interface I(V) { } 10 11 class F(V) if (is(V : const(I!(V)))) { } 12 13 class C : const (I!(J)) 14 { 15 F!(J) m; 16 } 17 -Eric
Apr 15 2016
parent reply ag0aep6g <anonymous example.com> writes:
On 15.04.2016 20:55, Eric wrote:
   13 class C : const (I!(J))
I think this const is ignored by the compiler.
   15    F!(J) m;
A little tip: You can omit the parentheses of template instantiations when the argument is a single identifier. That is, `F!(J)` can be written as `F!J`.
Apr 15 2016
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Saturday, April 16, 2016 00:28:46 ag0aep6g via Digitalmars-d-learn wrote:
 On 15.04.2016 20:55, Eric wrote:
   13 class C : const (I!(J))
I think this const is ignored by the compiler.
It's definitely ignored, and I'd argue that it's a bug in the compiler that it's even accepted. There's no such thing as a const or immutable class - just a const or immutable instance of a class. Member functions of a class can be const or immutable but not the class itself. Doing so is just equivalent to marking every function in the class as const or immutable. Deriving from a const or immutable class is nonsense. - Jonathan M Davis
Apr 15 2016