endless loop with ref and non-ref parameter

Ali Çehreli acehreli at yahoo.com
Thu Jan 24 21:38:42 PST 2013


On 01/24/2013 05:56 PM, Jonathan M Davis wrote:
 > On Thursday, January 24, 2013 17:13:36 Ali Çehreli wrote:

 >> Normally, const vs. immutable parameters are different in the way that
 >> they accept arguments:
 >>
 >> - immutable is "limiting" because it insists that the argument is 
immutable.
 >>
 >> - const is "welcoming" because it accepts mutable, const, and immutable.

 > I've never heard anyone describe it that way before.

We must find easier ways of explaining these semantics. I appreciate the 
examples that you have provided but we can't expect newcomers to extract 
meaning out of this.

We must be able to come up with a few function design guidelines. My 
choice of words like "welcoming" and "limiting" are attempts at finding 
sanity.

 > const is more generic,
 > whereas immutable is more specific. But from the compiler's point of 
view,
 > passing a non-const object to a const function means doing a 
conversion (from
 > mutable to const)

That is news to me. There shouldn't be any conversion from non-const to 
const because that is a conversion that should have no runtime effect. 
As Era Scarecrow put it, it means "this variable cannot be changed by 
me". As I understand it, the compiler should merely enforce that 
guarantee at compile time without performing any conversions.

 >, whereas calling a function of the same type does not
 > require a conversion. I believe that _that_ is the core of why ref takes
 > precedence over const.

If const really requires a conversion then we have a language design 
problem here.

 > So, if you have

[...]

These examples are great but as I said, they don't help the programmer 
at function signature stage.

immutable is easy: If I am going to need immutable data, then I make the 
parameter immutable.

By-value is also understandable: I don't care about the original. 
Additionally, when a 'ref' overload exists, by-value gives me the chance 
to move because only the rvalue can be bound to by-value. Pretty 
indirect way of thinking but we can deal with it...

const should be easy as well: "I am not going to modify". Just like in 
C++... Unfortunately that is not the case.

Ali



More information about the Digitalmars-d-learn mailing list