[PYTHON] Start with Windows, not so scary Nim ① It's like hello world.

Introduction

Nowadays, I have to read a lot of code written in Python and C and want to get used to Python-like grammar. Since it's a big deal, I decided to get started with the Nim language, which has a Python-like syntax similar to C.

For those who say what Nim is, I recommend the following, which is written by a python person. http://h-miyako.hatenablog.com/entry/2015/01/13/201846

Try Nim on Windows.

As with Nim, which has a very concise syntax among compiled languages, and minor languages, you can see articles on the web where geeks are silently trying. With all the effort, the publication of "Nim in Action" is approaching (in English), and even though version 1.0 is approaching, I wonder if there is an atmosphere of refusal other than the apt, so I wrote a solid introductory record based on windows. As soon as I decided to try it. I got to hello world in a little over 10 minutes.

Be careful with the installer.

If you go to the Nim download page, you will find an introduction to binaries for Windows at the top. http://nim-lang.org/download.html

Oops, when I download and run it, the familiar setup screen comes up. nimuinst.PNG

However, there are some quirks in this installer, and I end up wasting time. The workaround is described below.

(1) Check all the installation options for the time being.

Nim is a language that is compiled into C (etc.). Unless you already have a compiler installed that can correctly interpret the C code that Nim spits out, you must also install the C compiler to run Nim code. If you want to try Nim for the time being, check out all the installation options.

nimcheck.PNG ... Aporia is an editor for nim. It's a good idea to add it when you get started.

(2) Specify the installation destination by yourself.

Hey, where I wanted to dig in. At least in Japanese version windows, it seems that it will not be installed correctly with the original specified path. (In Japanese environment) Let's install without adding "" at the end of the installation destination.

Setting Example. nim222.PNG

Compile and run. Quite good.

Now, let's run it. Write and compile code with a .nim extension. As a caveat, it is necessary to specify the language to compile. That is, when compiling XXX.nim via C language, write "nim c XXX.nim".

Like this. hellonim.PNG

The code looks like this.


import strutils

type Person = object
  name: string
  age: int

proc greet(p: Person) =
  echo "Hi, I'm ", p.name, "," , p.age, " years old."
  echo "my name is$1、$I'm 2 years old." % [p.name, (p.age).intToStr]
  
let p = Person(name:"Taro", age:18)
p.greet() # or greet(p)

A description such as a minimum typed language is converted into an .exe binary with execution speed comparable to C. It would be nice if it was toString instead of intToStr, but aside from the small-citizen impressions, the python-like concise syntax is still quite good.

Continued (irregular serialization).

The following figure in Chapter 1 of "Nim in Action" is easy to understand as to why C should also be included. nim-py.PNG

It seems that Nim's editor support is steadily progressing, and it seems that the visual studio code is the best. I'm sure that I'm not afraid of the nim language, version 1.0, which will be of great interest to the enthusiasts, such as full-scale macro support, so I'll keep trying it.

After that, I would like some of my big brothers who are pythonic to try to build an ecosystem like python -c-nim.

Recommended Posts

Start with Windows, not so scary Nim ① It's like hello world.
hello world with ctypes
Hello, World with Docker
Hello world with flask
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Python starting with Hello world!
Hello, world! With virtual CAN communication
[Note] Hello world output with python
Hello World! By QPython with Braincrash