[Issue 15280] New: Unable to factor two simple functions into one inout function

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Nov 2 15:56:20 PST 2015


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 at puremagic.com
          Reporter: andrei at 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

--


More information about the Digitalmars-d-bugs mailing list