escaping addresses of ref parameters - not
Nick Sabalausky
a at a.a
Sun Feb 8 20:55:48 PST 2009
"Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message
news:gmo8hl$1687$1 at digitalmars.com...
> ***************
> Rule: ref parameters are PASS-DOWN and RETURN only. No escaping of
> addresses of ref parameters is allowed. If you want to escape the address
> of a ref parameter, use a pointer in the first place.
> ***************
>
> This rule is powerful and leads to an honest style of programming: if you
> plan on escaping some thing's address, you make that clear in the public
> signature. The fix to the idiom above is:
>
> struct Widget
> {
> private Midget * m;
> ...
> this(Midget * mdgt) { enforce(mdgt); m = mdgt; ... }
> }
>
Or something like:
struct Widget
{
private Midget * m;
...
this(NonNullable!(Midget*) mdgt) { m = mdgt; ... }
}
;)
> Widget makeACoolWidget()
> {
> auto coolMidget = new Midget;
> return Widget(coolMidget); // works!
> }
>
> Whaddaya think?
>
Sounds reasonable for the most part. My only concern is that (if I'm
understanding it right) the null-check is moved from compile-time to
run-time.
More information about the Digitalmars-d
mailing list