Bug 4087 – Static Node struct of std.range.SListRange

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-04-13T14:39:00Z
Last change time
2015-06-09T05:13:45Z
Keywords
patch
Assigned to
andrei
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-04-13T14:39:00Z
(This is kind of my first patch. Not using a patch command yet.) Inside the module std.range.SListRange, at about line 1586 there is: struct SListRange(T, Topology topology = Topology.flexible) { private: struct Node { T _value; Node * _next; } Node * _root; The D2 specs here say: http://www.digitalmars.com/d/2.0/struct.html A struct can be prevented from being nested by using the static attribute, but then of course it will not be able to access variables from its enclosing scope. I think currently all D structs are static, that feature is not implemented yet, but once that will be implemented the Node of SListRange will be 3 words long instead of 2 (and probably in practice 4 words), so I suggest to modify the code like this: struct SListRange(T, Topology topology = Topology.flexible) { private: static struct Node { T _value; Node * _next; } Node * _root; Likewise, I suggest to add the static attribute to nested structs in Phobos everywhere it's not necessary to access outer names.
Comment #1 by robert — 2010-04-13T14:45:41Z
Make sure you include the patch keyword when you add a patch, this way Walter/Don/Andrei can find it more easily :)
Comment #2 by andy — 2015-01-27T01:11:49Z
I cannot find any reference to SListRange in code, it must be gone by now. Closing.
Comment #3 by bearophile_hugs — 2015-01-27T01:15:29Z
(In reply to AndyC from comment #2) > I cannot find any reference to SListRange in code, it must be gone by now. > Closing. Reopened. They are named SList/DList now, of course: struct SList(T) { import std.exception : enforce; import std.range : Take; import std.range.primitives; import std.traits; private struct Node { Node * _next; T _payload; }
Comment #4 by schveiguy — 2015-01-27T01:38:27Z
All structs inside other structs are static. From the description "A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument." Note that applying static ONLY will prevent a context pointer for the struct that is declared in a function scope, not a struct with a local function alias (for which I actually think we should put an example on the page, I don't know exactly what this means). There is no need to apply static to the inner struct. There is no intention for this to mean, now or in the future, that the struct has a hidden reference pointer to its outer struct. That feature only happens for classes.
Comment #5 by bearophile_hugs — 2015-01-27T02:01:58Z
(In reply to Steven Schveighoffer from comment #4) > applying static ONLY will prevent a context pointer for the struct > that is declared in a function scope, not a struct with a local function > alias Thank you Steven for the explanation; after years of D usage I still miss some important details.