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
Add example of how to use scoped member variable #1644
Conversation
Note: import std.typecons;
class A
{
this(int) { }
}
struct S
{
typeof(scoped!A()) a; // fails!
} We should just make Scoped a non-voldemort struct in module scope. I really don't know why someone moved it inside. |
@monarchdodra: Any objections in moving |
@AndrejMitrovic AFAIK it was just a religious zeal with voldemort types. |
I'd also like to see If I think an option is to keep |
My point of view is that Voldermort (or more generally, private types) only make sense for adaptor, or operations with state. I'm thinking This is not the case of things like The real issue though, was how dangerous scoped's semantics are, in particular, it's constructions and destruction semantics: A So I'm OK if we allow them as members of aggregates, but we really need to make sure they are properlly documented, and explain the hows, why, and dangers of using them. Off topic: Why is |
I think part of the issue was the diffuclty of definining initialization semantics for it. Making it voldermort allowed us to bail on answering that question. Also, I don't see how it could be any safer, when you can place said structs on the stack? It's an escaping reference no matter how you see it. ALso, I'm unsure about the "number of indirections". It keeps locality, yes, but to call something on the class, you have to first dereference it, and then call the method on the acquired class reference. I'm not sure there's any way out of it for calling member methods, but it's redundant for accessing members. This is getting off topic, but I think This post is relevant: http://forum.dlang.org/thread/xfbeberchntuqrqasfdn@forum.dlang.org?page=2#post-uyjavwotvbiugppfzbgs:40forum.dlang.org |
Look at typically Javaescue D code:
You walk 2 memory location to get at books data. With scoped the data is "right there" inside of the Shelf. 4th if you embded something into a struct, it's again not any worse the the usual struct. Simply because it's prohibited by D spec to store any internal pointers (including to the Scoped part) inside of structs, in the end it makes for the exactly the same "do not escape references to the stack-allocated structs" as w/o Scoped involved. |
Unfortunately opDispatch has its own set of issues. I don't think you can currently achieve perfect forwarding with it. |
So, are we deciding to make Scoped a non-voldermort type then? I think that's the best solution. @eco ? |
I agree that'd be the ideal solution. I'll defer to someone else who knows more about the issues involved to actually make the change though. Feel free to close this. |
What's your problem here? Align on struct does work. |
Make it non-copyable? |
For aligning on the heap. Not on the stack. Also,
Yes, it's not that big of an issue actually. |
The pull as is is LGTM. |
Add example of how to use scoped member variable
Moving Scoped!T would break inner class pointers, so there should be a runtime check to catch this. |
The syntax for how to do this has changed several times so it's time to document the right way to do it.