ぜのぜ

しりとりしようぜのぜのぜのぜ

81日目

日記

お腹すいた

今日書いたコード

protocol Shape {
    func draw() -> String
}

struct Triangle: Shape {
    func draw() -> String {
        return """
            *
            **
            ***
            """
    }
}

let body = {
    Triangle()
}

func makeTriangle() -> some Shape {
    body()
}

func protoMakeTriangle() -> Shape {
    body()
}


let triangle = makeTriangle()
print(triangle.draw())
// *
// **
// ***
let protoTriangle = protoMakeTriangle()
print(protoTriangle.draw())
// *
// **
// ***

func flip<T: Shape>(_ shape: T) -> some Shape {
    return Triangle()
}

flip(triangle)
// flip(protoTriangle) // Protocol 'Shape' as a type cannot conform to the protocol itself

感想

今日書いたコードが嬉しさの一つっぽい.

Opaque Type何もわからんとなったんだけどこの2つを読んだらなるほどねーぐらいにはなった.2つ目は日本語だけどOpaque Typeの説明だけではないのでそれだけを知りたい場合は合わないかも.