Nimメモ
コンパイルコマンド
nim c -r ファイル名
Hello World
コード
echo("helo world!")
コンパイル
nim c -r sample.nim
結果
nim c -r ファイル名
コード
echo("helo world!")
コンパイル
nim c -r sample.nim
結果
変数定義
nimは型言語です。
以下の3つの定義方法があります。
-
var
- 定義後変数の値の書き換えが可能な変数を定義
-
let
- 初期値のみの代入が可能で、その後は変更が不可能な変数を定義
-
const
- 初期値のみの代入が可能で、その後は変更が不可能な変数を定義
-
let
との違いは、コードを書いたときに、すでに値が確定しているものしか代入することができない
サンプル
var
# コマンドラインからの文字の入力
var text = readLine(stdin)
# 文字列の連結
text = text & " concatenation string"
# 結果の表示
echo(text)
↓
入力が「hoge」の場合
hoge concatenation string
が出力される。
let
# コマンドラインからの文字の入力
let text = readLine(stdin)
# 表示
echo(text) # ←コマンドラインからの入力文字列が表示される
以下はだめ。
# コマンドラインからの文字の入力
let text = readLine(stdin)
# 文字列の連結
text = text & " concatenation string" # ←コンパイルエラー
const
const pi = 3.141592
echo(pi) # 3.141592
以下はだめ。
const pi = 3.141592
pi = pi + 3 # ←コンパイルエラー
以下もだめ。
# コマンドラインからの文字の入力
const pi = readLine(stdin) # ←コンパイルエラー
コメント
# 単行コメント
#[
複数行コメント
]#
if文
if 条件式:
処理
例
# コマンドラインからの文字の入力
let text = readLine(stdin)
if text == "hoge":
# 入力文字列が「hoge」の場合
echo("text is hoge")
elif text == "fuga":
# 入力文字列が「fuga」の場合
echo("text is fuga")
else:
# 入力文字列がその他の場合
echo("Sorry I don't know " & text)
loop
for
# 0から100まで表示される
for i in 0..100:
echo(i)
while
var i = 0
while i <= 100:
# 0から100まで表示される
echo(i)
i += 1
カウントアップ、カウントダウン
..
# 単行コメント
#[
複数行コメント
]#
if 条件式:
処理
例
# コマンドラインからの文字の入力
let text = readLine(stdin)
if text == "hoge":
# 入力文字列が「hoge」の場合
echo("text is hoge")
elif text == "fuga":
# 入力文字列が「fuga」の場合
echo("text is fuga")
else:
# 入力文字列がその他の場合
echo("Sorry I don't know " & text)
loop
for
# 0から100まで表示される
for i in 0..100:
echo(i)
while
var i = 0
while i <= 100:
# 0から100まで表示される
echo(i)
i += 1
カウントアップ、カウントダウン
..
# 0から100まで表示される
for i in 0..100:
echo(i)
var i = 0
while i <= 100:
# 0から100まで表示される
echo(i)
i += 1
..
nimには、「..」が使えます。
for i in 0..5:
echo(i) # 0, 1, 2, 3, 4, 5
countup
for i in countup(0, 5):
echo(i) # 0, 1, 2, 3, 4, 5
countdown
for i in countdown(5, 0):
echo(i) # 5, 4, 3, 2, 1, 0
関数
必ず引数の型、戻り値の型を指定しましょう。
引数なし、戻り値なし
proc sample() =
echo("sample function")
sample() # sample function
引数あり、戻り値なし
# proc 関数名(変数名: 型) =
proc sample(text: string) =
echo(text)
sample("test text") # test text
引数なし、戻り値あり
# proc 関数名(): 戻り値の型 =
proc sample(): string =
return "this is sample"
echo(sample())
引数あり、戻り値あり
# proc 関数名(変数名: 型): 戻り値の型 =
proc sample(text: string): string =
return text
echo(sample("hogehoge text")) # hogehoge text
暗黙のreturn
nimには「暗黙のreturn」というものがいます。
その名は「result変数」です。
関数内でresult
という変数に値を入れると、return
を書かなくとも関数の末端まで行くと自動でreturnしてくれます。
proc sample(): string =
# returnしなくても、勝手にreturnされる
result = "hoge"
echo(sample())
受け取りの強制
nimでは関数の戻り値がvoid(なし)でない限り、必ず呼び出し元で戻り値を受け取らなければなりません。
proc sample(): string =
return "hoge"
sample() # ← string型の戻り値があるのに受け取っていないので、コンパイルエラー
どうしても戻り値を受け取りたくない場合はdiscard
を使います。
proc sample(): string =
return "hoge"
discard sample() # ← OK
private関数、public関数
nimにはオブジェクト指向の概念が、なんというか、希薄というかほぼないと言うか…
なんかそんな感じです。
上の「関数」の項目で書いた関数はすべてprivate関数です。
他のファイルからは呼び出せません。
ではどうすれば呼び出せるか。
「*」をつけます
proc sample*(): string =
return "This is public function"
以上。
参考
Author And Source
この問題について(Nimメモ), 我々は、より多くの情報をここで見つけました https://qiita.com/nnahito/items/5ff59103fc88c62720fd著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .