2015 02-18 xxx-literalconvertible

32
xxxLiteralConvertible @taketo1024 2015/02/18 potatotips #14

Upload: taketo-sano

Post on 17-Jul-2015

1.543 views

Category:

Software


6 download

TRANSCRIPT

xxxLiteralConvertible

@taketo1024

2015/02/18 potatotips #14

2014/10/18 「iOS 8 / Swift 勉強会 @ ヤフー」

import Foundation

struct Complex: Equatable, IntegerLiteralConvertible, FloatLiteralConvertible { let x: Double let y: Double init(_ x: Double, _ y: Double) { self.x = x self.y = y } init(_ x: Double) { self.x = x self.y = 0 } init(integerLiteral x: IntegerLiteralType) { self.x = Double(x) self.y = 0 } init(floatLiteral x: FloatLiteralType) { self.x = x self.y = 0 } init(r: Double, θ: Double) { self.x = r * cos(θ) self.y = r * sin(θ) } }

func == (z: Complex, w: Complex) -> Bool { return z.x == w.x && z.y == w.y }

func Re(a: Complex) -> Double { return a.x }

func Im(a: Complex) -> Double {

import Foundation

struct Complex: Equatable, IntegerLiteralConvertible, FloatLiteralConvertible { let x: Double let y: Double init(_ x: Double, _ y: Double) { self.x = x self.y = y } init(_ x: Double) { self.x = x self.y = 0 } init(integerLiteral x: IntegerLiteralType) { self.x = Double(x) self.y = 0 } init(floatLiteral x: FloatLiteralType) { self.x = x self.y = 0 } init(r: Double, θ: Double) { self.x = r * cos(θ) self.y = r * sin(θ) } }

func == (z: Complex, w: Complex) -> Bool { return z.x == w.x && z.y == w.y }

func Re(a: Complex) -> Double { return a.x }

func Im(a: Complex) -> Double {

import Foundation

struct Complex: Equatable, IntegerLiteralConvertible, FloatLiteralConvertible { let x: Double let y: Double init(_ x: Double, _ y: Double) { self.x = x self.y = y } init(_ x: Double) { self.x = x self.y = 0 } init(integerLiteral x: IntegerLiteralType) { self.x = Double(x) self.y = 0 } init(floatLiteral x: FloatLiteralType) { self.x = x self.y = 0 } init(r: Double, θ: Double) { self.x = r * cos(θ) self.y = r * sin(θ) } }

func == (z: Complex, w: Complex) -> Bool { return z.x == w.x && z.y == w.y }

func Re(a: Complex) -> Double { return a.x }

func Im(a: Complex) -> Double {

let z = 2 + 3 * i

こういう風に書ける

z

let z = 2 + 3 * iz

{

Complex(0, 1) 定数として定義してある

こういう風に書ける

let z = 2 + 3 * i

こういう風に書ける

{

Complex(3, 0) * Complex(0, 1)

z

{

Complex(0, 1)

let z = 2 + 3 * i

こういう風に書ける

{

Complex(3, 0) * Complex(0, 1){

z

{

Complex(0, 1)

Complex(2, 0) + Complex(3, 0) * Complex(0, 1)

let z = 2 + 3 * i

こういう風に書ける

{

Complex(3, 0) * Complex(0, 1){Complex(2, 0) + Complex(3, 0) * Complex(0, 1)

z

let z: Complex = Complex(2, 0) + Complex(3, 0) * Complex(0, 1)

{{

Complex(0, 1)

i * i == -1

i * i == -1{Complex(0, 1) * Complex(0, 1)

i * i == -1{Complex(0, 1) * Complex(0, 1) {

Complex(0, 1) * Complex(0, 1) == Complex(1, 0)

いい感じだ!

let r = 2.0 let θ = M_PI / 4 let z = r * (cos(θ) + i * cos(θ))

let r = 2.0 let θ = M_PI / 4 let z = r * (cos(θ) + i * cos(θ))

!?

let i = Complex(0, 1) let a = -1 i * i == a

let i = Complex(0, 1) let a = -1 i * i == a

…???

import Foundation

struct Complex: Equatable, IntegerLiteralConvertible, FloatLiteralConvertible { let x: Double let y: Double init(_ x: Double, _ y: Double) { self.x = x self.y = y } init(_ x: Double) { self.x = x self.y = 0 } init(integerLiteral x: IntegerLiteralType) { self.x = Double(x) self.y = 0 } init(floatLiteral x: FloatLiteralType) { self.x = x self.y = 0 } init(r: Double, θ: Double) { self.x = r * cos(θ) self.y = r * sin(θ) } }

func == (z: Complex, w: Complex) -> Bool { return z.x == w.x && z.y == w.y }

func Re(a: Complex) -> Double { return a.x }

func Im(a: Complex) -> Double {

IntegerLiteralConvertible, FloatLiteralConvertible

IntegerLiteralConvertible, FloatLiteralConvertible

IntegerLiteralConvertible, FloatLiteralConvertible

リテラルからしか暗黙的キャストできない…

残念だが仕方ない。

xxxLiteralConvertible• ArrayLiteralConvertible

• BooleanLiteralConvertible

• DictionaryLiteralConvertible

• FloatLiteralConvertible

• NilLiteralConvertible

• IntegerLiteralConvertible

• StringLiteralConvertible

• UnicodeScalarLiteralConvertible

struct Matrix3x3<T>: ArrayLiteralConvertible { init(arrayLiteral elements: T...) { // ... } }

let m: Matrix3x3 = [1, 0, 0, 0, 1, 0, 0, 0, 1];

これぐらいのことはできる

さらに残念なことに、

let url: NSURL = “http://yahoo.co.jp”

let image: UIImage = “image.png"

let color: UIColor = 0x722F37

let url: NSURL = “http://yahoo.co.jp”

let image: UIImage = “image.png"

let color: UIColor = 0x722F37

↑ Swift 1.0 でできてたのが、1.1 でできなくなった…

本当に残念。

Thanks...

Twitter: taketo1024Blog: http://taketo1024.hateblo.jp/