2

Swift で一連の責任パターンを実装しようとしています。

public class Chain<T, U> {
    private var command: (T?, (U?) -> Void) -> Void
    private var runCommand: (() -> Void)?
    private var nextCommand: ((U?) -> Void)?

    private init(command: (T?, (U?) -> Void) -> Void) {
        self.command = command
    }

    private func next(u: U?) {
        self.nextCommand?(u)
    }

    func then<V>(command: (U?, (V?) -> Void) -> Void) -> Chain<U, V> {
        let c = Chain<U, V>(command: command)

        self.nextCommand = { command($0, c.next) }
        c.runCommand = self.runCommand

        return c
    }

    func endWith(command: (U?) -> Void) {
        self.nextCommand = command
        self.runCommand!()
    }

    static func build<V>(command: ((V?) -> Void) -> Void) -> Chain<AnyObject, V> {
        let c = Chain<AnyObject, V>(command: { _, next in command(next) })
        c.runCommand = { command(c.next) }
        return c
    }
}

私のクラスではコンパイル エラーは発生しませんが、単純なユース ケース (以下のようなもの) は機能しません。次のエラーが発生します。error: cannot invoke 'endWith' with an argument list of type '((_?) -> ()) ; expected an argument list of type '((U?) -> Void)'

何か考えはありますか?

Chain.build { next in
    print("Foo")
    next("Bar")
}
.then { o, next in
    print(o)
    next(15)
}
.endWith { o in
    print(o)
}

Swift でのジェネリック使用のエッジ ケースであることはわかっています。ただし、ジェネリック型を明示的に特殊化することはできないため、これまで解決策が見つかりませんでした。

4

1 に答える 1

3

コンパイラは、例の型を推測できません。あいまいな場所を指定するだけです。

Chain<String,Int>.build { next in
  print("Foo")
  next("Bar")
  }
  .then { (o: String?, next: Int? -> Void) in
    print(o)
    next(15)
  }
  .endWith { o in
    print(o)
}
于 2015-12-07T21:26:26.050 に答える