Re: assigment to null class object member compiled? is this a bug?

2018-10-22 Thread Adam D. Ruppe via Digitalmars-d-learn

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.


Re: assigment to null class object member compiled? is this a bug?

2018-10-22 Thread Alex via Digitalmars-d-learn

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:
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


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.


Re: assigment to null class object member compiled? is this a bug?

2018-10-21 Thread dangbinghoo via Digitalmars-d-learn

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


Re: assigment to null class object member compiled? is this a bug?

2018-10-19 Thread Vijay Nayar via Digitalmars-d-learn

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 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.


assigment to null class object member compiled? is this a bug?

2018-10-19 Thread dangbinghoo via Digitalmars-d-learn

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 Programming Language>.


The latest dmd (2.082) and LDC2 behaves the same.