On Wed, May 22, 2013 at 2:51 PM, Irek Szczesniak <[email protected]>
wrote:
> On Wed, May 22, 2013 at 9:31 PM, Dan Douglas <[email protected]> wrote:
>> On Tue, May 21, 2013 at 3:51 AM, Roland Mainz <[email protected]>
wrote:
>>> Hi!
>>>
>>> ----
>>>
>>> Attached (as "astksh20130503_typeset_copy_operator001.diff.txt") is 
>>> a small patch which provides a type-independent copy-by-name operator.
>>
>> Why are typeset options required to move/copy objects?
>
> typeset -m destname=srcname # move/rename variable typeset -c 
> destname=srcname # copy variable
>
>> Don't namerefs
>> on positional parameters create reference variables (`&arg'/`ref arg'
>> from C++/C#) that refer directly to an object right?
>
> I don't grok this. Can you give an example, please?

Sure. A nameref to a positional parameter defined within a function is
different than an ordinary nameref.

     ~ $ ksh -c 'function f { typeset x; g x; print -C x; }; function g {
typeset -n x=$1; typeset -p x; compound x=(x=foo; typeset -a y=(bar baz));
}; f x'
    typeset -n x=x
    (
            x=foo
            typeset -a y=(
                    bar
                    baz
            )
    )
     ~ $ ksh -c 'function f { typeset x; g x; print -C x; }; function g {
eval typeset -n "x=$1"; typeset -p x; compound x=(x=foo; typeset -a y=(bar
baz)); }; f x'
    typeset -n x=x
    unset

typeset -c is unnessesary here because (almost) any operation (except unset,
typeset -p, maybe others) always operate on the ultimate target of the
reference. These are stackable -- one function can refer to multiple
variables of the same name at the same time even with conflicting locals.

    function f {
        case $1 in
        0)
            typeset vars

            # Pass in a vars ptr
            f 1 vars

            # vars returned from 3
            print -C -- vars.{a..d}
            ;;
        1)
            nameref vars=$2
            typeset -a a=(yo jo) b=(bo mo)
            f 2 a b vars
            ;;
        2)
            nameref c=$2 d=$3 vars=$4
            typeset -a a=(foo bar) b=(baz bork)
            f 3 a b c d vars
            ;;
        3)
            nameref a=$2 b=$3 c=$4 d=$5 vars=$6

            # Print the ref vars
            typeset -p a b c d

            # Now follow the refs
            printf '%s=%#B ' "${!a}" a "${!b}" b "${!c}" c "${!d}" d

            # Now assign everything back to scope 0
            # I have NO clue why this variant works and others do not.
            compound vars.a=a vars.b=b vars.c=c vars.d=d
            echo
        esac
    }

    f 0

    # typeset -n a=a
    # typeset -n b=b
    # typeset -n c=a
    # typeset -n d=b
    # a=(foo bar) b=(baz bork) a=(yo jo) b=(bo mo)
    # (foo bar)
    # (baz bork)
    # (yo jo)
    # (bo mo)

>> If not ksh should
>> really have an explicit dereference operator instead.
>
> ksh93 has a dereference operator: ${!varname} returns the name a 
> nameref is referring to.
>

That isn't very useful. It only gives you a name and doesn't even tell you
which instance of the name. It doesn't allow you to operate on the reference
variable itself or decide whether or not to follow the reference, or how
many times to follow it.

>> `typeset -c' conflicts with Bash's (undocumented) "capcase" feature.
>
> 1. Its undocumented in bash(1). It never was and AFAIK never will be 
> because it was an experiment 2. I can't find any scripts which use 
> this feature, and in the light of ksh93's typeset -M it is redundant 
> 3. Its not working properly outside ASCII (you can verify that the 
> generated multibyte characters are garbage through AST wc -X) 4. ksh93 
> is not bash 5. typeset -m and typeset -c is much more useful than 
> another damn make characters uppercase feature which can be 
> implemented much easier through typeset -M, disciplines or other means

Object copy/move is fine, but passing around objects / references should be
possible directly from any expansion or builtin that takes variable names
without invoking typeset. Perhaps ksh forces some of these things so that it
doesn't have to worry as much about garbage collection... I'd have to spend
time looking into it.

In any event, over-cluttering the typeset namespace is just a bad idea. It's
the declaration command with the most portable subset of features.

--
Bah, gmail client.

_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers

Reply via email to