[Top][All Lists]

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

Re: Add support for array .pop()

From: lolilolicon
Subject: Re: Add support for array .pop()
Date: Sat, 15 Oct 2016 19:57:43 +0800

On Sat, Oct 15, 2016 at 7:45 PM, Marco Ippolito <address@hidden> wrote:
> On Sat, Oct 15, 2016 at 05:41:32PM +0800, lolilolicon wrote:
>> pop() {
>>     local -n _a=$1
>>     printf -v "$2" "${_a[-1]}"
>>     unset _a[-1]
>> }
>> declare -a a=(a b c)
>> while ((address@hidden)); do
>>     pop a v
>>     declare -p a v
>> done
> What I particularly like about your `pop()' function is that it works even
> when called from within a function, rather than from the global scope, as you
> do in your snippet.
> Specifically, if we assume that:
> - a is a global array
> - a is also a local array inside function1
> - pop is called from function1
> it still "does the right thing (TM)", when called before and after the local
> declaration.

I think it's because bash uses dynamic scoping.

> Feels like the nameref acts as if it referenced `scope.identifier'.
> I would still like to see similar functionality included in Bash, though.
> Why do you prefer:
>     declare -a a=(a b c)
> to:
>     a=(a b c)
> Guess: "Explicit is better than implicit"? (Zen of Python)

I don't want to think too much about any possible side effects... so I
just write what I mean.

> I see it more as: "Redundant code is... redundant".
> Why the underscore in:
>     local -n _a=$1
> Guess: to visually signal it's a nameref?

To avoid name conflicts. If you use say, a instead of _a, you will get
cyclic reference. That's a big pitfall with nameref (combined with
dynamic scoping), if you're not careful with your variable naming

> Not sure about identifier naming conventions in Bash but that, subjectively,
> seems to allude to something else, like "unused", or "local", etc.
> I am generally not particularly in favour of "Hungarian notation" but,
> perhaps, `a_ref' or `a_nameref' rather than `_a' in your case?

I don't think bash has a universal style standard like Python...

reply via email to

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