julia における関数の機微について調べてみた
versioninfo()
function func1(x)
sin(x) # return が無くても最後の式が返値になる
end
func1(x) = sin(x) # 同じシグネチャで再定義すると上書き可能
次の書き方でも func2 を定義できるけれど以下の相違がある
func2 = x -> sin(x)
func2 = func1
func2 = 1
関数名は const 扱いで上書きできない
func1 = x -> sin(x)
const にすれば代入は不可能になるけど、関数自体は無名のまま
const func3 = x -> sin(x)
func2 = func3
無名関数は内部的に #数字 という名前(?)で管理されている?
知ってないと表示を見ても何のことだか分からない
println(x->x)
x->x
引数やキーワード引数に ... を付けると、Tuple として受け取れる
function test(a, other... ; opt1=0, options...)
println("a=", a, ", other=", other)
println("opt1=", opt1, ", options=", options)
end
test(1, 2, 3, 4; opt1=1, opt2=2, opt3=3)
逆に、Tupleや配列に ... を付けて渡すと、展開して渡せる
test((1,2,3)...)
test([1,2,3]...)
test([1,2]...)
あれ?これは正しいのか?
length((1,))
ああ、(1) だけだと数字の 1 と区別が付かないから、1要素の Tuple は後ろに , を付けてるのか。
test(1;[(:opt1, 1), (:opt2, 2)]...)
ポリモーフィズム的な
test2(a::Int) = println("Int")
test2(s::String) = println("String")
test2(1)
test2("a")
test2(a::Number) = println("Number")
test2(1.0)
1 は Number でもあるが Int が採用される
test2(1)
test2(a) = println("Any type")
test2(true)
ありゃ、Bool は Number なのか。。。
test2([])
test2(1.0)
test2(1)
可変引数関数ではない方が優先される
test2(a...) = println("Variable args of any type")
test2(1)
test2([])
可変引数でも型指定があればそちらが優先
test2(a::Array...) = print("Variable args of Array")
test2([])
test2(:symbol)
test2(a, b=false) = println("With omittable b")
test2(1)
test2(:symbol)
どうしてそっちが呼ばれるの? → 答えは下の方に
test3(a) = println("Any type")
test3(a; option=false) = println("Any type with option")
2 methods にならない事に注意
test3(1)
これは、キーワード引数付きが優先されたのではなく後から書いたもので上書きされただけ
test3(a) = println("Any type")
test3(1)
test3(1, option=true)
ではなかった。
優先順位が変わっただけでキーワード引数付きも呼べている。
1 method と書かれている意味は何なのか???
test3(a; option2=false) = println("Any type with option2")
test3(1, option=true)
test3(1, option2=true)
キーワード引数無しとキーワード引数付きは別シグネチャだけど、異なる名前のキーワード付き同士は同一シグネチャなので、後から定義したもので上書きされてしまうということか。
test3(a; options...) = println("Any type with options")
test3(1, option2=true)
可変キーワード引数付きの優先度が高い?
test3(a; option2=false) = println("Any type with option2")
test3(1, option2=true)
じゃなくて、後から定義されたものが優先されているだけ?
test3(1, option1=true)
いやいや、上書きされてるや
指定キーワード付きと、可変キーワード付きは同一シグネチャとして、互いに上書き対象になってる
test4(a) = "only a"
test4(a, b=false) = "with omittable b"
test4("which should be called?")
優先順位が同位なので、後から定義されたものが採用されてるわけだ
だからもう一度「後から」定義してやればそちらが優先される
test4(a) = "only a"
test4("which should be called?")
省略可能引数持ちとそうでないものとを両方定義するときは順番が重要と覚えよう
ちゃんとこっちも呼べる
test4(1, 2)