C++ pattern matching is coming
Timon Gehr
timon.gehr at gmx.ch
Mon Oct 24 23:27:55 UTC 2022
On 10/24/22 20:18, Walter Bright wrote:
> There's a misunderstanding here.
Not really.
> @live *does* work in @safe code,
Yes.
> and confers benefits.
>
Barely.
> I oversimplified things by talking about `free`. Of course, `free()` is
> @system and is not callable from @safe code. But what I am *really*
> talking about is when a pointer is passed as an argument to a function,
> is it being "moved" or "copied"?
>
> If it is "copied", that means the caller retains ownership of the
> pointer. If it is "moved", the caller transfers ownership to the callee.
> What the callee does with the pointer that was transferred to it is not
> relevant to the caller.
>
> So,
>
> 1. void foo(int* p) => argument is moved to foo(), caller can no longer
> use it
>
> 2. void foo(scope int* p) => argument is copied to foo(), caller retains
> ownership
>
>
> If we change the annotations to:
>
> 1. void foo(owner int* p) => argument is moved to foo()
>
> 2. void foo(borrow int* p) => argument is copied to foo()
>
> it means the same thing.
The difference is that now you have annotated an adequate thing.
> This is why dip1000 is foundational to
> implementing an ownership/borrowing system.
>
> move == owner == notscope
>
> copy == borrow == scope
>
> If I was smarter, scope would have been spelled "borrow" :-/
I am aware. I also know a thing or two about move semantics and
borrowing. @live falls short of my expectations in at least two
fundamental ways:
- it is a function annotation (this makes no sense)
- it works on built-in pointers instead of library-defined types
It does not guarantee any of the invariants one usually tries to
establish with an ownership/borrowing system. Hence it is pretty much
useless in @safe code.
More information about the Digitalmars-d
mailing list