On Friday, 17 August 2018 at 06:59:48 UTC, Petar Kirov
[ZombineDev] wrote:
On Thursday, 16 August 2018 at 18:10:38 UTC, jmh530 wrote:
On Thursday, 16 August 2018 at 12:25:14 UTC, aliak wrote:
Hi
See: https://optional.dub.pm
I've totally revamped the Optional type and am now quite
happy with. It has a range interface and safe dispatching and
can be used to 1) avoid null dereferencing, 2) have non-null
guarantees, and 3) show clear intent where there may or may
not be a value. It's also @nogc and @safe and mutation of the
original object during safe dispatching works as well.
The readme.md page looks great. You might mention that it
works with @nogc and @safe (I presume Optional and NotNull).
One question: Suppose there was a @weaksafe that has all the
same features @safe except that it relaxed the requirement of
"No taking the address of a local variable or function
parameter." so that you could take the address of a NotNull
variable. Are there any reasons why this would be unsafe? I
imagine NotNull requires some type of run-time checking, but
other than that I don't see why not.
Actually "@weaksafe" already exists in the form of `@safe` +
`-dip1000` - you can take the address of a local variable in
`@safe` code and then you get a `scope`-ed pointer, which
you're not allowed to escape.
Right! There's that. That would most certainly help here.
So the first non-safety part of Optional comes from the dispatch
function. To support mutations, the pattern is basically:
struct Optional(T) {
T value;
auto dispatch() inout {
return inout Dispatcher(&this);
}
}
Now the thing is, the Dispatcher type is actually disabled on
copying, construction and identity assignment. So I wonder if
this can be marked @trusted in theory. Since the address never
escapes from a Dispatcher object.
And the other part is when two addresses are compared (which is
local only):
https://github.com/aliak00/optional/blob/83edfef7130ec02992ec2986611718f80167234d/source/optional/dispatcher.d#L42