Nimã®ããããæç®ç´¹ä»
Nimã®ããããæç®ã®ç´¹ä»ã§ããæ¥æ¬èªæ å ±ãå°ãªãããè±èªã®ãã®ãå¤ãã«ãªãã¾ãã
What is special about Nim? (Nimã®ç¹å¥ãªã¨ããã¯ä½ã)
URL: https://hookrace.net/blog/what-is-special-about-nim (è±èª)
Nimã®ããã¨ãããæããè¨äºã§ããã²ãããã«Nimãè¤ãã¾ããã¨ããè¨äºã«ãªã£ã¦ãã¾ãã
Nim1.0ã3ãæ以å
ã«ãªãªã¼ã¹ãããã¨æ¸ãã¦ããã¾ãããã¾ã 0.15.2ãªã®ã¯æ³¨æã§ãã(ã¯ãã1.0åºã¦ã»ããã»ã»ã»)
SDL2ã使ããNimã§2Dã®ãã©ãããã©ã¼ã ã»ã²ã¼ã ãæ¸ã
URL: http://postd.cc/writing-a-2d-platform-game-in-nim-with-sdl2/ (è±èªããæ¥æ¬èªã¸ã®ç¿»è¨³)
åæ: https://hookrace.net/blog/writing-a-2d-platform-game-in-nim-with-sdl2/
ä¸è¨ã® What is special about Nim? ã¨åãæ¹ãæ¸ãããè¨äºã§ãããã¡ãã¯POSTDãããæ¥æ¬èªã«ç¿»è¨³ããã¦ããããã§ãã
SDLã¨ããæå°éã®ã¡ãã£ã¢ã©ã¤ãã©ãªã使ã£ã¦ã²ã¼ã ãä½ãã¨ããè¨äºã§ãã
Nimã§ã®åºæ¬çãªæ¸ãæ¹ããçºå±ã«å ããé
å¸åãã®ãã¤ããªããã«ãããã¨ããå®è·µçãªè©±é¡ãæ±ã£ã¦ãã網ç¾
çãªè¨äºãªã®ã§ç¹ã«ããããã§ããæ®éã®ã²ã¼ã ããã°ã©ãã³ã°å
¥éã¨ãã¦ã®åºæ¥ãç´ æ´ãããã®ãããã§ããã
Nim by Example (OOP Macro)
URL: https://nim-by-example.github.io/oop_macro/ (è±èª)
Nimã®ãã¯ãã使ããC++風ã®classæ§æãå®ç¾ããã¨ããé»éè¡é¢ç½ãè¨äºã«ãªã£ã¦ãã¾ããNimã®ãã¯ãã®å
¥éã¨ãã¦ãããããã§ãã
Go-lang like interface
URL: http://forum.nim-lang.org/t/2422#14994 (è±èª)
ãã¡ãã¯ãã©ã¼ã©ã ã®ãã¹ãã«ãªãã¾ããå
容ã¯ãNimã«ã¯interfaceç¡ããã©ãã¯ã使ã£ã¦Goè¨èªã¿ãããªinterfaceä½ã£ããï¼çãªè¨äºã§ãã
Nimã¯æ¨æºã§è¨èªæ©è½ãå
å®ãã¦ããã®ã§å®éã«å©ç¨ããæ©ä¼ã¯å°ãªãããããã¾ããããNimã®å¼·åããåããã¾ããã
Nim binary size from 160 KB to 150 Bytes (Nimã®ãã¤ããªãµã¤ãºã160KBãã150Bã«ãã)
(追è¨å)
URL: https://hookrace.net/blog/nim-binary-size/ï¼è±èªï¼
Nimã®ãã¤ããªãµã¤ãºãLinuxä¸ã§æå°éã«ããã¨ããè¨äºã§ãã
ã¾ã¨ã
以ä¸ãç¾å¨ããããã®è¨äºã§ããä»ã«ãé¢ç½ãè¨äºãçºè¦ããã追è¨ãããã¨æãã¾ãã
Nimã®å°å ¥
以åã®ããã°ã«æããNimã®è¨äºãNim å
¥é
ã§æ¤ç´¢ããã¨ãããã«ããã¨ããæ³å®å¤ã®äºæ
ã«ãªããããã«Nimã®ãã¼ã¸ã§ã³ãé²ã¿è¨äºã®å
容ãå¤ããªã£ã¦ãã¾ã£ãã®ã§ãããã¯ãã¡ãã¨æ¸ãç´ããªãã¨ãããªãã¨æãããã¡ãã®ããã°ã§å°å
¥è¨äºãæ¸ãç´ããã¨ã«ãã¾ããã
ãã®è¨äºã¯Nimã®ãã¼ã¸ã§ã³0.15.2æç¹ã§ã®è¨äºã§ãããããããã°ããçµã¦ã°ã¾ãå¤ããªãã¨æãã¾ãããã®æã¯ã¾ãè¨äºãæ´æ°ãããã¨æãã¾ãã
ãã®è¨äºã§ã¯Windowsãæ³å®ãã¦ãã¾ããLinuxåãã®è¨äºã¯ã¾ããããæ¸ãããã¨æãã¾ãã
å¿ è¦ãªãã®
Nimã¯ã³ã³ãã¤ã«ã®éãåºæ¬çã«Cãéãã¦ãã¤ããªã«ã³ã³ãã¤ã«ããã®ã§ãå¿
ç¶çã«Cã®ã³ã³ãã¤ã©ãå¿
è¦ã«ãªãã¾ãã
Nimã§ä½¿ãCã³ã³ãã¤ã©ã¯æ§ã
ãªãã®ããµãã¼ãããã¦ãããä¸è¨ã®ãã®ãå
¬å¼ã«ãµãã¼ãããã¦ããããã§ãã
- vcc (Microsoft Visual C++)
- gcc (GNU C Compiler)
- llvm_gcc (LLVM-GCC Compiler)
- icc (Intel C++ Compiler)
- clang (Clang Compiler)
- ucc (Generic UNIX C Compiler)
ä»ã®ãã®ã使ããå ´åãããããã§ãããããããéæ¨å¥¨ã«ãªãã§ãããã
ããã§ã¯ä¸çªç¡é£ãªGCCãå°å
¥ãã¾ãã
ãã¦ã³ãã¼ã
Nimã³ã³ãã¤ã©
Windowsã®å ´åãNimå
¬å¼ã®downloadãã¼ã¸ã«ããZipsã®32bitã64bitã®ã©ã¡ããã好ã¿ã§é¸ã³ã¾ãããã
ããã§éè¦ãªã®ãå¾è¿°ã®Cã³ã³ãã¤ã©ã¨bitãåããããã¨ã§ãã32bitã®Nimã³ã³ãã¤ã©ã¨64bitã®Cã³ã³ãã¤ã©ãçµã¿åãããã¨åããªãå ´åãããã®ã§ãåããã¦ãã¦ã³ãã¼ããã¾ãããã
ããã§ã¯64bitçãå°å
¥ãã¾ãã
Zipsã®ä¸ã«ã¯Exesãããã¾ããã ããã¯ã¤ã³ã¹ãã¼ã©ã¼å½¢å¼ã§ã¤ã³ã¹ãã¼ã«ãã§ãã åæã«Cã³ã³ãã¤ã©ãAporia IDE(Nimç¨IDE)ãªã©ãã¤ã³ã¹ãã¼ã«ã§ããåªããã®ã ã£ãã®ã§ããã 0.15.0ã§ãã°ãè¦ã¤ãããã¡ã³ããã³ã¹ãå°é£ã«ãªã£ã¦ããããã§ãç¾å¨ä½¿ãã®ã¯éæ¨å¥¨ã«ãªã£ã¦ãã¾ãã ã¨ã¦ã便å©ãªãã®ã ã£ãã®ã§æ¯é復活ãã¦ã»ããã§ããã
Cã³ã³ãã¤ã©
GCCã¯Windowsã®å ´åãããããTDM-GCCãããã¨æãã¾ãã GCCã¯ä»ã«ãmingw-w64ããããã¦ã³ãã¼ãã§ããNimå ¬å¼ã¯ãã¡ããæ¨å¥¨ãã¦ãã¾ãããã©ãããã¦ã³ãã¼ãããã°ããããããã«ããã®ã¨ãMsys2ãCygwinãªã©ä»åå¿ è¦ãªCã³ã³ãã¤ã©ã«å ãã¦ä»ã®ç©ããã¦ã³ãã¼ããã¦ãã¦ãã¾ããã®ãå¤ãã®ã§ãå°å ¥ãããããTDM-GCCã«ãã¾ããã
TDM-GCCã®Downloadãã¼ã¸ã«è¡ããtdm64-gcc-[ãã¼ã¸ã§ã³].exe
ããã¦ã³ãã¼ããã¾ãã
ã¤ã³ã¹ãã¼ã«
Nimã³ã³ãã¤ã©ã¯ãã¦ã³ãã¼ãããzipãã¡ã¤ã«ãä»»æã®ãã£ã¬ã¯ããªã«å±éããå±éãããã£ã¬ã¯ããªã®binãã£ã¬ã¯ããªã«PATHãéãã¾ãããã
TDM-GCCã¯ã¤ã³ã¹ãã¼ã«ã®éã«åºã¦ããé¸æã§MinGW-w64/TDM64ãé¸ãã§ã¤ã³ã¹ãã¼ã«ãã¾ããããããã§ã¯ããã©ã«ãã®ãã£ã¬ã¯ããª(C:/TDM-GCC-64)ã«ã¤ã³ã¹ãã¼ã«ããã¨ããåæã§ããã¾ãã
è¨å®
æå¾ã«è¨å®ã§ãããã®ã¾ã¾ã§ã¯Nimã³ã³ãã¤ã©ã«TDM-GCCã®æ
å ±ãä¼ãã£ã¦ãªãã®ã§ãè¨å®ãã¡ã¤ã«ã«è¿½è¨ãã¾ãã
å±éããNimã³ã³ãã¤ã©ã®ãã£ã¬ã¯ããª(ããã§ã¯nim-0.15.2
)ã®configãã£ã¬ã¯ããªã«ããnim.cfg
ã«
@if windows: gcc.path = r"C:\TDM-GCC-64\bin" @end
ã追è¨ãã¾ãã
TDM-GCCã®ã¤ã³ã¹ãã¼ã«å ´æãå¤ããå ´åã¯åèªgcc.path
ã®å¤æ´ããé¡ããã¾ãã
ããã§ã¤ã³ã¹ãã¼ã«ã¯å®äºã§ãã
使ãæ¹
ã¤ã³ã¹ãã¼ã«ãä¸æããã£ããã®ç¢ºèªã¨ãã¦ã試ãã«ã³ã³ãã¤ã«ãã¦ã¿ã¾ãã
ä¸è¨ã®ã³ã¼ããhelloworld.nim
ã¨ãããã¡ã¤ã«ã«ä¿åãã
# helloworld.nim echo "Hello World!!"
ä¸è¨ã®ã³ãã³ããå®è¡ã§ããã¼ã¯ã¼ã«ãããã°ã©ã ã®ã³ã³ãã¤ã«ã§ãã
nim c helloworld.nim
ãã¨ã¯
helloworld.exe
ã§Hello World!!
ã表示ãããã°ç¢ºèªå®äºã§ãã
ã¾ã¨ã
ãã¼ã¸ã§ã³0.15.2ç¾å¨ã§ã¯çµæ§ã¤ã³ã¹ãã¼ã«ãé¢åããããªã£ã¦ãã¾ã£ã¦ãã¾ããããããå°æ¥ç(1.0ãåºãã¾ã§)ã«ã¯æ¹åãããã¨æãã®ã§æå¾ ãã¦å¾ ã¡ã¾ãããã
è£è¶³
Nimã¯0.15ããwindowsã§ã®ç«¯æ«åºåãUTF-8ã«ãªããWindowsã®ã³ãã³ãããã³ããã§ã¯æ¥æ¬èªã使ã£ãå ´åæååãããããã«ãªã£ã¦ãã¾ãã¾ããã
ä¸æçãªå¯¾çã¨ãã¦ãminttyãUTF-8ã§ä½¿ããªã©ãããããªãã¨æãã¾ãã
ããããã¯Gitãã¤ã³ã¹ãã¼ã«ããéã«ä»ãã¦ããGit Bashãminttyã§ä½¿ããããã§ããããã
ãã¡ãããããæ¹åãã¦ã»ããã§ããã
0.15.0ã§ã¯UTF-8ã«ãªã£ã¦ã¾ãããã0.15.2ããã¯ãã¨ã«æ»ã£ãããã§ãã
ç¾å¨èª¿æ»ä¸ã§ãã
Nimã®ã¢ã¸ã¥ã¼ã«
Nimã¯æ®æ®µã®ããã°ã©ãã³ã°ã«ä½¿ããæ±ç¨ã®ããã°ã©ãã³ã°è¨èªã§ããã
è¨èªã¨ãã¦ã¯ã·ã¹ãã ããã°ã©ãã³ã°ãæèãã¦ããè¨èªã§ãã
ã·ã¹ãã ããã°ã©ãã³ã°è¨èªã¨ãã¦ã¯C/C++ãã¡ã¸ã£ã¼ãªè¨èªã§ãããNimã§ã¯C/C++ã¨éã£ã¦åªç§ãªã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã使ããã¨ãã§ãã¾ãã
ä»åã¯ãããªã¢ã¸ã¥ã¼ã«ã®è©±ã§ãã
åºæ¬
åºæ¬çã«ã¯python風ã®ææ³ã«ãªã£ã¦ãã¾ãã
import
Nimã®ã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã§ä»ã®ã¢ã¸ã¥ã¼ã«ã使ãããã«ããã«ã¯ã
import ã¢ã¸ã¥ã¼ã«å
ã¨æ¸ããã¨ã«ãã£ã¦ä½¿ãã¾ãã
ä¾ãã°ãæ¨æºã©ã¤ãã©ãªã§ãã使ãstrutilsãimportããã«ã¯
import strutils
ã¨ããã°ããã«ä½¿ããã¨ãã§ãã¾ãã
except
importããéã«ä¸é¨ã®é¢æ°ãªã©ãã¤ã³ãã¼ãããªãããã«ããexceptãããã¾ãã
import strutils except `%`, toUpper echo "$1" % "abc".toUpper # compile error!
from
ä»ã«ããç¹å®ã®é¢æ°ã®ã¿ãã¤ã³ãã¼ãããfromæãããã¾ãã
from strutils import `%` echo "$1" % "abc" echo strutils.replace("abc", "a", "z") # ã¢ã¸ã¥ã¼ã«åãå«ãå ´å使ç¨å¯è½
importã¯ã«ã³ãã§åºåããã¨ã«ãã£ã¦1è¡ã§è¤æ°ã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ããããã¨ãã§ãã¾ãã
import strutils, sequtils
ããã«ãasã§å¥åã«ãããã¨ãã§ãã¾ãã
import strutils as su
ã¤ã³ãã¼ãããé¢æ°ãªã©ã¯ããã®ã¾ã¾é¢æ°åã§å¼ã³åºããã¨ãã§ããstrutils.format
ã®ããã«å¼ã³åºããã¨ãã§ãã¾ãã
å¥ã®ã¢ã¸ã¥ã¼ã«å士ã«åãé¢æ°åãå«ã¾ãã¦ããå ´åã¯ãã¢ã¸ã¥ã¼ã«åãã¤ãããã¨ã«ãã£ã¦åºå¥ãããã¨ãã§ãã¾ãã
# modulea.nim proc echoInt*(a: int) = echo a
# moduleb.nim proc echoInt*(a: int) = echo a
import modulea import moduleb echoInt(1) # compile error! modulea.echoInt(1) # works! moduleb.echoInt(1) # works!
Nimã®ã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã§ã¯import ã¢ã¸ã¥ã¼ã«å
ã¨ããéã«ã
- æ¨æºã©ã¤ãã©ãª
- ããã±ã¼ã¸ããã¼ã¸ã£ã®nimbleã«ãã£ã¦ã¤ã³ã¹ãã¼ã«ãããã©ã¤ãã©ãª
- åãã£ã¬ã¯ããª
ããã¤ã³ãã¼ããããã¨ãã¾ãã
ã¤ã³ãã¼ãããéã«ã¯ã¢ã¸ã¥ã¼ã«åã«ç¸å¯¾ãã¹ã使ããã¨ãã§ããç¸å¯¾ãã¹ã®/
ã.
ã«ç½®ãæãããã¨ãã§ãã¾ãã
# modules/foo.nim proc echoInt*(a: int) = echo a
import modules.foo echoInt(1)
Asterisk(public)
ãµã³ãã«ã®é¢æ°ã«ä»ãã¦ããã¢ã¹ã¿ãªã¹ã¯*
ã¯é¢æ°ãå
¬é(public)ããããã«ããææ³ã§ãã
é¢æ°ä»¥å¤ã«ããã¯ããåãªã©ã«ãä»ãããã¨ãã§ãã¾ãã
export
ä»ã¢ã¸ã¥ã¼ã«ã®é¢æ°ãªã©ãå¥ã¢ã¸ã¥ã¼ã«ããå ¬éããexportãããã¾ãã
# objmodule.nim type MyObject* = object
# basemodule.nim import objmodule export objmodule.MyObject proc `$`*(x: MyObject): string = "my object"
# main.nim import basemodule var x: MyObject echo $x
include
includeã¯ä»ã®ãã¡ã¤ã«ããã®ã¾ã¾åãè¾¼ãæã§ããCã®ããªããã»ããµã§ãã#include <***.h>
ã§ããã
include fileA, fileB, fileC
使ãã©ããã¨ãã¦ã¯è¤æ°ã®APIã®wrapperãä½ãã¨ããã³ã³ãã¤ã«æã«ä½¿ç¨ããAPIãåãæ¿ãã§ããããã«ããéã«whenã¨çµã¿åããã¦ä½¿ã£ãããã¾ãã
const audioAPI = "wasapi" when audioAPI == "WASAPI": include wasapi.nim elif audioAPI == "OpenAL": include openal.nim elif audioAPI == "CoreAudio": include coreaudio.nim else: raise newException(Exception, "Unknown Audio API")
nimble
nimbleã¯Nimã®ããã±ã¼ã¸ããã¼ã¸ã£å
¼ãã«ãã·ã¹ãã ã§ãã
C/C++ã¨éããå
¬å¼ã§ç¨æããã¦ãããã®ãªã®ã§ã»ã¨ãã©ã®Nimã©ã¤ãã©ãªãã³ãã³ãä¸çºã§å
¥ãã¨ããã¨ã¦ã便å©ãªãã®ã«ãªã£ã¦ãã¾ãã
$ nimble install csv
ä¸è¨ã®ã³ãã³ãã§csvãã¼ãµãä¸çºã§å
¥ãã¾ãã
å¾ã¯ã
import csv
ãæ¸ãã¦ç´ãã«ä½¿ãå§ãããã¨ãã§ãã¾ãã
nimbleã§å
¥ããããã©ã¤ãã©ãªã¨ãã¦ç¹å¾´çãªã®ã¯compilerã©ã¤ãã©ãªã§ãã
compilerã©ã¤ãã©ãªã¯æåéãã³ã³ãã¤ã©ã®ã©ã¤ãã©ãªã§ãããã¯Nimã®ã³ã³ãã¤ã©ã®ã©ã¤ãã©ãªã«ãªã£ã¦ãã¾ãã
Nimã¯ã³ã³ãã¤ã©ãNimèªèº«ã§æ¸ããã¦ããã®ã§(ã»ã«ããã¹ãã£ã³ã°)ããããã¦ã³ã³ãã¤ã©èªèº«ãã©ã¤ãã©ãªã¨ãã¦ä½¿ããããã«ãªã£ã¦ãã¾ãã
ã¾ã¨ã
Nimã®ã¢ã¸ã¥ã¼ã«ã·ã¹ãã ã¯é常ã«åªãã¦ãããããã ãã§C/C++ã«æ¯ã¹ã¦ã¢ããã³ãã¼ã¸ãããã®ã§ã¯ãªãã§ããããã
ç¹ã«compilerã©ã¤ãã©ãªã¯ä»ã®è¨èªã§ã¯ãªããªãç¡ãã©ã¤ãã©ãªã§ãã¨ã¦ãèå³æ·±ãã®ã§ãããåä½ã§è¨äºã«ãããã¨ããã§ãã
ãã¾ã
Nimã§ã¯C/C++ã®ããã«ã³ã³ãã¤ã«æã«ã³ã³ãã¤ã©ã«å¿
è¦ãªãã¡ã¤ã«å
¨ã¦ã渡ãå¿
è¦ã¯ãªããä¸ã¤ã®ãã¡ã¤ã«ã渡ãã°å¾ã¯importããèªåçã«è§£æãã¦ã³ã³ãã¤ã«ãã¦ããã¾ãã
ããã«Nimã§ã¯ã¤ã³ã¯ãªã¡ã³ã¿ã«ã³ã³ãã¤ã«ãã§ããä¸åº¦ã³ã³ãã¤ã«ãã¦ãã¾ãã°ãã¨ã¯æ´æ°ããã£ãé¨åã®ã¿ãåã³ã³ãã¤ã«ãã¦ãããã®ã§2åç®ä»¥éã®ã³ã³ãã¤ã«ã¯é常ã«é«éã§ãã
1åç®ã®ã³ã³ãã¤ã«ã¯æ¨æºã©ã¤ãã©ãªãå«ãã¦ã³ã³ãã¤ã«ãããã®ã§æéããããã¾ããã2åç®ä»¥éã¯é«éåãããã®ã§å®ç¨ä¸åé¡ã«ãªããã¨ã¯å°ãªãã§ãããã
Nimã§ã¯ãã¼ããããã§ã¤ã³ã¯ãªã¡ã³ã¿ã«ã³ã³ãã¤ã«ã®å¤§å¹
ãªæ¹åãäºå®ããã¦ããã®ã§ããã«é«éã«ãªããã¨ãäºæ³ãããã®ã§æå¾
ã§ããï¼
Nimã®é¢æ°ã«ã¤ãã¦
Nimã§ã¯é¢æ°ã®ãªã¼ãã¼ãã¼ããã§ãã¾ããNimã§ã¯ç¬èªã®æ¼ç®åãå®ç¾©ã§ãããªã¼ãã¼ãã¼ããã§ããã®ã§ãæ¼ç®åãæ´»ç¨ããã©ã¤ãã©ãªãªã©ãä½ãã¾ããï¼ä¾ï¼ãã¼ãµã³ã³ããã¼ã¿ãªã©ï¼
ããã ãã§ãªããmethod call syntaxããããã¨ã«ãã£ã¦ãç¬èªã®ã©ã¤ãã©ãªã§ãNimãããè¦ãç®ã§ä½¿ããã¨ãã§ããã®ã§ãã¨ã¦ãèªã¿ãããã½ã¼ã¹ã³ã¼ãã«ãªãã¾ããï¼Nimã¯é度ãçç£æ§ã ãã§ãªãèªã¿ããããéè¦ãã¦ãã¾ãï¼
以ä¸ã¯method call syntaxã®ä¾ã§ãã
# 両æ¹å ±åãæå³ writeLine(stdout, "Hello!!") stdout.writeLine("Hello!!")
æ¼ç®å
Nimã§æ¼ç®åã¯æ®éã®é¢æ°å®ç¾©ã§æ¼ç®åãããã¯ã¯ã©ã¼ãã§å²ããã¨ã«ãã£ã¦å®ç¾©ã§ãã¾ãã
type MyInt = object value: int proc newMyInt(value: int): MyInt = return MyInt(value: value) proc `+`(left, right: MyInt): MyInt = return newMyInt(left.value + right.value) proc `$`(myint: MyInt): string = return $myint.value echo newMyInt(100) + newMyInt(50)
ä¸è¨ã®ã³ã¼ãã¯èªä½ã®åã§ããMyIntã«æ°ããæ¼ç®ãå®ç¾©ãã¦ããã³ã¼ãã§ãã
ä¾
method call syntaxãæ¼ç®åãªã¼ãã¼ãã¼ããããã¦ååã®distinctã使ãã¨æ°ããåä½ãå®ç¾©ãããããªãã¨ãã§ãã¾ãã
type Ether = distinct int proc `+`(left, right: Ether): Ether {.borrow.} # borrowã¯å ã®é¢æ°ãåãã¦ããä»å æ å ±(ãã©ã°ã) proc `$`(ether: Ether): string {.borrow.} echo 50.Ether + 600.Ether
ä¸è¨ã¯æ°ããåä½ã¨ã¼ãã«ãå®ç¾©ãã¦ããæ§åã§ããï¼ã²ã¼ã ã¨ãã«ä½¿ãã¨é¢ç½ããã§ããï¼
ã¾ã¨ã
以ä¸ãNimã®é¢æ°ã«ã¤ãã¦ã§ããNimã®é¢æ°ã¯DSLçã«ä½¿ããã¨ãã§ããã¨ã¦ãèªã¿ãããã¨æãã¾ãã
DSLã«ã¤ãã¦ã¯Nimã®éè¦ãªæ©è½ã§ããtemplateãmacroã¨çµã¿åããã¦ç価ãçºæ®ãããã®ãªã®ã§ãããã«ã¤ãã¦ããããè¨äºãæ¸ãããã¨æãã¾ãã
åã®å®ç¾©
Nimã®åå®ç¾©ã«ã¤ãã¦ã§ãã
Nimã«ã¯æ§ã
ãªåã®å®ç¾©æ¹æ³ãããã¾ãã
åå®ç¾©ã®ç¨®é¡ã¨ãã¦ã
- enum
- object
- ref object
- ptr object
- å¥å
- distinct
ãããã¾ãã
ä»ã«ãåã®ç¹æ§ãå¤ãããã®ã¨ãã¦ãnot nil
ãããã¾ãã
å®ç¾©æ¹æ³
åå®ç¾©ã®æ§æã¯ã
type åå = åå®ç¾©ã®ç¨®é¡
ã§ãã
åå®ç¾©ã®ç¨®é¡ã«ä¸è¨ã®enumãªã©ãè¨è¿°ãã¾ãã
enum
enumã¯åæåã§ãã
Nimã®enumã¯Cè¨èªã®enumã¨ä¼¼ã¦ãã¾ãã
type Direction = enum North East South West echo North
ããããCè¨èªã¨éãã®ã¯ããããã«å¤ãå²ãå½ã¦ããã¨ãã§ãããã¨ã§ãã
type Direction = enum North = "N" Eest = "E" South = "S" West = "W" echo North
{.pure.}
ãã¤ãããã¨ã«ãã£ã¦ãã©ã®åæåã«å±ãã®ããè¨è¿°ããã®ãå¼·å¶ããããã¨ãã§ãã¾ãã
type Direction {.pure.} = enum North East South West echo North # error! echo Direction.North # works!
object
objectã¯ããããæ§é ä½ã§ãããã©ã«ãã§ã¹ã¿ãã¯å²å½ããã¾ãã
type GameObject = object x: int y: int var obj = GameObject(x: 1, y: 5) echo obj
ref object
ref objectã¯ãã¼ãå²å½ãããæ§é ä½ã§ãã
type GameObject = ref object x: int y: int var obj = GameObject(x: 1, y: 5) echo obj.x, ":", obj.y
refã¨objectã¯å¥ã®ãã¼ã¯ã¼ããªã®ã§ãæ¢åã®åã®refçãä½ããã¨ãã§ãã¾ãã
type GameObject = object x: int y: int type PGameObject = ref GameObject var pobj = PGameObject(x: 1, y: 5) echo pobj.x, ":", pobj.y
objectåã¨refåã®ä¸¡æ¹ã§ä½¿ããé¢æ°ãor
ã使ããã¨ã§ä½ããã¨ãã§ãã¾ãã
type GameObject = object x: int y: int type PGameObject = ref GameObject proc echoGameObject(obj: GameObject or PGameObject) = echo obj.x, ":", obj.y var obj = GameObject(x: 1, y: 5) var pobj = PGameObject(x: 2, y: 5) echoGameObject(obj) echoGameObject(pobj)
ptr object
ptr objectããã¼ãå²å½ãããæ§é ä½ã§ãããGCã«ãã£ã¦è¿½è·¡ããã¾ããããã£ã¦é常ã¯ref objectã使ããã¨ãæ¨å¥¨ããã¾ããããããå ´åã«ãã£ã¦ã¯ä½ã¬ãã«ã¾ã§å¶å¾¡ãã¦æé©åãããã¨ãæ±ããããã®ã§ããããã£ããã®ãç¨æããã¦ãã¾ãã
type RawGameObject = ptr object x: int y: int var rawobj = cast[RawGameObject](alloc(sizeof(RawGameObject))) rawobj.x = 1 rawobj.y = 5 echo rawobj.x, ":", rawobj.y dealloc(rawobj)
Nimã¯ã·ã¹ãã ããã°ã©ãã³ã°ãæ確ã«æèãã¦ããã®ã§ããããã£ãä½ã¬ãã«æä½ãã§ããããã«ãªã£ã¦ãã¾ãã
ã¸ã§ããªã¯ã¹
type Buffer[T] = ref object data: seq[T] var buf = Buffer[int](data: @[1, 2, 3, 4, 5]) echo buf.data
Nimã®ã¸ã§ããªã¯ã¹ã¯C++ã®ããã«ãã³ãã¬ã¼ãç³»ã®ã¸ã§ããªã¯ã¹ã§ãããã®ããã¨ã©ã¼ã¡ãã»ã¼ã¸ãèªã¿ã«ããã¨ããæ¬ ç¹ãããã®ã§æ³¨æã§ãã
å¥å
åã«å¥åãä»ããæ¹æ³ã§ãããããããã¾ã使ããã¨ã¯ç¡ãã¨æãã¾ããä¸å¿ã
type IntSeq = seq[int] proc echoIntSeq(data: seq[int]) = echo data var a: IntSeq = @[1, 2, 3, 4, 5] var b: seq[int] = @[1, 2, 3, 4, 5] echoIntSeq(a) echoIntSeq(b)
ããã¾ã§å¥åãä»ããã ããªã®ã§ãå ã®åã¨åããã®ã¨ãã¦æ±ããã¨ãã§ãã¾ãã
distinct
distinctã¯æ¢åã®åããæ°ããåãä½ãã¾ãããªã®ã§ãä¸è¨ã®å¥åã¨ã¯éããåããã®ã¨ãã¦æ±ããã¨ã¯ã§ãã¾ããã
type Index = distinct int var a = 1 var b: Index = 2 # error! var b: Index = Index(2) # works!
not nil
not nilã¯ãã®åãnilã«ãªããã¨ãé²ãæ©è½ã§ããå ·ä½çã«ã¯ãobjectãåæåãããã«çæããããã¨ãé²ãã ãããã®ã«ä½¿ãã¾ãã
type GameObject = object x: int y: int type PGameObject = ref GameObject not nil var pobj: PGameObject # error! var pobjinit: PGameObject = PGameObject(x: 1, y: 5) # works!
ã¾ã¨ã
Nimã®åã¯æ§ã ãªå®ç¾©æ¹æ³ãããã¾ãããããåä»ãã¨ãã¦ãæ±ãã¾ãããå ãã®åã¨ãã¦ãæ±ãã¾ãããããæè»ã¨è¨ããä¸è²«æ§ãç¡ãã¨è¨ããã¯äººã«ããã¨æãã¾ãããé¢ç½ãåã®æ±ãæ¹ã ã¨æãã¾ãã
Nimã®ãã¼ã¿å
Nimã®ãã¼ã¿åã«ã¤ãã¦ã§ãã
åºæ¬çãªãã¼ã¿å
Nimã®åºæ¬çãªãã¼ã¿åã¨ãã¦ã¯ã
- int
- int8
- int16
- int32
- int64
- uint
- uint8
- uint16
- uint32
- uint64
- float
- float32
- float64
- bool
- char
- string
- cstring
ãªã©ãããã¾ãã å é ã«uãã¤ãã¦ãããã®ã¯Cã§ããunsignedã§ãæ«å°¾ã®æ°åã¯ãããæ°ã表ãã¦ãã¾ãã
ã³ã³ããå
ããã¦ããããå å«ããã³ã³ããåãããã¾ããNimã®ã³ã³ããåã¯ããããã使ãããããæ¼ç®åãªã¼ãã¼ãã¼ãããããã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ãç¨æããã¦ããã®ã§ã¨ã¦ãæ¸ããããã§ãã
ã³ã³ããåã§ãã使ããã®ã¨ãã¦ã¯ã
- array
- seq
- varargs
- openarray
- Table
ã¡ãªã¿ã«ä¸è¨ã®ä¾ã§ã¯åãããããã®ããå¤æ°ã«åãæ示çã«ä»ãã¦ãããã®ãããã¾ããããã®ç¨åº¦ãªãNimã®ã³ã³ãã¤ã©ã¯åæ¨è«ãã¦ãããã®ã§çç¥å¯è½ã§ãã
array
arrayã¯éçé åã§ãã
var arr: array[5, int] = [1, 2, 3, 4, 5]
seq
seqã¯åçé åã§ãã@ãã¤ãããã¨ã§ä½æå¯è½ã§ãã
var arr: seq[int] = @[1, 2, 3, 4, 5]
varargs
varargsã¯å¯å¤é·å¼æ°ã«ä½¿ãã¾ãã ã¡ãªã¿ã«@ãã¤ãããã¨ã§seqã«å¤æå¯è½ã§ãã
proc echoVariadic(args: varargs[int]) = echo @args echoVariadic(1, 2, 3, 4, 5)
ããã«ãvarargsã§å¯å¤é·å¼æ°ãåãåãéã«ãä»»æã®å¤æå¦çãè¡ããã¨ãã§ãã¾ãã
proc toInt(value: string): int = case value of "1": return 1 of "2": return 2 else: return 0 proc echoVariadic(args: varargs[int, toInt]) = echo @args echoVariadic(1, 2, "1", "2")
openarray
openarrayã¯é
åãæ±ç¨çã«æ±ãåã§ãã
ä¾ãã°ãarrayã¨seq両æ¹ã§ä½¿ããé¢æ°ãä½ã£ããããã®ã«ä½¿ãã¾ãã
proc echoHead(arr: openarray[int]) = echo arr[0] echoHead([1, 2, 3, 4, 5]) echoHead(@[1, 2, 3, 4, 5])
Table
Tableã¯ããã·ã¥ãã¼ãã«ã§ãæ¨æºã©ã¤ãã©ãªtablesãimportãããã¨ã§ä½¿ãã¾ãã
ä»ã«ãé åºãä»ããOrderedTableãªã©ãããã¾ãã
var agetable = initTable[string, int]() agetable["genji"] = 35 agetable["hanzo"] = 38 agetable["mercy"] = 37 echo agetable["genji"]
ãã¤ã³ã¿å
- pointer
- ptr
- ref
pointer
pointerã¯çãã¤ã³ã¿åã§ãCã§ããvoid*ã§ããåºæ¬çã«ã¯Cã¨ã®é£æºã®éã«ä½¿ããã®ã§ãNimã«éããããã°ã©ã ãæ¸ãéã«å¿ è¦ã«ãªããã¨ã¯ç¡ãã¨æãã¾ãã
ptr
ptrã¯åãã¤ããpointerã§ãããããåºæ¬çã«ã¯Cã¨ã®é£æºã®éã«ä½¿ããã¨ãå¤ãã§ãããã
ref
refãåãã¤ããpointerãªã®ã§ãããptrã¨refã¯GCã§ç®¡çããããã©ããã®éããããã¾ãã
ptrã¯GCã§ç®¡çããããrefã¯GCã§ç®¡çããã¾ãã
ã¾ã¨ã
以ä¸ãNimã®ãã¼ã¿åã§ãã使ããã®ã§ãããã ãå¾åã®ãã¤ã³ã¿åã¯ãã¼ã¿å®ç¾©ã®objectãªã©ã¨å¯æ¥ã«é¢ä¿ãã¦ãããã®ã§ãå°ãåããã«ãã説æã«ãªã£ãã¨æãã¾ãããã¼ã¿å®ç¾©ãªã©ã®é¨åã¯ã¾ãå¥ã®è¨äºã§æ¸ãããã¨æãã¾ãã
Nimã®åºæ¬æ§æ
ã¾ããä»ã®è¨èªã«ããããããªåºæ¬çãªæ§æãç´¹ä»ãã¦ããã¾ãã
Hello World
ã¾ãã¯æåã®ä¸æ©ã®HelloWorldã§ãã
echo "Hello World!!"
ããã¯ã
echo("Hello World!!")
ããã¨åãæå³ã§ãã
Nimã§ã¯ãrubyã®ããã«é¢æ°å¼ã³åºãã®å ´åã«æ¬å¼§ãçç¥ãããã¨ãã§ãã¾ããããããå ´åã«ãã£ã¦ã¯æ¬å¼§ãä»ããå¿
è¦ãããã®ã§ããã¯æ³¨æã§ãã
å¤æ°ãå®æ°ãã³ã³ãã¤ã«æå®æ°
ãããããvarï¼å¤æ°ï¼ãletï¼å®æ°ï¼ãconstï¼ã³ã³ãã¤ã«æå®æ°ï¼ã«ãªãã¾ãã
var name = "Hello!!" echo name name = "Rewrite!!" echo name
let name = "Hello!!" echo name
const name = "Hello!!" echo name
å¤ãæ示ã®å ´åãåæ¨è«ãããã¾ããããããå¤ãç¡ã(Nimã®å ´åã¯nil
)å ´åãå宣è¨ãå¿
è¦ã§ãã
var name: string name = "Hello!!"
æ¡ä»¶åå²
æ§æã¨ãã¦ã¯Pythonã®ãããªif elif else
ã§ãã
çå½å¤ã¯true bool
ã§ãã
if true: echo "True!!" else: echo "False!!"
Nimã¯æ§é ã表ç¾ããææ³ã¨ãã¦ãPythonã®ãããªã¤ã³ãã³ãå½¢å¼ãæ¡ç¨ãã¦ãã¾ããåºæ¬çã«ã¯ã¹ãã¼ã¹2ã¤ã§è¡¨ç¾ãã¾ãã
ä»ã«ããcase of
æ§æãããã
var name = "hello" case "hello" of "hello": echo 1 of "world": echo 2 else: echo 3
ã®ããã«æ¸ãã¾ãã
ä»ã«ãwhenãããã¾ãããããã¯ã³ã³ãã¤ã«æåå²ã§ãã
ç¹°ãè¿ãæ§æ
ç¹°ãè¿ãæ§æã¯ãfor while
ãããã¾ãã
for i in 1..5: echo i
var i = 0 while i < 5: echo i i += 1
for whileã®ä¸¡æ¹ã§break continue
ã使ãã¾ãã
..
ã¯ããããRangeã§ãã
é åãã«ã¼ãããéã«ãã
var arr = [1, 2, 3, 4, 5] for val in arr: echo val
ãã®ããã«æãã¾ãã
indexã§åããã¨ãã§ãã¾ãã
var arr = [1, 2, 3, 4, 5] for i in 0..arr.len-1: echo arr[i]
ãããããã®å ´åã«ã¯..<
æ¼ç®åã§ã
var arr = [1, 2, 3, 4, 5] for i in 0..<arr.len: echo arr[i]
ã®ããã«æ¸ããã¨ãã§ãã¾ãã
é¢æ°
é¢æ°ã¯Nimã§ã¯æç¶ãã¨è¨ããã¦ãããæ§æã¨ãã¦ã¯proc
ã«ãªãã¾ãã
proc add5(val: int): int = return val + 5
é¢æ°å®£è¨ã§ã¯åã®çç¥ã¯ã§ãã¾ããã
returnã§æ»ãå¤ãè¿ãã¦ãã¾ãããä»ã«æ»ãå¤ãè¿ãæ¹æ³ã¨ãã¦ãresultã«ä»£å ¥ããã¨ããæ¹æ³ãããã¾ãã
proc add5(val: int): int = result = val + 5
ã¾ã¨ã
以ä¸ãNimã®åºæ¬æ§æã«ãªãã¾ãããããNimã¯ãã®ä»ã®æ©è½ãè±å¯ãªã®ã§ããã¯ã¾ãå¥ã®è¨äºã«æãããã¨æãã¾ãã