phobos icon indicating copy to clipboard operation
phobos copied to clipboard

Allow assigning `null` to `Nullable`

Open GrimMaple opened this issue 1 year ago • 10 comments

GrimMaple avatar Oct 26 '22 19:10 GrimMaple

Thanks for your pull request and interest in making D better, @GrimMaple! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8616"

dlang-bot avatar Oct 26 '22 19:10 dlang-bot

Assigning null is different than setting the Nullable state to null, see: The problem with Nullable: nullable(null).isNull is false.

I'm not sure this is a good idea. @atilaneves

dkorpel avatar Oct 27 '22 10:10 dkorpel

I think the real issue here is the name Nullable.

If it were called Optional or Maybe, nobody would expect assigning null to work like this in the first place, and nobody would be surprised to find out that it doesn't.

Unfortunately, we must keep the name Nullable for compatibility, but perhaps we could rename it and make Nullable an alias?

pbackus avatar Nov 04 '22 23:11 pbackus

Seeing how this PR isn't a particular hit with the D community, there is another solution I can propose: a compile-time error when trying to assign null to nullable saying something like "Use nullify instead"

@pbackus just renaming it to optional wouldn't be enough -- we would need to invent nullopt or something

GrimMaple avatar Nov 05 '22 14:11 GrimMaple

I'm not convinced of the utility of this either.

atilaneves avatar Nov 10 '22 15:11 atilaneves

I don't see a lot of movement on this, so I'm going to reask: would making a error on assigning null be more of an accepted solution?

GrimMaple avatar Jan 16 '23 13:01 GrimMaple

I don't see a lot of movement on this, so I'm going to reask: would making a error on assigning null be more of an accepted solution?

It's already a compile-time error, isn't it?

atilaneves avatar Jan 18 '23 17:01 atilaneves

I don't see a lot of movement on this, so I'm going to reask: would making a error on assigning null be more of an accepted solution?

It's already a compile-time error, isn't it?

Well it's not a particularly great compile-time error. I was suggesting explicitly erroring "Use nullify instead of assigning null"

GrimMaple avatar Jan 19 '23 06:01 GrimMaple

I don't see a lot of movement on this, so I'm going to reask: would making a error on assigning null be more of an accepted solution?

It's already a compile-time error, isn't it?

Well it's not a particularly great compile-time error. I was suggesting explicitly erroring "Use nullify instead of assigning null"

That sounds like a good idea.

atilaneves avatar Jan 19 '23 08:01 atilaneves

If the null is a valid value, (e.g. Nullable!string), you should still be able to assign that. Otherwise (e.g. Nullable!int) it can give an error.

dkorpel avatar Jan 19 '23 09:01 dkorpel