What functions can be called on a shared struct that's implicitly castable to immutable?

Simen Kjærås simen.kjaras at gmail.com
Mon Nov 4 04:09:11 PST 2013


On 04.11.2013 11:28, Maxim Fomin wrote:
> Why should method call invole copy? This breaks lots of assumptions and
> it would be a pretty weird corner case.

The idea was that it's basically an optimization. The type system 
guarantees that there is no difference.


>> (structs in D are defined to have cheap copying, I seem to recall)
>
> Why? There is no restrictions on postblit, so copying may be cheap as
> well as arbitrary costly.

I do not have a reference handy, but Andrei or Walter has stated in the 
past that it is safe to assume that copying is cheap.


>> and call fuzz on that copy - the type system claims that the call to
>> fuzz cannot possibly change the state of the struct on which it is
>> called.
>
> Your problem may be solved pretty simple:
>
> pure fuzz(S s)
> {
>
> }
>
> and be happy with s.fuzz() operating and copy if you want so. Does it
> address your problem?

Not really. My specific problem is with std.math.abs and shared BigInt. 
Bug 11188 explains how it is not possible to call abs((shared 
BigInt).init). The reason for that is that the operator overloads are 
not marked shared. They are however both const and pure. As we know, 
operator overloads may not be defined outside the aggregate type.

--
   Simen


More information about the Digitalmars-d mailing list