LoginSignup
1
2

More than 5 years have passed since last update.

[Spider]Swiftと似た?を使ったチェーンのメモ

Last updated at Posted at 2014-11-18

参考公式ドキュメント

SpiderはSwiftと似ている?

Spiderは、 Swiftからも着想を得て 開発されています。

そのため、しばしばSwiftと似たシンタックスが登場します。

例えば、Swiftで特徴的なシンタックスのひとつとして、optionalがありますよね。

var name:String?

のように。

クラスとかなら、

class Animal {
    func name()->Void{
        println("animal")
    }
}

var animal:Animal?

animal = Animal()

animal?.name()

みたいな。

実は、Spiderでもこの ?チェーン のようなものが
搭載されているので、それを紹介します。

Spiderでの?チェーン

Spiderでは、下記のように?をつけることで

undefinedのTypeErrorをさけることができます。

func Animal() {
    this.name = () -> {
        return "Dog"
    }
}

var animal = new Animal()
animal.name()

var name = animal.name?() // "Dog"
var age = animal.age?()   // undefined

もちろん、Swiftと比べると別物ではありますが ;^_^A

もうわざわざif文でチェックする必要がないですね!

これは良機能だと思います!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2