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

Re: suprise with -=

21.10.2015, 21:02, "Ray Andrews" <rayandrews@xxxxxxxxxxx>:
> On 10/19/2015 05:27 PM, Bart Schaefer wrote:
>>  The parser doesn't know that "first" is an integer, or even that it's
>>  a variable that was previously declared.
> That puzzles me. Have not all your examples demonstrated that the
> parser is aware of the type of the variable and that it will perform
> it's operations accordingly?
>     test2 ()
>     {
>         integer first=1 second=2
>         third=first+second
>         print A $third
>         integer third
>         print B $third
>         integer fourth=first+second
>         print C $fourth
>     }
>     A first+second # 'third' is scalar so does the 'wrong' thing.
>     B 0 # 'third' is now known to be integer by the 'print'.
>     C 3 # 'fourth' is integer up front and remembered to
>     be so.
> I see very clearly that the type can change silently or sometimes not
> change at all and just sorta 'do nothing':

How is *parser* related? To make

    integer fourth=first+second

set `fourth` to `3` it is *absolutely* not needed to parse this as

    integer(assign("fourth", plus("first", "second")))

. Just at the time of parsing you parse it as

    integer(assign("fourth", "first+second"))

and at the time of *execution* you evaluate `first+second` as an expression.

>     test1 ()
>     {
>         integer first=1
>         string1=foo
>         first+=string1
>         echo ${(t)first}
>         echo "$first"
>     }
>     integer-local
>     1 # The addition does nothing at all, but no error is thrown.
> ... and "${(t) ...}" is surely the demonstration that types are
> remembered? I'm makinga deep error here, probably.

Remembering types has *absolutely* nothing to do with parsing.
${(t)V} is executed at runtime, after parsing. Neither parser needs to know
what is `V` and whether it is defined.

Basically this happens in every dynamically typed language. (Though most
statically typed language developers do not think that parsing
two constructs differently depending on variable types is not a good idea;
this does not prevent e.g. `foo.a` to be an error due to incorrect `foo` type,
but this is checked after parsing stage and is always read as
something like `getattr("foo", "a")`.)

>>  The first shells didn't have integers or arrays at all. They had only
>>  strings, and a few (external) programs like "expr" that could interpret
>>  strings of digits as numbers.
> Thanks. These 'history lessons' are invaluable (to me, anyway). If
> anything besides strings were never anticipated in the original design
> of shells,then integers would bea 'tack on' and one could see that the
> whole issue of declarations/typing would behandled poorly. The lesson
> is that one must be bloody careful. OTOH, whereas in Cif one wants to
> force a typecast it's a
> labor, whereas in zsh one can do it not onlyeffortlessly, but even
> invisibly.
> Powerful but dangerous.Caveat emptor.

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