|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
Basic typesTypes that you'll use the most often: let a = 42 # int is int64 (8 bytes) on a 64-bit system pi = 3.14 # float is float64 (8 bytes) on a 64-bit system s = "nim" # string ok = true # bool (1 byte) c = 'x' # char (1 byte) Integer types: int # signed, 64-bit long on a 64-bit system int8 # 8 bits, i.e. 1-byte long int16 int32 int64 uint # unsigned, 64-bit long on a 64-bit system uint8 # 8 bits, i.e. 1-byte long uint16 uint32 uint64 Type info #1: import typetraits # echo n.type.name proc main() = let a = 42 # has type int echo a # 42 echo a.sizeof # 8 (8 bytes in the memory) echo a.type.name # "int" (as string) echo a.low # -9223372036854775808 (smallest possible value) echo a.high # 9223372036854775807 (largest possible value) echo int.low # -9223372036854775808 (works with types too) echo int.high # 9223372036854775807 (works with types too) Type info #2: let a = 42 b = true c = 'A' echo typeof(a) # int echo typeof(b) # bool echo typeof(c) # char let s: string = $typeof(a) echo s # "int" as string Integer literals in various number systems: let decimal = 42 hex = 0xffff octal = 0o644 binary = 0b1111_0101 Type suffixes: nim> 42'i8 42 == type int8 nim> 42'u8 42 == type uint8 nim> 3.14'f32 3.14 == type float32 nim> 3.14 3.14 == type float64 In C, Char typeLike in C, a char is just 1 byte. let a1 = 'A' a2 = '\x41' # also 'A', given in hexa special = 'é' # ERROR, it's 2 bytes in UTF-8 ok = "é" # stored as a string |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |