Persistent list
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Fri Nov 13 15:36:58 PST 2015
On 11/13/15 6:10 PM, Andrei Alexandrescu wrote:
> I created a simple persistent list with reference counting and custom
> allocation at http://dpaste.dzfl.pl/0981640c2835. It's a good
> illustration of a number of issues. In particular, each cast must be
> properly explained.
>
> Here's my exegesis:
>
> * Lines 141-152: I couldn't make tail() work with inout. Generally I'm
> very unhappy about inout. I don't know how to use it. Everything I read
> about it is extremely complicated compared to its power. I wish we
> removed it from the language and replaced it with an understandable idiom.
This seems to work for me:
inout(List) tail() inout
{
assert(root);
auto n = root.next;
incRef(n);
return inout(List)(n, allocator);
}
>
> * Lines 161-185: Same problem: inout.
inout(List) opBinaryRight(string op)(T head) inout
if (op == "~")
{
auto allocator = either(allocator, theAllocator);
import std.conv : emplace;
void[] buf = allocator.allocate(Node.sizeof);
auto n = emplace!(const Node)(buf, head, root, 1);
incRef(root);
return inout(List)(n, allocator);
}
>
> * Lines 191-199: Same problem: inout.
Should work now that inout is valid on both the others
>
> * Lines 208-259: I made inout work for getting the range of the list.
inout not necessary here, you can use const. If inout doesn't apply to
the return value, I believe it devolves to straight const (not sure though).
> Please reply with improvements, ideas, comments, and such. Thanks!
Just note: anything inout is treated like it's const. You can't modify
ANYTHING inside it. And since nothing casts to or from inout, when you
return something that's composed via inout, it has to be done in a
functional way (i.e. constructed and returned).
Here is an updated paste with inout functions:
http://dpaste.dzfl.pl/3fbc786a50c1
Two unrelated notes:
1. I've been writing swift code for the last couple weeks, and goddamn
if it hasn't ruined my ability to type semicolons at the end of lines
2. I thought there was a way to clone dpastes? I had to make a new paste
and then copy all the code.
-Steve
More information about the Digitalmars-d
mailing list