Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: A way to untie -T vars?



On Sat, Jan 21, 2023 at 9:11 AM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> % typeset -a FOO=($FOO) foo=($foo)

Speaking of unorthodoxy ...

% typeset -T FOO foo
% typeset foo=bar
typeset: foo: inconsistent type for assignment
% typeset -i foo=1
% typeset -p FOO foo
typeset: no such variable: FOO
typeset -i foo=1
%

So ... with "typeset" I can forcibly convert a tied scalar to an
array, or an array to an integer, but there's no way to convert an
array to an ordinary scalar.

Anyway ...

untie () {
  emulate -L zsh
  case ${(tP)1} in
  (scalar-tied)
    local scalar=${(P)1}
    unset $1
    typeset -g $1=$scalar
    ;;
  (array-tied)
    local array=( ${(P)1} )
    unset $1
    # Need eval here for array assignment, as
    # reserved word doesn't work with $1=(...)
    eval "typeset -ga $1=( \$array )"
    ;;
  (*) ;;
  esac
}




Messages sorted by: Reverse Date, Date, Thread, Author