zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: suprise with -=
Date: Tue, 20 Oct 2015 19:55:25 -0700	[thread overview]
Message-ID: <5626FE9D.9020003@eastlink.ca> (raw)
In-Reply-To: <151019172744.ZM558@torch.brasslantern.com>

On 10/19/2015 05:27 PM, Bart Schaefer wrote:
> On Oct 19, 12:34pm, Ray Andrews wrote:
> }
> } [...] declare variable types, which is what I'm trying to do with
> } 'integer' and after that I'd expect both increment and decrement to
> } behave the same way.
>
> The parser doesn't know that "first" is an integer, or even that it's
> a variable that was previously declared.  This is perfectly legal:
>
>      if (( RANDOM % 7 ))
>      then integer first
>      else declare first
>      fi
>      first+=second
Then what does 'integer' or 'typset -i' do?
> (Which is why I'm inclined to say it ought to always be string context
> when not explicitly math context, but it's probably way to late for that.)

Given all these little gotchas, I think you are right.  (()) is math, 
otherwise you're on
thin ice.  Still tho, surely 'integer' should be  a typecast and not 
changeable, tho of
course it's too late to do anything about that.  I can't help but wonder 
what was going
on in the minds of the guys who coded the first shells and decided to 
permit this sort
of anarchy, I'm sure they had their reasons but since the shells are 
written in C, I'd have
expected normal C discipline to set the standard.
> } I'd consider it very rude for any forcible conversions to occur.
> } Better an error, tho again once one has declared an integer one might
> } expect one's operators to behave consistently.
>
> Note:
>
> torch% integer first=1 second=2
> torch% first+=(second)
> torch% echo $first
> 1 second
>
> This has silently forced $first to change into an array because of
> explicit array context; it did not interpret "(second)" as arithmetic
> parens.

Well I guess that's the universal reality of shells, so I just hafta get 
used to it.  Actually I am already more or less used to it.
>
> Also just to mess things up a bit more:
>
> torch% integer first=1 second=2
> torch% third=first+second
> torch% print $third
> first+second
> torch% integer third
> torch% print $third
> 3
>
> If something has a string value and you re-declare it integer, it does
> math on its string value.  Doesn't happen for array to integer.
>
Thanks for the examples, these are so educational.  I'll play with them 
for several days.


  reply	other threads:[~2015-10-21  2:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-19  5:46 Ray Andrews
2015-10-19 18:35 ` Bart Schaefer
2015-10-19 19:34   ` Ray Andrews
2015-10-20  0:27     ` Bart Schaefer
2015-10-21  2:55       ` Ray Andrews [this message]
2015-10-21  3:52         ` Bart Schaefer
2015-10-21 18:01       ` Ray Andrews
2015-10-21 18:43         ` ZyX
2015-10-22 15:29           ` Ray Andrews
2015-10-22 15:43             ` ZyX
2015-10-22 16:02               ` Ray Andrews
2015-10-22 23:56               ` Bart Schaefer
2015-10-23  7:34                 ` Daniel Shahaf
2015-10-21 18:46         ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5626FE9D.9020003@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).