bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: language inconsistency(wart) & RFE


From: Linda Walsh
Subject: Re: language inconsistency(wart) & RFE
Date: Sat, 17 Oct 2015 08:55:31 -0700
User-agent: Thunderbird



isabella parakiss wrote:

Maybe you can just use this?    alias declare='declare -g'
----
        If I needed a way to declare something global, yes...
But what I am wanting is a way to allow changing the defaults
of the implicit variable creation (which could still be
explicitly declared with "-g" if one wanted their result to be
made global.

        For example, I use some aliases in documenting the
intended usage of vars... especially with respect to integers, arrays and hashes. Declaring a hash by calling
it a 'hash' is somewhat generic, but if I call it a 'map', it
is more clear from a documentation standpoint that I am
trying to use something as a way to map names to a value.
        So I will tend to pre-declare many of my vars, but
some are just an annoyance to predeclare.

        Like readarray -t config <~/.toolrc -- read a config
file into an array, and then parse it for relevant options &&
then throw the "config array" out when done parsing it. Having to "declare" a temporary, _first_, before usage is
a bit backwards...

        Obvious example is using throw-away names as
index or iterator vars.  I believe it to be rare, that
people use "for nam in "${names[@]}"; or
"for ((i=0;i<10;++i));" as a means to declare nam or 'i' as
"global variables.  They are  too meaningless for use as
a global, and much more likely to collide with usage of such
'tmp' names in other functions that may be called in a nested
fashion.
        Allowing the option to default to private state,
would take care of accident collisions where one loop uses 'i',
but calls some util function elsewhere that also uses 'i'
and ending up with strange side-effects.  It removes default
behavior to suppress accidental "spooky actions at a distance",
where one or both functions didn't pre-declare 'i' in a func.

        It seems to me, that pre-declaring a variable draws
attention to it -- which is more wanted if it is a global,
but drawing attention to 'i' in a forloop by pre-declaring
it makes the code more confusing, as you are drawing attention
to a temporary who's value you usually won't care about after
the function has exit'ed.
        Allowing an option to change this default, allows for
more portable, more reliable and less confusing code to be
written, which would seem like a desirable goal.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]