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 ]

Create a custom operator (operator overloading)

See https://nim-lang.github.io/Nim/tut1.html#procedures-operators for more info about operators.

  • An operator can be infix (a + b) or prefix (-a).
  • There are NO postfix operators in Nim.

"Apart from a few built-in keyword operators such as and, or, not, operators always consist of these characters: + - * \ / < > = @ $ ~ & % ! ? ^ . |. User-defined operators are allowed. Nothing stops you from defining your own !?+~ operator, but doing so may reduce readability."

To define a new operator, enclose the operator in backticks "`".

func `%`(a, b: int): int =
  a mod b

echo 10 % 3  # 1

𝥶Modulo is called mod in Nim, % won't work. But, if you insist, you can define your own % operator for modulo.

proc `++`(x: var int) =
  inc x

var a = 5

++a

echo a  # 6

𝥶Similarly, there's no ++ operator in Nim to increment the value of an integer. Do you need it? You can create an operator called ++ that does exactly this.


Honestly, I don't think it's a good idea to create your own % (modulo) or ++ (increment) operator to mimic other languages. Learn how to do it in Nim and follow the Nim way.

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 July 29, 15:32