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 ]

type conversions

echo int(3.14)      # 3

echo int(3.99)      # 3 , integer part

echo float(2)       # 2.0

let
  a = 2
  pi = 3.14159

echo $a             # "2"

echo $pi            # "3.14159"

In Nim, the $ "operator" can convert anything to a string.

What is this $ operator?

Actually, it's a procedure. Nim allows operators and punctuation symbols to be used as procedure names, so $ is just a regular proc that takes a value and returns a string.

These are equivalent:

echo $42            # "42"

echo $(42)          # "42"

echo `$`(42)        # "42"

The backtick syntax works for any operator in Nim — it's how you call operators as regular functions:

echo 2 + 3          # 5

#echo +(2, 3)       # syntax error

echo `+`(2, 3)      # 5

The standard library defines $ for all built-in types, and you can overload it for your own types.

Default string representation of an object:

type
  Dog = object
    name: string

let dog = Dog(name: "Dogmeat")

echo dog    # (name: "Dogmeat")

With customized string representation:

import std/strformat   # &"Hello {name}!"

type
  Dog = object
    name: string

proc `$`(self: Dog): string =
  &"Dog('{self.name}')"

let dog = Dog(name: "Dogmeat")

echo dog     # Dog('Dogmeat')

echo $dog    # Dog('Dogmeat')

𝥶Here, `$` is the same thing as Java's toString() or Python's __str__() . When you print an object (echo dog), then Nim checks if the object has its own `$` proc. If yes, then that proc will be called. Like in Java or Python.

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, 00:08