概要
素早くSwiftを実行できる環境として、Playgroundが有名ですがRubyでいう所のirb的なREPLもあります。
REPLはインタプリタ的に振る舞い、ステートメントの検証、評価、宣言を即時コンパイルして実行するとのこと。
-
swift
で起動、:quitかctl+Dで停止できる。 - 起動中は「:help」等「:」つきでコマンドを叩ける。
- 明示的に変数に代入しないと勝手に$R0のような変数に代入される。
- letへの再代入、unwrapp忘れ等もちゃんとエラーになる。
- import FoundationすればNSUUIDとかも使える。
サンプル
1> "100".toInt()
$R0: Int? = 100
2> var a = 250
a: Int = 250
3> func sum(val1:Int, val2:Int)->Int{
4. return val1 + val2
5. }
6>
7> sum(10, 20)
$R1: Int = 30
8> let result = sum($R0, a)
repl.swift:8:18: error: value of optional type 'Int?' not unwrapped; did you mean to use '!' or '?'?
let result = sum($R0, a)
^
!
8> let result = sum($R0!, a)
9.
result: Int = 350
9> result = 500
repl.swift:9:8: error: cannot assign to 'let' value 'result'
result = 500
参考
Introduction to the Swift REPL
色々できそう。↓
コマンドライン(Command Line)プログラムとして使う [1]