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 ]

Subrange types

See this in the manual: https://nim-lang.org/docs/manual.html#types-subrange-types

"A subrange type is a range of values from an ordinal or floating-point type (the base type). To define a subrange type, one must specify its limiting values -- the lowest and highest value of the type."

Examples:

type
  Natural = range[0 .. high(int)]      # as defined in `system`
  Positive = range[1..high(int)]       # as defined in `system`
  Negative = range[low(int) .. -1]     # I missed it from `system`
  #
  PositiveFloat = range[0.0..Inf]

var
  a: Natural
  b: Positive
  c: int

a = 5          # OK
#a = -2        # Error
b = 3          # OK
#b = -10       # Error

c = a          # OK
c = b          # OK

var f: PositiveFloat

f = 3.14       # OK
#f = -2.5      # Error

Range object (1..6) and range types (range[1..6])

In Nim, "range" can mean two different things, and they shouldn't be confused.

type
  DiceRoll = range[1..6]
  #          ^      ^
  #          A      B
  • When you write "1..6", you create a range object. I wrote about it earlier here. It makes a simple object with two fields that define a closed interval. It's just a description of a range; it only does something useful when passed to something that knows how to work with it.
  • range is a generic type to construct range types. It defines a new type whose values are restricted to that interval. It exists at compile time as a type constraint.
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, 10:28