私はSwiftを初めて使っています。私は再帰的列挙し、ジェネリック医薬品とのバイナリツリーを実装しようとしていた。スウィフトのジェネリックスでの再帰的列挙
$ swift ADT.swift
ADT.swift:83:20: error: cannot convert value of type 'BinaryTree<T>' to expected argument type 'BinaryTree<_>'
return inorder(left) + [val] + inorder(right)
^~~~
しかし、これは動作します:
func inorder<T>(_ root: BinaryTree<T>) -> [T] {
switch root {
case .Nothing:
return []
case let .Node(val, left, right):
let l = inorder(left)
let r = inorder(right)
return l + [val] + r
}
}
間違いありここ
enum BinaryTree<T> {
indirect case Node(T, BinaryTree<T>, BinaryTree<T>)
case Nothing
}
func inorder<T>(_ root: BinaryTree<T>) -> [T] {
switch root {
case .Nothing:
return []
case let .Node(val, left, right):
return inorder(left) + [val] + inorder(right)
}
}
私が得たエラーです私の構文では?ありがとう!
私はSwift 3.0を使用しています。
ありがとうございます。興味深いことに、いくつかのソリューションでは、元のタイプ以外のタイプについての情報は提供されずにコンパイルされます。 –
@ Kuan-Ying Chou:まあ、すべての解決策が共通しているのは、コンパイラが(埋め込み) '+ '演算子の連鎖使用のためにすべての型を推論する必要がないということです。コンパイラがコンパイルに失敗したため、バグとして確認されます。私はそれに応じて私の答えを更新しました。 – thm