Re: [PATCH] admit 'typeset' is here to stay

2016-02-20 Thread Dan Douglas
On Sat, Feb 20, 2016 at 5:28 PM, Martijn Dekker  wrote:
> Am I missing something? I thought they did exactly the same thing in
> bash. If I'm not wrong about that, then as far as bash is concerned,
> they are in fact synonyms and functionally equivalent.

Yes declare and typeset are identical in bash.

> 'typeset' and 'declare' in bash are both dynamically scoped, aren't
> they? Where is the difference between them?

typeset has more historical baggage. If you're sticking to bash then use
whichever you prefer. though the only reason you'd typically use typeset
is if you're not planning on doing that, in which case you have to care
about the differences.

> 'typeset' in AT ksh93 is statically scoped, but ksh93 is the only
> shell to have statically scoped local variables -- all the other shells
> with 'typeset' (pdksh/mksh, bash, zsh, yash) have it as in ksh88,
> dynamically scoped -- and it doesn't look like this is ever going to
> change, or it would have changed sometime in the 23 years since ksh93
> came out.

It may not change, but dynamic scope can be a handicap in all of
those. Regardless, "local" is reasonably portable to bash and zsh. It's a
default alias to typeset in mksh. Ksh93 v- includes a dynamically-scoped
local (the details of which are somewhat unclear but hopefully that
situation will improve). That leaves old shells like ksh88 that had
dynamically scoped typeset without local. If ksh88 and portability are
that important then you probably don't want to use typeset at all (or
alias it to local and be careful with how it's used, or use typeset if
you know what you're doing).

Scope isn't the only problem. typeset is tied to all of the nonstandard
shell data types. zsh's contiguous integer array type for example that
clobbered `typeset -a` by giving it the same name as ksh/bash sparse
arrays. Even the "emulate" modes don't fix that blunder. Problems like
that are probably a hopeless lost cause. typeset didn't come from bash,
and at least some aspects of it differ wildly between every shell that
has it. (I haven't even gotten into the interaction with "function"
vs "name()" and all the weird scope issues with traps and set options
in {m,}ksh)

>> For the most common requirement: localizing or applying an attribute to
>> a variable within a single function; "local" and the posix-compatible
>> declaration builtins are usually what people want. "local" is supported
>> by every current implementation that also has "typeset" and "declare",
>
> That would be bash and zsh only, so that's not very meaningful.
>
> Perhaps you meant "every current implementation that also has
> 'typeset'", which would be ksh93, pdksh/mksh, bash, zsh, and yash.

yes.

> The latter two have dynamically scoped 'typeset', so on those, you can
> simply alias 'local' to 'typeset' to get the same functionality with the
> familiar keyword.

bingo.

> But on ksh93, getting the equivalent of a dynamically scoped 'local' is
> impossible. It has statically scoped local variables only. See Part III,
> Q28 at http://kornshell.com/doc/faq.html

It's not impossible, there are many workarounds even without the bash
compatibility mode. I have lots of scripts that run in both ksh93 and
bash and make heavy use of indirection and locals. Only complex scripts
are very sensitive to the scope issues to begin with.

Bash and mksh are much better nowadays since all three have namerefs
with compatible syntax. The only big missing feature is references
on positional parameters. For simple scripts that's not necessarily
a problem.



Re: [PATCH] admit 'typeset' is here to stay

2016-02-20 Thread Martijn Dekker
Dan Douglas schreef op 20-02-16 om 21:55:
> "typeset" vs. "declare" isn't always a straightforward decision
> depending on one's requirements. Bash's "typeset" has always
> been there for portability despite being a synonym for "declare",
> so I think just calling it a synonym would be counterproductive.
> http://thread.gmane.org/gmane.comp.standards.posix.austin.general/8371/focus=8377

Am I missing something? I thought they did exactly the same thing in
bash. If I'm not wrong about that, then as far as bash is concerned,
they are in fact synonyms and functionally equivalent.

> It's tricky because the big problems surrounding "typeset" lie in the
> details of variable scope and how each option is supported. There are
> some good reasons for choosing "typeset" if you're willing to work around
> all the subtleties but I think that's a bit beyond a concise description
> for the help output.

'typeset' and 'declare' in bash are both dynamically scoped, aren't
they? Where is the difference between them?

