digitalmars.D.learn - assigment to null class object member compiled? is this a bug?
- dangbinghoo (17/17) Oct 18 2018 hi,
- Vijay Nayar (8/25) Oct 19 2018 Technically the code you have is syntactically correct. You are
- dangbinghoo (10/17) Oct 21 2018 the section 6.2, which is
- Alex (9/27) Oct 22 2018 You are wrong, actually:
- dangbinghoo (25/58) Oct 22 2018 no, I'm not, the code
- Adam D. Ruppe (10/11) Oct 22 2018 D compilers are allowed to make that an error, but it might not.
hi, why the code bellow compiles? --- import std.stdio; class A { int m; } void main() { A a; a.m = 1; } --- and running this code get: `segmentation fault (core dumped) ./test` I consider this couldn't be compiled according to book <The D Programming Language>. The latest dmd (2.082) and LDC2 behaves the same.
Oct 18 2018
On Friday, 19 October 2018 at 06:53:32 UTC, dangbinghoo wrote:hi, why the code bellow compiles? --- import std.stdio; class A { int m; } void main() { A a; a.m = 1; } --- and running this code get: `segmentation fault (core dumped) ./test` I consider this couldn't be compiled according to book <The D Programming Language>. The latest dmd (2.082) and LDC2 behaves the same.Technically the code you have is syntactically correct. You are permitted to create a class variable without assigning it to a class object. (Assigning it to a class object would look like "A a = new A();") Which section of The D Programming Language book makes you think this would not compile? I have the book as well, but I'm not quite sure what part of the book you're referring to.
Oct 19 2018
On Friday, 19 October 2018 at 09:08:32 UTC, Vijay Nayar wrote:Technically the code you have is syntactically correct. You are permitted to create a class variable without assigning it to a class object. (Assigning it to a class object would look like "A a = new A();") Which section of The D Programming Language book makes you think this would not compile? I have the book as well, but I'm not quite sure what part of the book you're referring to.the section 6.2, which is --- A a; a.x = 5; --- the book explained this should be refused to compile. thanks! -- dangbinghoo
Oct 21 2018
On Monday, 22 October 2018 at 01:39:48 UTC, dangbinghoo wrote:On Friday, 19 October 2018 at 09:08:32 UTC, Vijay Nayar wrote:You are wrong, actually: In the book, site 179 it states: "If you try to access a non-static member of a reference and the compiler can prove statically that the reference would definitely be null, it will refuse to compile the code." But the compiler is not able to prove that the reference is definitely null. At least in this case, with compiler, I think of. That's why it called runtime error.Technically the code you have is syntactically correct. You are permitted to create a class variable without assigning it to a class object. (Assigning it to a class object would look like "A a = new A();") Which section of The D Programming Language book makes you think this would not compile? I have the book as well, but I'm not quite sure what part of the book you're referring to.the section 6.2, which is --- A a; a.x = 5; --- the book explained this should be refused to compile. thanks! -- dangbinghoo
Oct 22 2018
On Monday, 22 October 2018 at 15:51:03 UTC, Alex wrote:On Monday, 22 October 2018 at 01:39:48 UTC, dangbinghoo wrote:no, I'm not, the code --- A a; a.x = 5 -- is the example explaining that 'the compiler can prove statically that the reference would definitely be null, it will refuse to compile the code.' and for `is not able to prove that the reference is definitely null` the books has another example code: --- A a; if (<condition>) { a = new A; } ... if (<condition>) { a.x = 43; // correct } --- I think Adam is giving a better reason: the envolving D now behaves different by default about the null flow catch with the book. thanks!On Friday, 19 October 2018 at 09:08:32 UTC, Vijay Nayar wrote:You are wrong, actually: In the book, site 179 it states: "If you try to access a non-static member of a reference and the compiler can prove statically that the reference would definitely be null, it will refuse to compile the code." But the compiler is not able to prove that the reference is definitely null. At least in this case, with compiler, I think of. That's why it called runtime error.Technically the code you have is syntactically correct. You are permitted to create a class variable without assigning it to a class object. (Assigning it to a class object would look like "A a = new A();") Which section of The D Programming Language book makes you think this would not compile? I have the book as well, but I'm not quite sure what part of the book you're referring to.the section 6.2, which is --- A a; a.x = 5; --- the book explained this should be refused to compile. thanks! -- dangbinghoo
Oct 22 2018
On Friday, 19 October 2018 at 06:53:32 UTC, dangbinghoo wrote:why the code bellow compiles?D compilers are allowed to make that an error, but it might not. With the current implementation, dmd that.d will compile, but dmd -O that.d will fail with an error. Yes, turning on optimizations happens to catch the null flow at compile time. It is just a matter of compiler implementation happening to catch it or not.
Oct 22 2018