-
-
Notifications
You must be signed in to change notification settings - Fork 743
RefCounted implementation for classes #3259
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
Conversation
This still needs a bit polishing (docs et.al.), but I wanted to discuss a few things upfront. |
I agree this doesn't need the lazy initialization stuff, but instead of making it non-nullable, why not make it work just like any class reference It shouldn't be hard to implement; just cast |
Added the @nogc test, adjusted the methods of the old RefCounted, and enabled ref escape checking for that.
Because it's a class value, not a class reference, i.e. you can't nullify TODO:
|
In fact using payload and refCount might hide members of the payload, will change that back to the old scheme of wrapping everything in refCountedStore. |
You can if
Analogous to // These are equivalent
RefCounted!T c;
RefCounted!T c2 = null; |
Mmh, I guess we could add a null state for RefCounted. auto c = RefCounted!C(); // this is null
auto c2 = refCounted!C(); // this is not |
Ouch. edit: Construction of the supertype |
This difference might be OK though, because one is clearly a function call while the other is a struct literal. |
assert(Foo.ctor == 1 && Foo.dtor == 0); | ||
} | ||
assert(Foo.ctor == 1 && Foo.dtor == 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could add a test for the inheritance case
f5ebe0c
to
cf69dfd
Compare
Changed the implementation to account for the discussed changes. |
} | ||
|
||
/// Boolean conversion for RefCounted, true when initialized. | ||
bool opCast(T:bool)() const @safe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This notation catches things like cast(int)
as well, it's probably better to use bool opCast(T)() if(is(T == bool))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it does not Error: template instance opCast!int does not match template declaration opCast(T : bool)()
, parameter specializations are exact matches despite the colon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, didn't know that, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weird that the syntax deviates from the is expression, just another accidental complexity.
Left to be done.
|
I am terribly sorry I didn't chime in sooner - I was on vacation the past two weeks. A lot of what @JakobOvrum has said above really resonates with my. Frankly, if we're going to overhaul
In short, its API should be nearly identical to that of Again, I'm sorry for all the legwork you've already done, Martin - I'll help in any way I can now that I'm back, including taking up these changes in my own PR. |
Not quite,
The implicit bool conversion is there, but
Will extend
I agree, we'll have to see how to deprecate it properly.
I did that before, named it
Right, let's see how far we can get.
You can make PRs that target my branch to collaborate on this. |
- add a RefCounted variant for classes - remove the autoInit stuff which isn't applicable for classes - @nogc and nothrow compatible
I think |
Ah, I didn't realize that |
I added support for any type in
Meanwhile I'll work on the destruction problem. |
I have proposed to remove |
I'll hit the ground with this once after we settle on what API we want in #3225. |
Done, dlang/dmd#4650. |
ping, what's the status of this? |
I believe we're waiting for #3225 to shake out before deciding what to do here. |
TODO:
|
Closing in favor of a complete PR of all smart refs (WIP https://github.com/MartinNowak/phobos/tree/smartRefs). |
because certain semantics aren't possible with classes
this() to guarantee that the payload is initialized