Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

anonymous tuples

In Python, a tuple is immutable. In Nim, it depends on how you decared it: with var (mutable) or with let (immutable). The size of a tuple is fix.

import typetraits  # echo n.type.name


var t = ("Banana", 2, 'c')

echo t            # ("Banana", 2, 'c')

echo t.type.name  # (string, int, char)  <-- this is its type

echo t[0]         # Banana

t[0] = "Kiwi"
echo t            # ("Kiwi", 2, 'c')

# echo t.len      # ERROR: len doesn't work with tuples

echo tupleLen(typeof(t))    # 3; number of elements

Return multiple values from a function

Like in Python:

func getMovieInfo(): (string, int, float) =
  return ("Total Recall", 1990, 7.5)

let t = getMovieInfo()

echo t        # ("Total Recall", 1990, 7.5)
echo t[1]     # 1990

let (title, year, score) = getMovieInfo()   # !!! on the left side, use parentheses !!!

echo title    # Total Recall
echo year     # 1990
echo score    # 7.5

Gotcha

Nim has a surprising behaviour that can be strange after Python:

var a, b = 3
#[ it's short for:
  var a = 3
  var b = 3
]#
echo a, " ", b    # 3 3

var x, y = f()
#[ short for:
  var x = f()
  var y = f()
  That is: f() is called twice!
]#

let title, year, score = getMovieInfo()
#[ short for:
  let title = getMovieInfo()
  let year = getMovieInfo()
  let score = getMovieInfo()
  Ooops! This is not what we wanted.
]#

If you want to do value unpacking from a tuple, don't forget the parentheses around the variables:

let (title, year, score) = getMovieInfo()   # !!! on the left side, use parentheses !!!

Iterate over the values in a tuple

The len() function doesn't work on tuples (see above). If you just want to iterate over the values in a tuple, you can do this:

let t = (1, "hello", 3.14)

for value in t.fields:
  echo value

Output:

1
hello
3.14

Anonymous tuples are comparable

let
  ta = (2, 8)
  tb = (2, 8)
  tc = (3, 12)

echo ta == tb     # true

echo ta == tc     # false
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 06, 16:39