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
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

Generics

Let's see some simple examples.

Example 1: short, implicit form

proc my_echo(x: int | float) =
  echo(x)

my_echo(5)         # 5

my_echo(3.14)      # 3.14

#my_echo("hi")     # Error

The type of x can be int or float.

This is also a generic proc, but Nim allows us to write it like this. The generic param is implicit/hidden.

Here is the proof that it's a generic proc:

my_echo[int](5)         # 5

my_echo[float](3.14)    # 3.14

They work.

Example 2: explicit form

proc my_echo[T](x: T) =
  echo(x)

my_echo(5)        # 5

my_echo(3.14)     # 3.14

my_echo("hi")     # hi

Here T is a type parameter — it works for any type.

Example 3: generic proc with type restriction

proc my_echo[T: int | float](x: T) =
  echo(x)

my_echo(5)         # 5

my_echo(3.14)      # 3.14

#my_echo("hi")     # ERROR

This is a generic proc, but restricted to only int or float.

The proc in Example 1 is actually converted to this form by the Nim compiler!

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 May 02, 21:02