|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
GenericsLet's see some simple examples. Example 1: short, implicit formproc my_echo(x: int | float) = echo(x) my_echo(5) # 5 my_echo(3.14) # 3.14 #my_echo("hi") # Error The type of 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 formproc my_echo[T](x: T) = echo(x) my_echo(5) # 5 my_echo(3.14) # 3.14 my_echo("hi") # hi Here Example 3: generic proc with type restrictionproc 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 The proc in Example 1 is actually converted to this form by the Nim compiler! |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |