Add get(default) same-type overload in Nullable#7437
Conversation
|
Thanks for your pull request and interest in making D better, @FeepingCreature! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
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 referencesYour 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 locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7437" |
|
Implicit conversions? |
|
Should be done on the caller side by explicitly specifying the parameter type... |
|
ping (@RazvanN7 ) |
|
My code was originally used the same type as the But I guess this was only as a precautionary measure for the case where T would be used further in the function, so if this causes trouble, I have nothing against this PR, but maybe @schveiguy has a different perspective. |
|
Wouldn’t this be a breaking change? I.e. Nullable!ubyte.get(1000) would stop working? I don’t remember the original discussion. On my phone right now I will look tomorrow. |
|
Yeah, I think you're right. Damn. When the default value is a more generic type than the stored value, this would break. The way to go would be a fix for 20670. But that seems hard, since the issue touches compiler data structure details afaics. (I can't tell if it's even possible to differentiate I don't know if there's anyone out there who even uses @Geod24 please remove the merge tag pending discussion? |
|
@FeepingCreature : Sure
True, but that also means it is currently kinda broken. import std.typecons;
void main ()
{
Nullable!ubyte nl = 42;
ubyte v1 = nl.get();
ubyte v2 = nl.get(0); // Error: cannot implicitly convert expression nl.get(0) of type int to ubyte
ubyte v3 = nl.get(1000); // Error: cannot implicitly convert expression nl.get(1000) of type int to ubyte
}The error on |
|
So my review of @RazvanN7's PR that includes this statement:
Was not the same as what is being proposed here. In the original, the code was: T get(T)(T datum) {...}And my point was simply that the In other words, I just wanted a name change, not functionality change.
Can't we fix this with an overload? i.e.: T get(T defaultVal) { ... } // should be preferred when literal can fit
auto get(U)(U defaultVal) {...}Edit: BTW, I tested this on run.dlang.io, and it works for your example (changing v3 to int of course). The point of get(default) is to replace |
|
Note @FeepingCreature the proposed overload solution fixes your issue as well. I think that's the best solution (for now, of course, fixing 20670 would be good too). |
|
Looks good, I'll change it tomorrow. |
06b4eed to
e74f4c9
Compare
Works around https://issues.dlang.org/show_bug.cgi?id=20670 , which may cause `Nullable` with arrays of `immutable struct`s to incorrectly strip away the `immutable`.
e74f4c9 to
a5c52a3
Compare
|
This didn't close any issue, so it won't show up in the changelog. Should an issue be made for this, or should a changelog entry be added? |
|
There isn't really a separate issue, and the root issue is hard to fix. I think this feature is new enough that most people haven't noticed it exists yet, and immutable structs are sadly kind of rare, so it'll probably be fine - people will notice the change just by virtue of trying to use .get and it working. |
Works around (I suspect) https://issues.dlang.org/show_bug.cgi?id=20670 , which may
cause
Nullablewith arrays ofimmutable structs to incorrectly stripaway the
immutable, creating impossible types that can't be reused.@RazvanN7 Since you added
get(default), is there any reason why it would have to infer a different type than theNullable's?