RAII pointers
Moritz Maxeiner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 3 12:06:06 PDT 2017
On Saturday, 3 June 2017 at 17:40:32 UTC, Russel Winder wrote:
> Would one be considered more idiomatic D, or is it a
> question of different circumstances different approaches. The
> differences are mainly in construction I believe.
>
Well, the differences I spot are:
- null check in destructor: That's just because I forgot to add
it. If you add `@disable(this)` (disable the default
constructor), all elaborate constructors ensure it is not null,
and no members can set it to null, you might be able to skip the
check, but I may have missed some corner cases, so better be safe.
- factory functions (Stanislav) vs. elaborate constructors (me):
+ If you don't need to be able to construct the object without
arguments, it's a stylistic choice and I consider the elaborate
constructors to be more idiomatic.
+ otherwise (i.e. you need to be able to construct the object
without arguments), you need the factory functions, because
elaborate constructors for structs cannot have zero arguments, as
that would clash with the default constructor that must be
computable at compile time (for the struct's `.init` value)
- inout: You can use that in what I wrote, as well; that's just a
shorthand way to write several functions that do the same thing:
one for `const T`, one for `immutable T`, and one for `T`
More information about the Digitalmars-d-learn
mailing list