The "no gc" crowd
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Oct 10 11:56:22 PDT 2013
On Thu, Oct 10, 2013 at 08:39:52PM +0200, Joseph Rushton Wakeling wrote:
> On 10/10/13 20:28, Sean Kelly wrote:
> >Isn't BigInt a struct? I'd expect it to work via copying just like
> >concrete types.
>
> Yes, it's a struct, but somewhere inside its internals I think it
> contains arrays. I'm not sure how that affects copying etc., but
> suffice to say that if you try the following:
>
> BigInt a = 2;
> BigInt b = a;
> b = 3;
> assert(a != b);
> assert(a !is b);
>
> ... then it passes. So it behaves at least in this extent like a
> value type.
I took a glance over the BigInt code, and it appears to have some kind
of copy-on-write semantics. For example, in your code above, when you
wrote b=a, b actually *aliases* a, but when you assign 3 to b, a new
data array is created and b is updated to point to the new data instead.
So it's not really a true value type, but more like a COW reference
type.
> But suffice to say that it was an unpleasant surprise that I
> couldn't just take it and pass to a function accepting an
> unqualified BigInt argument.
That only works with true value types, but BigInt isn't really one of
them. :)
T
--
Everybody talks about it, but nobody does anything about it! -- Mark Twain
More information about the Digitalmars-d
mailing list