Numerical literals are written as in most other languages. Out inside this with no indentation restrictions. var myVariable : int # a documentation commentĭocumentation comments are tokens they are only allowed at certain places in the input file as they belong to the syntax tree! This feature enables simpler documentation generators. CommentsĬomments start anywhere outside a string or character literal with the hash character #. They are very useful for embedding HTML code templates for example. """ they can span over multiple lines and the \ is not an escape character either. The third and last way to write string literals is long-string literals. In raw literals, the backslash is not an escape character. Special characters are escaped with \: \n means newline, \t means tabulator, etc. String literals are enclosed in double-quotes character literals in single quotes. Let us look at Nim's lexical elements in more detail: like other programming languages Nim consists of (string) literals, identifiers, keywords, comments, operators, and other punctuation marks. These built-ins are declared in the system module which is implicitly imported by any other module. The "hello world" program contains several identifiers that are already known to the compiler: echo, readLine, etc. Note that this is basically the only form of type inference that exists in Nim: it is a good compromise between brevity and readability. Since the compiler knows that readLine returns a string, you can leave out the type in the declaration (this is called local type inference). The var statement declares a new variable named name of type string with the value that is returned by the readLine procedure. String literals are enclosed in double-quotes. Indentation is done with spaces only, tabulators are not allowed. Indentation is Nim's way of grouping statements. Though it should be pretty obvious what the program does, I will explain the syntax: statements which are not indented are executed when the program starts. Otherwise, Nim might be handicapped by checks that are not even available for C. For comparing the performance with unsafe languages like C, use the -d:danger switch in order to get meaningful, comparable results. With -d:release some checks are turned off and optimizations are turned on.įor benchmarking or production code, use the -d:release switch. To compile a release version use:īy default, the Nim compiler generates a large number of runtime checks aiming for your debugging pleasure. Nim compile -run greetings.nim arg1 arg2Ĭommonly used commands and switches have abbreviations, so you can also use: You can give your program command-line arguments by appending them after the filename: With the -run switch Nim executes the file automatically after compilation. Save this code to the file "greetings.nim". # This is a comment echo "What's your name? " var name : string = readLine ( stdin ) echo "Hi, ", name, "!" We start the tour with a modified "hello world" program: The Nim manual - many more examples of the advanced language featuresĪll code examples in this tutorial, as well as the ones found in the rest of Nim's documentation, follow the Nim style guide.Learn Nim in 5 minutes - quick, five-minute introduction to Nim.Nim Basics tutorial - a gentle introduction of the concepts mentioned above.Here are several other resources for learning Nim: This tutorial assumes that you are familiar with basic programming concepts like variables, types, or statements. This document is a tutorial for the programming language Nim. "Der Mensch ist doch ein Augentier - Schöne Dinge wünsch' ich mir."
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |