NotNull pointers
bearophile
bearophileHUGS at lycos.com
Mon Aug 29 16:52:59 PDT 2011
Walter:
> I've implemented the ability to disable default initialization. This makes it
> practical to implement a library based "NotNull" type without a special syntax
> for it. The rationale for this is (rather than making it builtin)
If it works well enough, a syntax is allowed to come later, as it's happening with tuple unpacking now (dmd pull 341).
I have tried the feature, but what's the way to use it?
import core.stdc.stdio;
struct NotNull(P) if (__traits(compiles, {P p = null;})) {
private P p;
this() @disable;
this(P q) {
assert(q);
p = q;
}
NotNull opAssign(P q) {
assert(q);
p = q;
return this;
}
@property P get() {
return p;
}
alias get this; // force rvalue access
}
struct Foo { int x; }
alias NotNull!(Foo*) NFooP;
void bar(NFooP foop) { // Error: variable test2.bar.foop initializer required for type NotNull!(Foo*)
printf("d\n", foop.x);
}
void main() {
NFooP p = new Foo;
bar(p);
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list