bug-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] make function local


From: Pierre Gaston
Subject: Re: [Help-bash] make function local
Date: Fri, 10 Apr 2015 16:04:26 +0300



On Fri, Apr 10, 2015 at 1:40 AM, Peng Yu <address@hidden> wrote:
On Tue, Apr 7, 2015 at 8:49 PM, Chet Ramey <address@hidden> wrote:
> On 4/7/15 4:13 PM, Peng Yu wrote:
>> Hi, Currently, bash does not support local function. But it might be
>> helpful to add such a feature, so that function can become a first
>> class citizen in the language. Could this be considered in the future?
>
> Is there a particular problem you're trying to solve for which local
> functions would be the appropriate solution?

One could also ask the same question for local variables. Any limited
cases that show local variable is need, by definition, can be changed
to ones in which global variables can also work.

Therefore, no matter why small examples that I should here, it will
always be criticized as can be solved by an alternative solution.

However, the real problem is that global variable make larger programs
difficult to maintain. Therefore, the problem becomes severe for
people using bash for making bash libraries. Any ad hoc use of bash
can always get away from this problem.

http://en.wikipedia.org/wiki/Global_variable

Does this make sense?

Regarding the usage of bash, I don't think it should be necessarily
limited to some restricted usage. It can be used as a full fledged
language, except that it does not handle many basic types as in C, but
it is far better in handle processes and files, and calling external
programs.. There are already some packages (like bash-completion)
developed. With better support from the language, it will make the
process of making packages easier.

If you must ask for an example, here is one. But again, people always
can find some walkaround (in this case rename the second g to g1, say)
for any code snippet as it is short by definition.

function g {
# g is defined in some bash library
# used by sourcing the library
}

function f {
# wants to define some local function so that it is only available in f
# accidentally g is choose
# Then call g in f
}

g #after a while g is called, but now g is the g in f but not the original g.

--
Regards,
Peng

Your example is not a use case but just an example of what you mean by local function.

I think it's fair for Chet to ask about what you want to do with local functions, there are hundreds of feature that can be nice to have, how to choose among these?

Also maybe there are other things he has in mind or other feature that people have requested that would also answer your need.

For instance your concern seems to be larger programs and name clashes, so maybe a way to defined functions local to a particular source file would be helpful to define your library (or the other way round, only expose functions explicitly exported) but maybe you are in fact a fan of object oriented code and would love to be able to add, say, member functions to assoc array etc.. etc...



reply via email to

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