D Ranges
Jonathan M Davis
jmdavisProg at gmx.com
Fri Sep 13 23:17:45 PDT 2013
On Friday, September 13, 2013 23:07:03 H. S. Teoh wrote:
> OTOH, I find myself switching to classes just to get the reference
> semantics in other cases, even if I never actually do any inheritance.
> Trying to do reference semantics with structs, while certainly possible,
> is just too error-prone IME. Just a few days ago, I encountered what
> looked like a nasty functionality bug in my program, only to eventually
> discover that it was caused by a missing 'ref' in a function's struct
> parameter, so updates to the struct didn't persist as the code assumed
> it would. I found myself seriously considering using classes instead,
> just for the default ref semantics.
In general, if you want to have structs with reference semantics, it's
probably better to just give them reference semantics by making it so that any
of their members which are value types are on the heap or by putting all of
the struct's guts on the heap. At some point, it becomes debatable as to
whether that's better than using a class, but it does have less overhead.
There's also RefCounted, which does incur the bookkeeping overhead of the
refcounting, but it does make it so that the memory is freed as soon as you
don't need the object anymore.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list