Implicit Member Expressionという仕様だが、要は一意に特定できる個所でレシーバーを省略できるという機能で、そんなに面白くないし子ネタ何ですが、Language Reference の説明が分かりにくかったので。
An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a class method, in a context where type inference can determine the implied type. It has the following
Language Referenceには、このように説明があり、enumration caseかclass methodで使えると読める。
が、昨日試した感じだとclass methodでは使えなかった。てか、そもそもSwiftではclass methodとは言わずにtype methodと呼んでいるようだ。
それで、色々試した結果、下記のような感じだった。
class MyInt8 { class func int8() -> Int8 { return .max } class func myInt8() -> MyInt8 { return MyInt8() } } var a1:Int8 = .max // パターン1 OK var a2:MyInt8 = .myInt8() // パターン2 OK var a3:Int8 = .int8 // パターン3 NG var a4:Int8 = .int8() // パターン3' NG
パターン1: Int8の組み込みメソッドだか変数だかはOK
パターン2: ユーザー定義のクラスインスタンスを返すならOK
パターン3: Int8を返すType methodはNG
この機能なんに使うんだろうか。
インスタンスをswitchで振り分けてみようかと思って、コードを書いていたらXcodeが死ぬほど落ちたので試せませんでした。; _ ;
switch .myInt8() { case ...
こんな感じで書こうとすると、switchの行を書いた瞬間に固まり落ちる。。。夢を見た気がする。(NDA)