Skip to content

Instantly share code, notes, and snippets.

@bagelturf
Last active April 27, 2016 05:25
Show Gist options
  • Save bagelturf/b048f41debfa2385b23adfea5ee261dc to your computer and use it in GitHub Desktop.
Save bagelturf/b048f41debfa2385b23adfea5ee261dc to your computer and use it in GitHub Desktop.
Why do I need "as V?" ?
import Cocoa
private struct Bin<V> {
var key: Int
var value: V?
}
public class Thing<V> {
private var bins = [Bin<V>]()
public func value<V>(key: Int) -> V? { // Answer: remove the <V> because it is not needed and shadows the outer <V>
precondition(key != 0)
for index in 0..<bins.count {
let bin = bins[index]
let value = bin.value
if value == nil { continue }
if bin.key == key {
return value as! V? // Compiles OK
// return value // Cannot convert return expression type error
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment