"Andrei Alexandrescu" <seewebsiteforem...@erdani.org> wrote in message news:gmo8hl$168...@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.