Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Moving CS8618 'Non-nullable field is uninitialized' to the construction place #2411

Closed
alexk8 opened this issue Apr 10, 2019 · 9 comments

Comments

@alexk8
Copy link

alexk8 commented Apr 10, 2019

class A{
  public string str; //warning: not initialized
}

This warning is something bad by design,
it fits badly into deserialization concept which is used everywhere! (json,EF,ORM,Mappers,etc)
I suggest that it's caller's responsibility to construct object properly,
I'd like to see something like this:

#nullable enable

class A{
  public string str; //do not warn (not initialized)!!! that' a lot of pain
}

void Main(){
  A a1 = new A(); //do warn: "str is not initialized"
  A a2 = new A{str="str"}; //ok
}

*** EDITED ***
some other crazy ideas:

  A a3 = deserialize A(a => {
    //a.str = "str";
  }); //throws runtime error "str is not initialized"
@YairHalberstadt
Copy link
Contributor

#2328

@Unknown6656
Copy link
Contributor

@alexk8 This has nothing to to with the proposal at all, however, I have to say, that your profile picture gave my sleep-depraved brain quite a start! good one!
image

@Joe4evr
Copy link
Contributor

Joe4evr commented Apr 10, 2019

This warning is something bad by design

Only because the warning is in the wrong place. Proposing to fix that by putting a new keyword into the language like this has an absurdly high bar, whereas the linked proposal would solve the same problem at a much lower cost and that is more in-line with the design of NRTs.

@marksmeltzer
Copy link

I agree with @Joe4evr. The solutions proposed on #2328 are far more useful.

@alexk8
Copy link
Author

alexk8 commented Apr 11, 2019

Only because the warning is in the wrong place. Proposing to fix that by putting a new keyword into the language like this has an absurdly high bar, whereas the linked proposal would solve the same problem at a much lower cost and that is more in-line with the design of NRTs.

Agree, but NRTs concept does introduce lots of new magic, doesn't it? Let's say that's separete, quite crazy proposal, but why not? Of course "deserializaton" can be done totaly by reflection, The only thing I'd like to have is null-checks after runtime weak construction, in more optimal and strightforward way - compiler generated. This probably does not require keyword itself, some other magic may happen in compiletime, it needs only a hint to do that. Somebody may invent other way, I've just shown my general idea.

[MustInitialize] from #2328 would be quite messy (used everywhere), this behaviour should be default by design IMHO

@alexk8
Copy link
Author

alexk8 commented Apr 11, 2019

However, I agree that this "deserialization" stuff is not really needed. The main problem is:

the warning is in the wrong place.

@alexk8
Copy link
Author

alexk8 commented Apr 11, 2019

closing this issue in favour of 2328

@alexk8 alexk8 closed this as completed Apr 11, 2019
@alexk8 alexk8 changed the title Proposal/Discussion: NRT design (deserialization pain, etc) CS8618 'Non-nullable field is uninitialized' is in a wrong place May 3, 2019
@alexk8
Copy link
Author

alexk8 commented May 3, 2019

I've decided to reopen the issue just to ask - Is it even possible to move the warning to the construction side, by default?
And only if a non-default constructor exist, then that constructor must initialize all properties

@alexk8 alexk8 reopened this May 3, 2019
@alexk8 alexk8 changed the title CS8618 'Non-nullable field is uninitialized' is in a wrong place Proposal: Moving CS8618 'Non-nullable field is uninitialized' to the construction place May 3, 2019
@alexk8
Copy link
Author

alexk8 commented May 4, 2019

Found more similar #2109
closing

@alexk8 alexk8 closed this as completed May 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants