digitalmars.D.learn - cast(A)b is not an lvalue
- Namespace (6/6) Dec 26 2012 If I don't comment out line 19 I get:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/14) Dec 26 2012 I can answer the question in the subject line without looking at dpaste:...
- Namespace (2/10) Dec 26 2012 My question is: Should not work all three?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (26/38) Dec 26 2012 Here is the code:
- monarch_dodra (19/64) Dec 26 2012 The example is much better with a "new A();" actually ;)
- monarch_dodra (4/63) Dec 26 2012 Wait, never mind. Your example is better.
- Maxim Fomin (3/7) Dec 26 2012 What does this for? I constantly face in code samples shared in
- Namespace (4/15) Dec 26 2012 It's automatically generated from Dpaste templates.
- Andrej Mitrovic (2/7) Dec 26 2012 Probably for D1 compatibility. D1 didn't have writeln.
If I don't comment out line 19 I get: /home/c803/c821.d(19): Error: function c821.foo (ref A a) is not callable using argument types (B) /home/c803/c821.d(19): Error: cast(A)b is not an lvalue Code: http://dpaste.dzfl.pl/89f55c62 Should not work all three?
Dec 26 2012
On 12/26/2012 07:37 AM, Namespace wrote:If I don't comment out line 19 I get: /home/c803/c821.d(19): Error: function c821.foo (ref A a) is not callable using argument types (B) /home/c803/c821.d(19): Error: cast(A)b is not an lvalue Code: http://dpaste.dzfl.pl/89f55c62 Should not work all three?I can answer the question in the subject line without looking at dpaste: Yes, in many cases the result of a cast operation is an rvalue. It is a temporary that is constructed at the spot for that cast operation. Imagine casting an int to a double. The four bytes of the int is nowhere close to what the bit representation of a double is, so a double is made at the spot. Ali
Dec 26 2012
I can answer the question in the subject line without looking at dpaste: Yes, in many cases the result of a cast operation is an rvalue. It is a temporary that is constructed at the spot for that cast operation. Imagine casting an int to a double. The four bytes of the int is nowhere close to what the bit representation of a double is, so a double is made at the spot. AliMy question is: Should not work all three? IMO: yes.
Dec 26 2012
On 12/26/2012 09:05 AM, Namespace wrote:Here is the code: import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln; class A { } class B : A { } void foo(ref A a) { } void main() { A a = new A(); A ab = new B(); B b = new B(); foo(a); foo(ab); foo(b); // < compile error } foo() takes a class _variable_ by reference (not a class _object_ by reference). Since b is not an A variable, one is constructed on the spot. Imagine foo() actually does what its signature suggest: void foo(ref A a) { a = new B(); } That line above is an attempt to modify the caller's rvalue. AliI can answer the question in the subject line without looking at dpaste: Yes, in many cases the result of a cast operation is an rvalue. It is a temporary that is constructed at the spot for that cast operation. Imagine casting an int to a double. The four bytes of the int is nowhere close to what the bit representation of a double is, so a double is made at the spot. AliMy question is: Should not work all three? IMO: yes.
Dec 26 2012
On Wednesday, 26 December 2012 at 17:13:14 UTC, Ali Çehreli wrote:On 12/26/2012 09:05 AM, Namespace wrote:The example is much better with a "new A();" actually ;) //---- class A { } class B : A { void B_method(); } void foo(ref A a) { a = new A(); } void main() { B b = new B(); foo(b);//so now after this, b holds a A object? That would be catastrophic... b.B_method(); //Awwww crap... }Here is the code: import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln; class A { } class B : A { } void foo(ref A a) { } void main() { A a = new A(); A ab = new B(); B b = new B(); foo(a); foo(ab); foo(b); // < compile error } foo() takes a class _variable_ by reference (not a class _object_ by reference). Since b is not an A variable, one is constructed on the spot. Imagine foo() actually does what its signature suggest: void foo(ref A a) { a = new B(); } That line above is an attempt to modify the caller's rvalue. AliI can answer the question in the subject line without looking at dpaste: Yes, in many cases the result of a cast operation is an rvalue. It is a temporary that is constructed at the spot for that cast operation. Imagine casting an int to a double. The four bytes of the int is nowhere close to what the bit representation of a double is, so a double is made at the spot. AliMy question is: Should not work all three? IMO: yes.
Dec 26 2012
On Wednesday, 26 December 2012 at 19:45:53 UTC, monarch_dodra wrote:On Wednesday, 26 December 2012 at 17:13:14 UTC, Ali Çehreli wrote:Wait, never mind. Your example is better. I actually fell in the "trap" thinking my variable got modified :)On 12/26/2012 09:05 AM, Namespace wrote:The example is much better with a "new A();" actually ;)Here is the code: import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln; class A { } class B : A { } void foo(ref A a) { } void main() { A a = new A(); A ab = new B(); B b = new B(); foo(a); foo(ab); foo(b); // < compile error } foo() takes a class _variable_ by reference (not a class _object_ by reference). Since b is not an A variable, one is constructed on the spot. Imagine foo() actually does what its signature suggest: void foo(ref A a) { a = new B(); } That line above is an attempt to modify the caller's rvalue. AliI can answer the question in the subject line without looking at dpaste: Yes, in many cases the result of a cast operation is an rvalue. It is a temporary that is constructed at the spot for that cast operation. Imagine casting an int to a double. The four bytes of the int is nowhere close to what the bit representation of a double is, so a double is made at the spot. AliMy question is: Should not work all three? IMO: yes.
Dec 26 2012
On Wednesday, 26 December 2012 at 17:13:14 UTC, Ali Çehreli wrote:Here is the code: import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln;What does this for? I constantly face in code samples shared in this NG.
Dec 26 2012
On Wednesday, 26 December 2012 at 19:52:21 UTC, Maxim Fomin wrote:On Wednesday, 26 December 2012 at 17:13:14 UTC, Ali Çehreli wrote:It's automatically generated from Dpaste templates. all: Thanks for the explanation.Here is the code: import std.stdio; static if (!is(typeof(writeln))) alias writefln writeln;What does this for? I constantly face in code samples shared in this NG.
Dec 26 2012
On 12/26/12, Maxim Fomin <maxim maxim-fomin.ru> wrote:Probably for D1 compatibility. D1 didn't have writeln.static if (!is(typeof(writeln))) alias writefln writeln;What does this for? I constantly face in code samples shared in this NG.
Dec 26 2012