digitalmars.D - Ouch: return values as lvalue
- Lionello Lunesu (12/12) Jan 30 2007 Consider this home-made const:
- Andrei Alexandrescu (See Website For Email) (4/19) Jan 30 2007 const will take care of it. The code above fetches a member of an
- Lionello Lunesu (7/25) Jan 30 2007 "Andrei Alexandrescu (See Website For Email)"
- Andrei Alexandrescu (See Website For Email) (6/31) Jan 30 2007 Probably it could, but sometimes the object will be used:
- Andrei Alexandrescu (See Website For Email) (17/42) Jan 30 2007 It's a long discussion. I agree that non-templated code without effect
- Joel C. Salomon (6/9) Jan 30 2007 while(func() == 0)
- Andrei Alexandrescu (See Website For Email) (7/17) Jan 30 2007 Code in general. Consider:
- Joel C. Salomon (5/26) Jan 30 2007 And the lack of a preprocessor means statements like that are unlikely
- Andrei Alexandrescu (See Website For Email) (4/31) Jan 30 2007 Which is exactly what templates do. But, as I show in another post, all
- Hasan Aljudy (4/37) Jan 30 2007 so a template *could* theoretically generate code like that for some inp...
- Lionello Lunesu (3/43) Jan 30 2007 Yes, that's probably why Walter is opposed to warnings. See his post.
- Walter Bright (3/5) Jan 30 2007 I agree that the language should be very careful about issuing errors
- Lionello Lunesu (18/36) Jan 30 2007 "Andrei Alexandrescu (See Website For Email)"
- Andrei Alexandrescu (See Website For Email) (7/46) Jan 30 2007 I think that's a good point, but I wouldn't put a lot of faith in it. An...
- Oth Erou (10/11) Jan 30 2007 void main(){
- Lionello Lunesu (3/14) Jan 30 2007 The , ? :) Nice one!
- Oth Erou (4/5) Jan 31 2007 In fact there are two bugs according to typos,
Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs? L.
Jan 30 2007
Lionello Lunesu wrote:Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue. Andrei
Jan 30 2007
"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote in message news:45BF7C8B.3080506 erdani.org...Lionello Lunesu wrote:Yeah, I know, but it's odd: it's setting a member in a struct that's about to be deleted. It's like writing "{ int id=4; }". Wouldn't it be possible for the compiler to warn about "code without side-effect" or something? L.Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue.
Jan 30 2007
Lionello Lunesu wrote:"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote in message news:45BF7C8B.3080506 erdani.org...Probably it could, but sometimes the object will be used: foo(bar().baz += 1); But that can be detected, too. But in generic code you can't rely on one specific expectation. And Walter hates warnings :o). AndreiLionello Lunesu wrote:Yeah, I know, but it's odd: it's setting a member in a struct that's about to be deleted. It's like writing "{ int id=4; }". Wouldn't it be possible for the compiler to warn about "code without side-effect" or something?Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue.
Jan 30 2007
Lionello Lunesu wrote:"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote in message news:45BF7C8B.3080506 erdani.org...It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____. Consider: struct RealLock { ... } struct DummyLock { ... } alias DummyLock Lock; { Lock lock; ... } DummyLock doesn't do anything. The idea is that the user would replace the alias with RealLock in a multithreaded application. The compiler shouldn't issue an error, even though the code appears to do nothing interesting. AndreiLionello Lunesu wrote:Yeah, I know, but it's odd: it's setting a member in a struct that's about to be deleted. It's like writing "{ int id=4; }". Wouldn't it be possible for the compiler to warn about "code without side-effect" or something?Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue.
Jan 30 2007
Andrei Alexandrescu (See Website For Email) wrote:It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors? --Joel
Jan 30 2007
Joel C. Salomon wrote:Andrei Alexandrescu (See Website For Email) wrote:Code in general. Consider: 1 + 1 == 2; The code above isn't an error but it also doesn't do anything. It should be flagged as an error. The empty statement in your example is not "code" the way I meant it above. AndreiIt's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors?
Jan 30 2007
Andrei Alexandrescu (See Website For Email) wrote:Joel C. Salomon wrote:And the lack of a preprocessor means statements like that are unlikely to happen “by accident”. (I’m afraid I’m still thinking in C.) Unless this is the sort of thing a code generator might write. --JoelAndrei Alexandrescu (See Website For Email) wrote:Code in general. Consider: 1 + 1 == 2; The code above isn't an error but it also doesn't do anything. It should be flagged as an error. The empty statement in your example is not "code" the way I meant it above.It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors?
Jan 30 2007
Joel C. Salomon wrote:Andrei Alexandrescu (See Website For Email) wrote:Which is exactly what templates do. But, as I show in another post, all you really need is an alias. AndreiJoel C. Salomon wrote:And the lack of a preprocessor means statements like that are unlikely to happen “by accident”. (I’m afraid I’m still thinking in C.) Unless this is the sort of thing a code generator might write.Andrei Alexandrescu (See Website For Email) wrote:Code in general. Consider: 1 + 1 == 2; The code above isn't an error but it also doesn't do anything. It should be flagged as an error. The empty statement in your example is not "code" the way I meant it above.It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors?
Jan 30 2007
Andrei Alexandrescu (See Website For Email) wrote:Joel C. Salomon wrote:so a template *could* theoretically generate code like that for some input. Then .. if the compiler issued an error for it .. don't you think the situation would be "the compiler is getting in your way"?Andrei Alexandrescu (See Website For Email) wrote:Which is exactly what templates do. But, as I show in another post, all you really need is an alias. AndreiJoel C. Salomon wrote:And the lack of a preprocessor means statements like that are unlikely to happen “by accident”. (I’m afraid I’m still thinking in C.) Unless this is the sort of thing a code generator might write.Andrei Alexandrescu (See Website For Email) wrote:Code in general. Consider: 1 + 1 == 2; The code above isn't an error but it also doesn't do anything. It should be flagged as an error. The empty statement in your example is not "code" the way I meant it above.It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors?
Jan 30 2007
Hasan Aljudy wrote:Andrei Alexandrescu (See Website For Email) wrote:Yes, that's probably why Walter is opposed to warnings. See his post. L.Joel C. Salomon wrote:so a template *could* theoretically generate code like that for some input. Then .. if the compiler issued an error for it .. don't you think the situation would be "the compiler is getting in your way"?Andrei Alexandrescu (See Website For Email) wrote:Which is exactly what templates do. But, as I show in another post, all you really need is an alias. AndreiJoel C. Salomon wrote:And the lack of a preprocessor means statements like that are unlikely to happen “by accident”. (I’m afraid I’m still thinking in C.) Unless this is the sort of thing a code generator might write.Andrei Alexandrescu (See Website For Email) wrote:Code in general. Consider: 1 + 1 == 2; The code above isn't an error but it also doesn't do anything. It should be flagged as an error. The empty statement in your example is not "code" the way I meant it above.It's a long discussion. I agree that non-templated code without effect should issue a compile-time error (not warning) _____as long as under no change of type definitions etc., the code could make sense_____.while(func() == 0) ; — or do you only mean that extremely short-lived (effect-less) /assignments/ should be errors?
Jan 30 2007
Andrei Alexandrescu (See Website For Email) wrote:The compiler shouldn't issue an error, even though the code appears to do nothing interesting.I agree that the language should be very careful about issuing errors for code that doesn't do anything.
Jan 30 2007
"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote in message news:45BF7C8B.3080506 erdani.org...Lionello Lunesu wrote:I've been thinking about this some more. Why exactly is a member of an rvalue, an lvalue? struct S { int i; } //foo returns a struct, which is placed on stack/register, somewhere anyway S foo(); foo() = myS; // not allowed foo().i = 2; // suddenly allowed! Why? // func returns an int, which is also placed somewhere, // physically, stack/register doesn't matter int func(); func() = 2; // I can't write this... why? What exactly is the difference between setting the whole thing, or only one of its members? In the case of S above, there's no real difference, yet they behave differently? L.Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue.
Jan 30 2007
Lionello Lunesu wrote:"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote in message news:45BF7C8B.3080506 erdani.org...I think that's a good point, but I wouldn't put a lot of faith in it. An rvalue accepts member function calls, which in turn can return things like pointers (now) or lvalues (later). Probably calling certain members should be disallowed on rvalue objects, but right now D does not give such flexibility. AndreiLionello Lunesu wrote:I've been thinking about this some more. Why exactly is a member of an rvalue, an lvalue? struct S { int i; } //foo returns a struct, which is placed on stack/register, somewhere anyway S foo(); foo() = myS; // not allowed foo().i = 2; // suddenly allowed! Why? // func returns an int, which is also placed somewhere, // physically, stack/register doesn't matter int func(); func() = 2; // I can't write this... why? What exactly is the difference between setting the whole thing, or only one of its members? In the case of S above, there's no real difference, yet they behave differently?Consider this home-made const: struct Task { char[] ID; } private Task _CurrentTask; //mutable public Task CurrentTask() { return _CurrentTask; } //const public void StopTask() { CurrentTask.ID = null; } Notice the bug? That last line should read "_CurrentTask.ID = null;" Isn't there something the compiler can do to help me catch these bugs?const will take care of it. The code above fetches a member of an rvalue, which is an lvalue.
Jan 30 2007
Lionello Lunesu Wrote:Notice the bug?void main(){ real res = 0.01; while(res < 0,1) { if (res >= 0.1) printf("shooting over: %f\n", res), printf("Increasing.\n"); res*=1+1e-10; } }
Jan 30 2007
"Oth Erou" <Oth erou.ch> wrote in message news:epodno$1lvp$1 digitaldaemon.com...Lionello Lunesu Wrote:The , ? :) Nice one!Notice the bug?void main(){ real res = 0.01; while(res < 0,1) { if (res >= 0.1) printf("shooting over: %f\n", res), printf("Increasing.\n"); res*=1+1e-10; } }
Jan 30 2007
Lionello Lunesu Wrote:The , ? :) Nice one!In fact there are two bugs according to typos, which have changed a single character and go undetected by the compiler.
Jan 31 2007