Segment violation (was Re: Why I could not cast string to int?)

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 2 15:20:49 PST 2012


On Thursday, February 02, 2012 18:14:25 bearophile wrote:
> xancorreu:
> > But you "only" put a "in" in
> > recFactorial function argument. What this mean? **Why** this is more
> > efficient than mine?
> 
> It wasn't meant to improve performance. "in" turns a function argument to
> "input only" (and eventually scoped too). Generally when you program in D2
> it's a good practice to use immutability where you can and where this
> doesn't cause other performance or typing problems. Immutability avoids
> bugs, allows a stronger purity (and I have seen DMD is often able to
> compiler a little more efficient program if you use immutability/constants
> everywhere they are a good fit). So 95% of the arguments of your program
> are better tagged with "in".

in is pointless on value types. All it does is make the function parameter 
const, which really doesn't do much for you, and in some instances, is really 
annoying. Personally, I see no point in using in unless the parameter is a 
reference type, and even then, it's often a bad idea with reference types, 
because in is really const scope, and the scope is problematic if you want to 
return anything from that variable. It's particularly problematic with arrays, 
since it's frequently desirable to return slices of them, and scope (and 
therefore in) would prevent that. It's useful in some instances (particularly 
with delegates), but I'd use in _very_ sparingly. It's almost always more 
trouble than it's worth IMHO.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list