[Solved] Sophia list type

Is it possible to get an element of a list…ie something like myList(i) or myList[i]
Or do I have to use a mapping ie map(int, element)

The standard library has a function List.nth : (int, list('a)) => option('a). So

include "List.aes"
contract MyContract =
  entrypoint test(myList : list('a), i : int) : 'a =
    let Some(x) = List.nth(i, myList)
    x

(Indexing starts at 0)

Depending on your use case a map might be more suitable though, since indexing into a list is linear in the index.

4 Likes

awesome, tnx! btw what does 'a stand for?

'a is a type variable, which can be instantiated with any type you like. So you can call test with a list(int) or a list(map(string, address)) etc.

1 Like

cool, thanks! thought it was something like that :slight_smile: