What does ref means

Simen Kjaeraas simen.kjaras at gmail.com
Wed Sep 7 12:13:17 PDT 2011


On Wed, 07 Sep 2011 20:50:04 +0200, Johannes Totz <johannes at jo-t.de> wrote:

> On 06/09/2011 12:00, bearophile wrote:
>> malio:
>>
>>> Okay, thanks bearophile. But I currently doesn't exactly understand
>>> what's the difference between "ref" and "const ref"/"immutable
>>> ref". If "ref" is syntactic sugar for pointers only (like your
>>> first example), does it also create a copy of the parameters which
>>> are marked as "ref"? I thought that pointers (and in this context
>>> also "ref") avoid the creation of costly copies?!?
>>
>> "ref" just passes a reference to something, so it doesn't perform
>> copies.
>>
>> "const ref" or "immutable ref" just means that you can't change the
>> value (with the usual semantic differences between const and
>> immutable, that are both transitive).
>
> So if a parameter is immutable (without ref) the compiler could infer a
> ref to avoid copy because it can't be modified?

Theoretically at least. I don't believe such an optimization is actually
performed in current D compilers.


-- 
   Simen


More information about the Digitalmars-d-learn mailing list