Prototype of Ownership/Borrowing System for D
mipri
mipri at minimaltype.com
Wed Nov 20 06:57:55 UTC 2019
On Wednesday, 20 November 2019 at 04:59:37 UTC, Walter Bright
wrote:
> https://github.com/dlang/dmd/pull/10586
>
> It's entirely opt-in by adding the `@live` attribute on a
> function, and the implementation is pretty much contained in
> one module, so it doesn't disrupt the rest of the compiler.
dmd and phobos sure do compile fast! For anyone else:
https://wiki.dlang.org/Starting_as_a_Contributor#Building_from_source
as a bash script:
mkdir ~/dtest
cd ~/dtest
git clone https://github.com/WalterBright/dmd.git
cd dmd
git checkout ob
make -f posix.mak -j8 AUTO_BOOTSTRAP=1
cd ..
git clone https://github.com/dlang/phobos
cd phobos
make -f posix.mak -j8
and now you can compile with
~/dtest/dmd/generated/linux/release/64/dmd
The longest part of this process is the 'git clone' so why not?
No need to -preview= anything.
@live: // doesn't add @live to following functions.
I tried writing some bad code and got some nice errors
that made sense. This is the first error that surprises
me (someone who's never learned Rust or dealt with a
borrow checker):
import std;
import core.stdc.stdlib: malloc;
struct Person {
uint age;
uint karma;
char[0] name;
}
@live void birthday(Person* p) {
p.age++;
} // Error: variable x4.omg.p is left dangling at return
void main() {
auto p = cast(Person*)malloc(Person.sizeof + 10);
birthday(p);
p.name.ptr[0] = 'B';
writeln(p);
}
With linear types in ATS there's a special syntax to
suggest that birthday is giving ownership back to caller
on return. But without that... this of course works:
@live Person* birthday(Person* p) {
p.age++;
return p;
}
...
p = birthday(p);
More information about the Digitalmars-d
mailing list