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: Peng Yu
Subject: Re: [Help-bash] make function local
Date: Fri, 10 Apr 2015 08:54:22 -0500

On Fri, Apr 10, 2015 at 8:04 AM, Pierre Gaston <address@hidden> wrote:
>
>
> 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,

Name these 100 nice features, please. I'd doubt you ever can find 100
nice features at the language level. What you are referring are
probably just at the API level. See the good part of javascript URLs
below. There are only a handful of features that are nice for a
language.

> how to
> choose among these?

Easy. Look at what make other language extremely successful and use
it. Local function is more of the immediate next thing that can be
added without incurring too abrupt changes to bash which will give
people time to adapt.

> 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...

You don't know functional programming, do you? That example is
functional program (not full fledged though) but not OO.

It seems that you don't know its importance, either? With functional
programming, you basically can make OO on top of it, but not the other
way around. R has examples on how to make OO using functional
programing.

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

There a few key features a good programming language should have but no more.

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742
http://jstherightway.org/

function as first class citizen is just one of the feature that makes
javascript so successful now days.

-- 
Regards,
Peng



reply via email to

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