I’m new to Fish and really liking it so far but the list type is really confusing me.

Something that I find really non-intuitive about fish is the list data type.

You can set a list like this

set my_list a b c
echo my_list[1] #a

But if you try this it doesnt work:

function mklist
  echo a b c
end
set lst (mklist)
echo $lst[1] # a b c

Putting the echo in quotes doesnt work either.

You can do:

function mklist
    echo a b c
end
set lst (string split " " -- (mklist))
echo $lst[1] # a

But needing to always split your return values is kinda terrible.

So it seems like what fish expects you to do is echo multiple lines.

function mklist
    echo a
    echo b 
    echo c
end
set lst (mklist)
echo $lst[1] # a

Its just very weird to me that it doesnt understand a comma delimited string to be a list.

I feel like I must be missing something.


Edit: FWIW I think I get why they designed it like this after thinking some more. It just feels weird.

This requires you to be explicit about returning lists. Otherwise any echo with spaces would be treated as a list and not a single value

  • TechnoCat@piefed.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    4 days ago

    Alright, reading into it a bit more they are not just special strings. Somehow fish marks those variables as special list values. But as soon as you echo them they get concatenated with spaces.