Last active
April 27, 2016 05:25
-
-
Save bagelturf/b048f41debfa2385b23adfea5ee261dc to your computer and use it in GitHub Desktop.
Why do I need "as V?" ?
This file contains 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
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