On Friday, 24 May 2019 at 10:16:50 UTC, aliak wrote:
Basically, I want this to fail:
auto notNull(T, Args...)(Args args) {
return NotNull!T(new T(args));
}
struct NotNull(T) {
private T _value;
@property ref inout(T) value() inout { return this._value; }
alias value this;
//disable opAssign to null as well
}
class C {}
void func(ref C t) {
t = null;
}
auto a = notNull!C;
func(a); // i want a compile error here
Any ideas that don't involve disabling copying or making the
property non-ref?
Pretty sure that can't be done. On the other hand, why is the
property ref if you're explicitly not going to use its ref-ness?
Alternatively, can you show me how you use its ref-ness?
And just for completeness, you are aware that alias this takes an
overload set, so that this works?
struct NotNull(T) {
private T _value;
@property inout(T) value() inout { return _value; }
@property void value(T val) { _value = val; } // new
alias value this;
// disable opAssign to null as well
}
class C {}
void func(ref C t) { t = null; }
unittest {
NotNull n;
n = new C(); // Look ma, I'm assigning without ref!
func(n); // Does not compile - value() doesn't return by
ref
}
--
Simen