- Goals, history, philosophy
- Basics
- Meta programming
- c2nim
"The reasonable man adapts himself to the world: the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man." -- George Bernard Shaw
- as fast as C
- as expressive as Python
- as extensible as Lisp
- Development started in 2006.
- First bootstrapping succeeded in 2008. - Compiler written in Pascal. - Translated by a tool (pas2nim) to Nim - used pas2nim to produce the first wrappers
- Development started in 2006.
- First bootstrapping succeeded in 2008. - Compiler written in Pascal. - Translated by a tool (pas2nim) to Nim - used pas2nim to produce the first wrappers
- Goals: - leverage meta programming to keep the language small - compile to C - implementation size: 20_000 lines of code - learn from C's, C++'s, Ada's mistakes
"Good Software Takes Ten Years. Get Used To it." -- Joel Spolsky
- current implementation size: about 90_000 lines of code
- language is actually pretty big: generics, concepts, exceptions, procs, templates, macros, methods, inheritance, pointers, effect system, ...
The language borrows heavily from:
- Modula 3: * traced vs untraced pointers
- Delphi
* type safe bit sets (
set of char
) * the parts of the syntax people don't like - Ada * subrange types * distinct type * safe variants / case objects
- C++ * Excessive overloading * Generic programming
Python * indentation based syntax * programming should be fun * the parts of the syntax people do like
Lisp * we really want a macro system * embrace the AST * homoiconicity; everything is a function application
(well, in Nim's case ... not really)
Oberon * the export marker
C# * async / await * lambda macros
- One language to rule them all.
- One language to rule them all.
- "Nim is good at everything".
- web development
- games
- compilers
- operating system development
- scientific computing
- scripting
- command line applications
- UI applications
- And lots more!
- One language to rule them all.
- "Nim is good at everything".
- web development
- games
- compilers
- operating system development
- scientific computing
- scripting
- command line applications
- UI applications
- And lots more!
- Convince programmers with code
Function application is f()
, f(a)
, f(a, b)
.
And here is the sugar:
Sugar | Meaning | Example |
---|---|---|
f a |
f(a) |
spawn log("some message") |
a.f() |
f(a) |
db.fetchRow() |
a.f |
f(a) |
mystring.len |
f a, b |
f(a, b) |
echo "hello ", "world" |
a.f(b) |
f(a, b) |
myarray.map(f) |
a.f b |
f(a, b) |
db.fetchRow 1 |
BUT: f
does not mean f()
; myarray.map(f)
passes f
to map
if thisIsaLongCondition() and
thisIsAnotherLongCondition(1,
2, 3, 4):
x = true
- Rule of thumb: optional indentation after operators,
(
and,
if
,case
etc also available as expressions
- strict and statically typed
- type system weakened for the meta-programming
- value based datatypes (like in C++)
- subtyping via single inheritance (
object of RootObj
) - subtyping via
range
:type Natural = range[0..high(int)]
- generics (
HashSet[string]
) - "concepts": constraints for generic types
- no interfaces, use (tuple of) closures instead
- no Hindley-Milner type inference, Nim embraces overloading
- limited amount of flow typing
- model effects as tuples
(T, E)
rather thanE[T]
- every effect is inferred
- tracks side effects
- tracks exceptions
- tracks "tags": ReadIOEffect, WriteIoEffect, TimeEffect, ReadDirEffect, ExecIOEffect
- tracks locking levels; deadlock prevention at compile-time
- tracks "GC safety"
array[FixedSize, T]
* fixed size in Nim * value based datatypes * layout is compatible to C * create via[1, 2, 3]
constructionseq[T]
* dynamically resizable at runtime * grow withadd
, resize withsetLen
* create via@
ornewSeq
:@[1, 2, 3]
* allocated on the heap and GC'edopenArray[T]
* allows to passseq
orarray
to a routine * internally a (pointer, length) pair
tuple
- value based datatypes
- structural typing
- optional field names
- construct with
()
object
- value based datatypes
ref object
an idiom to get reference semantics out of objects
type Callback = proc (a, b: string) {.closure.}
* functions are first class in Nim * "calling convention" affects type compatibility *closure
is a special calling convention (closures are GC'ed)
- ::
- nim c -r --taintMode:on taintmode_ex
- ::
- nim c -r --taintMode:on taintmode_ex
proc
iterator
template
macro
method
converter
- (
func
)
Rewritten to:
test "tactors"
test "tactors2"
test "threadex"
-->
test "tactors", "tactors2", "threadex"
- ::
- git clone https://github.com/Araq/NimCon2015 nim c -r build.nim
2 options
- via
dynlib
- via
header
type
GtkWidget {.importc: "GtkWidget_t", header: "<gtk.h>".} = object
data {.importc: "Data".}: cint
binary {.importc: "Binary".}: cfloat
compatible: char
proc gtk_image_new(): ptr GtkWidget
{.cdecl, header: "<gtk.h>", importc.}
{.passC: staticExec("pkg-config --cflags gtk").}
{.passL: staticExec("pkg-config --libs gtk").}
proc printf(formatstr: cstring)
{.header: "<stdio.h>", importc: "printf", varargs.}
printf("%s%s", "Nim strings ", "converted to cstring for you")
C type | Nim type |
---|---|
int |
cint |
unsigned long |
culong |
float |
cfloat |
int x[4] |
array[4, cint] |
int* |
ptr int |
char* |
cstring |
char** |
cstringArray = ptr array [0..ArrayDummySize, cstring] |
For example:
Produces:
Website | http://nim-lang.org |
Mailing list | http://www.freelists.org/list/nim-dev |
Forum | http://forum.nim-lang.org |
Github | https://github.com/nim-lang/Nim |
IRC | irc.freenode.net/nim |