Nim (programming language): Difference between revisions

Content deleted Content added
m Side effects: {{codett|lang=nim|1=}}
m Syntax: split <syntaxhighlight>
Line 137:
* <code>SomeOrdinal</code> {{En dash}} Represents all the basic countable and ordered types, except of non integer number
 
This code sample demonstrates the use of typeclasses in Nim]
<syntaxhighlight lang="nim" line="1">
# Let's declare a function that takes any type of number and displays its double
# In Nim functions with side effect are called "proc"
proc timesTwo(i: SomeNumber) =
echo i * 2
</syntaxhighlight>
 
<syntaxhighlight lang="nim">
# Let's write another function that takes any ordinal type, and returns
# the double of the input in its original type, if it is a number;
Line 154 ⟶ 156:
# multiplied by two, and reconverted to its based type
result = (i.int * 2).T
 
echo twiceIfIsNumber(67) # Passes an int to the function
echo twiceIfIsNumber(67u8) # Passes an uint8
echo twiceIfIsNumber(true) # Passes a bool (Which is also an Ordinal)
</syntaxhighlight>
 
{{codett|lang=nim|echo twiceIfIsNumber(67) # Passes an int to the function}}
echo twiceIfIsNumber{{codett|lang=nim|(67u8) # Passes an uint8 }}
{{codett|lang=nim|echo twiceIfIsNumber(true) # Passes a bool (Which is also an Ordinal)}}
 
=== Influence ===