'typeset' in AT ksh93 is statically scoped, but ksh93 is the only
shell to have statically scoped local variables -- all the other shells
with 'typeset' (pdksh/mksh, bash, zsh, yash) have it as in ksh88,
dynamically scoped -- and it doesn't look like this is ever going to
change, or it would have changed sometime in the 23 years since ksh93
came out.

> For the most common requirement: localizing or applying an attribute to
> a variable within a single function; "local" and the posix-compatible
> declaration builtins are usually what people want. "local" is supported
> by every current implementation that also has "typeset" and "declare",

That would be bash and zsh only, so that's not very meaningful.

Perhaps you meant "every current implementation that also has
'typeset'", which would be ksh93, pdksh/mksh, bash, zsh, and yash.

However, ksh93, pdksh/mksh, and yash don't have "local".

The latter two have dynamically scoped 'typeset', so on those, you can
simply alias 'local' to 'typeset' to get the same functionality with the
familiar keyword.

But on ksh93, getting the equivalent of a dynamically scoped 'local' is
impossible. It has statically scoped local variables only. See Part III,
Q28 at http://kornshell.com/doc/faq.html

- M.




Re: [PATCH] admit 'typeset' is here to stay

2016-02-20 Thread Dan Douglas
On Sat, Feb 20, 2016 at 2:55 PM, Dan Douglas  wrote:
> http://thread.gmane.org/gmane.comp.standards.posix.austin.general/8371/focus=8377

I meant to post Chet's reply to my question:
http://article.gmane.org/gmane.comp.standards.posix.austin.general/8482

But that whole thread has some good info.



Re: [PATCH] admit 'typeset' is here to stay

2016-02-20 Thread Dan Douglas
On Thu, Jan 28, 2016 at 4:36 PM, Martijn Dekker  wrote:
> In 'help typeset', the 'typeset' builtin is called obsolete and has been
> so since at least bash 2.05b (2002) or possibly earlier. Perhaps it's
> time to just call it a synonym, as indeed the texinfo documentation does.
>
> diff -ur bash-4.4-beta.orig/builtins/declare.def
> bash-4.4-beta/builtins/declare.def
> --- bash-4.4-beta.orig/builtins/declare.def 2015-08-24
> 19:02:28.0 +0100
> +++ bash-4.4-beta/builtins/declare.def  2016-01-28 22:32:16.0 +
> @@ -65,7 +65,7 @@
>  $SHORT_DOC typeset [-aAfFgilnrtux] [-p] name[=value] ...
>  Set variable values and attributes.
>
> -Obsolete.  See `help declare'.
> +Synonym of `declare'.  See `help declare'.
>  $END
>
>  #include 
>

"typeset" vs. "declare" isn't always a straightforward decision
depending on one's requirements. Bash's "typeset" has always
been there for portability despite being a synonym for "declare",
so I think just calling it a synonym would be counterproductive.
http://thread.gmane.org/gmane.comp.standards.posix.austin.general/8371/focus=8377

It's tricky because the big problems surrounding "typeset" lie in the
details of variable scope and how each option is supported. There are
some good reasons for choosing "typeset" if you're willing to work around
all the subtleties but I think that's a bit beyond a concise description
for the help output.

For the most common requirement: localizing or applying an attribute to
a variable within a single function; "local" and the posix-compatible
declaration builtins are usually what people want. "local" is supported
by every current implementation that also has "typeset" and "declare",
plus a few others like dash (to a limited degree).  Those that are mostly
concerned with bash compatibility usually choose "declare" when not using
"local".



[PATCH] admit 'typeset' is here to stay

2016-01-28 Thread Martijn Dekker
In 'help typeset', the 'typeset' builtin is called obsolete and has been
so since at least bash 2.05b (2002) or possibly earlier. Perhaps it's
time to just call it a synonym, as indeed the texinfo documentation does.

diff -ur bash-4.4-beta.orig/builtins/declare.def
bash-4.4-beta/builtins/declare.def
--- bash-4.4-beta.orig/builtins/declare.def 2015-08-24
19:02:28.0 +0100
+++ bash-4.4-beta/builtins/declare.def  2016-01-28 22:32:16.0 +
@@ -65,7 +65,7 @@
 $SHORT_DOC typeset [-aAfFgilnrtux] [-p] name[=value] ...
 Set variable values and attributes.

-Obsolete.  See `help declare'.
+Synonym of `declare'.  See `help declare'.
 $END

 #include