digitalmars.D.bugs - [Issue 15280] New: Unable to factor two simple functions into one
- via Digitalmars-d-bugs (102/102) Nov 02 2015 https://issues.dlang.org/show_bug.cgi?id=15280
https://issues.dlang.org/show_bug.cgi?id=15280 Issue ID: 15280 Summary: Unable to factor two simple functions into one inout function Product: D Version: D2 Hardware: x86_64 OS: Linux Status: NEW Severity: normal Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: andrei erdani.com Consider: struct List(T) { private struct Node { T payload; Node* next; } private const(Node)* root; private this(const Node* n) { root = n; } /** * Returns a copy of the tail of the list.Complexity $(BIGOH 1). */ List tail() { assert(root); auto n = root.next; return List(n); } /// ditto const(List) tail() const { assert(root); auto n = root.next; return const(List)(n); } inout(List) tail2() inout { assert(root); auto n = root.next; return inout(List)(n); } } void main() { List!(immutable int) lst; } The idea is to take the two almost identical (save for the use of the qualifier) functions tail() into one function tail2() that uses inout. Qualifier-independent code has been the main motivation behind the inout qualifier. The compiler issues errors: struct List(T) { private struct Node { T payload; Node* next; } private const(Node)* root; private this(const Node* n) { root = n; } /** * Returns a copy of the tail of the list.Complexity $(BIGOH 1). */ List tail() { assert(root); auto n = root.next; return List(n); } /// ditto const(List) tail() const { assert(root); auto n = root.next; return const(List)(n); } inout(List) tail2() inout { assert(root); auto n = root.next; return inout(List)(n); } } void main() { List!(immutable int) lst; } test.d(36): Error: mutable method test.List!(immutable(int)).List.this is not callable using a inout object test.d(42): Error: template instance test.List!(immutable(int)) error instantiating --
Nov 02 2015