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: Dan Douglas
Subject: Re: [Help-bash] make function local
Date: Fri, 10 Apr 2015 22:47:21 -0500

 On Thu, Apr 9, 2015 at 5:40 PM, Peng Yu <address@hidden> wrote:
 > 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.

 The solution is namespaces. ksh93 supports namespace blocks (including nested
 namespaces), though it's still quite buggy.

 Note C doesn't have namespaces either and gets by despite being used as
 a library language to a much greater extent than bash. It's worked around with
 naming conventions. There are many prefix conventions even within the
 C standard library and POSIX for various headers - atomic_*, mtx_*, pthread_*,
 sem_*, etc.

 On Fri, Apr 10, 2015 at 8:54 AM, Peng Yu <address@hidden> wrote:
 > 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.

 Functional programming is a style, not a language feature. Features that
 facilitate it are common in OO langs.

 C# implements first-class functions with closures by translating them in the IL
 to inner classes that maintain the lexical state. I'm pretty sure Java 8 does
 essentially the same with its lambdas. People have been using the inner class
 trick for ages prior to that.

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

 The type of function you're talking about isn't merely a "local function".
 JavaScript doesn't exactly have "local functions", and probably isn't the best
 example because of its numerous ways of defining functions. It also doesn't
 have a separate namespace for functions. No matter how you define them, you're
 likely using sugar that in the end results in a "function object" that's
 accessed through an identifier that's in the same namespace as any other
 variable (which might be local).

 Bash needs several major features before it could even think about supporting
 function objects.

 First it needs yet another type of function (probably with yet another syntax)
 which supports returning a value, and declaration as an expression so it can
 be passed around and treated as a value.

 At the same time it needs to add some sort of type system so that data other
 than strings (objects or functions) can be bound to variables and formal
 parameters.

 ksh93 has the beginnings of an object system which supports user-defined types,
 but functions are still insufficient for doing anything "functional". Some of
 its basic ideas are nice but I have pretty much given up on understanding David
 Korn's design. It's obviously half-baked in its current state and hard to use
 for getting real work done. It's better than nothing I guess.

 Anyway, you can probably do something resembling FP with some combination of
 `typeset -T` wrappers to contain your functions, and `typeset -M` and `-C` to
 move and copy objects around by reference. It's not pretty.

 --
 Dan Douglas



reply via email to

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