Lifetime tracking
docandrew via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 2 19:28:50 PDT 2016
On Friday, 3 June 2016 at 00:40:09 UTC, Stefan Koch wrote:
> On Friday, 3 June 2016 at 00:31:31 UTC, Walter Bright wrote:
>
>> If they cover the cases that matter, it's good. Rust has the
>> type system annotations you want, but Rust has a reputation
>> for being difficult to write code for.
>
> I think we can incorporate typesafe borrowing without making it
> difficult to write.
+1, a big problem with Rust is just that the syntax is really
ugly to those coming from D/C/C++/Java. An idea I had was using
plain English attributes in function signatures to denote
ownership.
e.g.
void myfunc(sees Myobj arg1, copies Myobj arg2, borrows Myobj
arg3, owns Myobj arg4)
{
//"sees arg1" - read-only reference (basically const now, but
cannot be
//cast away)
//"copies arg2" - read/write copy of argument. It works the same
way
//value types work now, and will be freed after function
exit (unless it is
//returned).
//"borrows arg3" is a by-reference pass, may have the benefit of
enabling
//optimization for small functions since it eliminates a
copy
// (maybe save a stack push and allow register re-use?). Will
not be freed
//after function exits (ownership returns to calling
// function). Reference can be locked for multi-threaded apps.
//"owns arg4" - frees after function exit (unless it is
returned).
}
At a glance it's obvious who owns what, what's read-only, etc.
Also, a nice bonus is that "const" can become a more rigid
guarantee - as in Rust, there can exist multiple const
references to an object, but only one mutable reference.
Immutable or const by default is probably a bridge too far from
what we're used to.
There are still a lot of corner-cases that I'd have to think
through, i.e. calling class methods through a const/"sees"
reference (would have to be "pure" calls only), good syntax for
ownership changes mid-function (maybe use "sees" "copies"
"borrows" and "owns" as operators?), passing to C functions,
mangling, etc.
Anyhow, just some brainstorming to stir discussion. It looks
pleasant to me, but I'm not sure if you can call it "D" anymore.
-Jon
More information about the Digitalmars-d
mailing list