Created
March 27, 2016 10:20
-
-
Save lancy/7e644a8e50deb68a1aec to your computer and use it in GitHub Desktop.
Mutating Generic Enum Issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum List<Element> { | |
case End | |
indirect case Node(Element, next: List<Element>) | |
} | |
enum ListError: ErrorType { | |
case EndCannotAppendValue | |
} | |
extension List { | |
mutating func append(value: Element) throws -> List { | |
guard case .Node(let element) = self else { throw ListError.EndCannotAppendValue } | |
let next = List.Node(value, next: .End) | |
// Error: Cannot convert value of type 'List<Element>' to expected argument type 'List<_>' | |
self = List.Node(element, next: next) | |
return next | |
} | |
} |
Bad news: append
function can only return a immutable value, which can't be call in chain.
var head = List.Node(1, next: .End)
// error: cannot use mutating member on immutable value: function call returns immutable value
try! head.append(2).append(2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 11 is wrong:
Type of
element
is infer to(Element, next: List<Element>)
, which make compiler inferself
asList<(Element, next: List<Element>)>
.The correct code should be: