• No results found

Nim syntax

In document Nim in Action (Page 42-45)

Getting started

2.1 Nim syntax

The syntax of a programming language is a set of rules that govern the way pro-grams are written in that language. You’ve already had a small taste of Nim’s syntax in the previous chapter.

This chapter covers

Understanding Nim basics

Mastering control flow

Using collection types

Handling exceptions

Defining data types

Most languages share many similarities in terms of syntax. This is especially true for the C family of languages, which happens to also be the most popular—so much so that four of the most popular programming languages are syntactically heavily inspired by C.1 Nim aims to be highly readable, so it often uses keywords instead of punctuation. Because of this, the syntax of Nim differs significantly from the C lan-guage family; instead, much of it is inspired by Python and Pascal.

In this section, I’ll teach you the basics of Nim’s syntax. Learning the syntax is a very important first step, as it teaches you the specific ways in which Nim code should be written.

2.1.1 Keywords

Most programming languages have the notion of a keyword, and Nim is no exception.

A keyword is a word with a special meaning associated with it when it’s used in a specific context. Because of this, you may not use keywords as identifiers in your source code.

STROPPING You can get around this limitation by using stropping. See section 1.2 to learn more.

As of version 0.12.0, Nim has 70 keywords. This may sound like a lot, but you must remember that you won’t be using most of them. Some of them don’t yet have a meaning and are reserved for future versions of the language; others have minor use cases.

The most commonly used keywords allow you to do the following:

Specify conditional branches: if, case, of, and when

Define variables, procedures, and types: var, let, proc, type, and object

Handle runtime errors in your code: try, except, and finally

You’ll learn exactly what these keywords mean and how to use them in the next sec-tions of this chapter. For a full list of keywords, consult the Nim manual, available at http://nim-lang.org/docs/manual.html#lexical-analysis-identifiers-keywords.

2.1.2 Indentation

Many programmers indent their code to make the program’s structure more apparent.

In most programming languages, this isn’t a requirement and serves only as an aid to human readers of the code. In those languages, keywords and punctuation are often used to delimit code blocks. In Nim, just like in Python, the indentation itself is used.

Let’s look at a simple example to demonstrate the difference. The following three code samples written in C, Ruby, and Nim all do the same thing. But note the differ-ent ways in which code blocks are delimited.

1 According to the TIOBE Index for December 2016, www.tiobe.com/index.php/content/paperinfo/

tpci/index.html.

if (42 >= 0) {

printf("42 is greater than 0");

}

if 42 >= 0

puts "42 is greater than 0"

end

if 42 >= 0:

echo "42 is greater than 0"

As you can see, C uses curly brackets to delimit a block of code, Ruby uses the keyword end, and Nim uses indentation. Nim also uses the colon character on the line that pre-cedes the start of the indentation. This is required for the if statement and for many others. But as you continue learning about Nim, you’ll see that the colon isn’t required for all statements that start an indented code block.

Note also the use of the semicolon in listing 2.1. This is required at the end of each line in some programming languages (mostly the C family). It tells the compiler where a line of code ends. This means that a single statement can span multiple lines, or multiple statements can be on the same line. In C, you’d achieve both like this:

printf("The output is: %d", 0);

printf("Hello"); printf("World");

In Nim, the semicolon is optional and can be used to write two statements on a single line. Spanning a single statement over multiple lines is a bit more complex—you can only split up a statement after punctuation, and the next line must be indented.

Here’s an example:

echo("Output: ", 5)

echo(5 + 5) echo(5

+ 5) echo(5 + 5)

Because indentation is important in Nim, you need to be consistent in its style. The convention states that all Nim code should be indented by two spaces. The Nim com-piler currently disallows tabs because the inevitable mixing of spaces and tabs can have detrimental effects, especially in a whitespace-significant programming language.

Listing 2.1 C

Listing 2.2 Ruby

Listing 2.3 Nim

Both of these statements are correct because they’ve been split after the punctuation and the next line has been indented.

This statement has been incorrectly split before the punctuation.

This statement has not been correctly indented after the split.

2.1.3 Comments

Comments in code are important because they allow you to add additional meaning to pieces of code. Comments in Nim are written using the hash character (#). Any-thing following it will be a comment until the start of a new line. A multiline comment can be created with #[ and ]#, and code can also be disabled by using when false:.

Here’s an example:

# Single-line comment

#[

Multiline comment ]#

when false:

echo("Commented-out code")

The first of the two types of multiline comment can be used to comment out both text and code, whereas the latter should only be used to comment out code. The compiler will still parse the code and ensure that it’s syntactically valid, but it won’t be included in the resulting program. This is because the compiler checks when statements at com-pile time.

In document Nim in Action (Page 42-45)