Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: += parameter assignments

On Jan 14, 12:45pm, Oliver Kiddle wrote:
} Subject: Re: PATCH: += parameter assignments
} Bart Schaefer wrote:
} >     integer i=4
} >     typeset s=4
} >     i+=5
} >     s+=5
} >     ((i+=5))
} >     ((s+=5))
} >     print $i $s
} >
} > yields
} >
} >     14 50
} What would you have sooner expected - `14 14'?

The point is that I can't think of -anything- reasonable to expect,
except perhaps that in a saner world ((s+=5)) would be an error because
you can't add an integer to a string.  Unfortunately we're beyond hope
of changing that (and I wouldn't really want to anyway).

About the closest thing I can come up with is that all assignments in
(( )) should force the parameter type to change to numeric (integer or
float), but that has its own set of potential gotchas.  Better might
be if += outside of (( )) always converted numerics to strings and then
did an append.

} > However, what I consider to be worse is that:
} >
} >     s=four
} >     ((s+=5))
} >     s+=5
} >     print $s
} >
} > yields
} >
} >     55

(Incidentally, while were on that subject:


gives an error (bad math expression: operator expected at `four'), so why
doesn't that same thing happen when the leading digit is absent?)

} I'm not sure that I've entirely understood your argument here as your
} example doesn't seem particularly confusing to me. The ((s+=5)) results
} in 5 with s remaining a scalar - that was the case before. It remained
} a scalar so the s+=5 results in 55.

The argument is something like this:  It's OK for += to mean "convert to
number, do arithmetic, then convert back to the original type" as long as
it *always* means that.  It's confusing when it means one thing in (( ))
and two or more things (depending on the parameter type) outside (( )).

To have -= be additive for strings and "subtractive" for numeric types
is just compounding the problem.

} The results might not be imediately obvious after a mix of both the
} math and non-math += for scalars but considered on their own I don't
} find them "confusing".

What I don't like is that I can't look at `s+=5' and know what it's going
to do.  At least I know `((s+=5))' will always add 5 numerically, even if
it might convert s to 0 first.

Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   

Messages sorted by: Reverse Date, Date, Thread, Author