Skip to content

Nim for Java programmers

hlaaftana edited this page Apr 18, 2020 · 11 revisions
DISCLAIMER!

Unofficial, work in progress! This is still a stub. Please help extending it. There may be inaccuracies in this guide. The guide assumes some intermediate knowledge.

The general tutorials can be found here:
http://nim-lang.org/docs/tut1.html
http://nim-lang.org/docs/tut2.html

The manual provides an overview of the language: http://nim-lang.org/docs/manual.html

Comparison

Feature Java Nim
Compilation JVM bytecode C/C++/Obj-C/JS source
Paradigms class-based, object-oriented procedural, compile-time
Metaprogramming reflection, annotations macros, templates, generics
Memory management Garbage collected Multiple strategies: garbage collected, manual, automatic
Blocks Delimited by curly braces Indented like Python, statement list expression
Whitespace significance Only needed to separate keywords from identifiers. Statements are terminated by semicolons Important in separating prefix operators from infix ones, command calls, indentation, etc
Expressions vs statements Method calls returning a value can pass for both expressions and statements if, case, when, block can be expressions if exhaustive, discard statement needed to turn expressions into statements, proc ending with an expression is an implicit return
Operators Predefined set of symbols, 1+-1 easily parsed as 1 + (-1), no overloading Custom operators possible, overloading possible, first character based precedence, subscript overloading possible, spaces needed for some cases
Number types Signed integers of 8, 16, 32 and 64 bits (8 and 16 bits are actually 32 bits in bytecode), floats of 32 and 64 bits Unsigned and signed integers with bitsize in type name e.g. int32, int has platform-dependent size, float is always float64, compatibility types like cint available and use C defined types.
Boolean type boolean, one of true or false, 32 bit integer bool, enum of true or false, 8 bit integer
Char type char, 32-bit integer, treated as unsigned 16-bit integer char, unsigned 8 bit integer, cchar and cuchar, cschar types for compatibilty with C
Enums Implemented as ordered singleton instances of a class 8 or 16 bit enumerations like C, can be used as array indices
Strings Immutable wrapper around char[] Mutable and growable, similar to seq[char], compatible with openarray[char]
Collection types Variable-length arrays in the core language, List, Set, Map interfaces in the standard library array[I, T] where I is a compile time integer or range type, UncheckedArray[T], seq[T], Pascal-style bitset type set[T], slice type with a..b, tuples and named tuples, standard library: tables, sets and more
Typeclasses Interfaces Experimental concepts, intersection/union types, openarray, range
Generics Erased and for reference types (subject to change with Project Valhalla, uses angle brackets like C++ Similar in implementation to C++ templates but uses square brackets like Scala
Variance in generics ? extends T/? super T, generics invariant by default out T/in T, experimental, generics invariant by default
Type aliases None type Foo = Bar
Type inference var type for locals, diamond operator, lambda inference let/const/var don't need type annotation if initialized, routine generics can be inferred from arguments, auto return type. No inference for anonymous procs or object constructor generics. Empty seq (@[]) needs type annotation
Clone this wiki locally