Skip to content

Instantly share code, notes, and snippets.

@kasrak
Created December 10, 2014 05:03
Show Gist options
  • Save kasrak/2bca5b265b16c477c14a to your computer and use it in GitHub Desktop.
Save kasrak/2bca5b265b16c477c14a to your computer and use it in GitHub Desktop.
Swift: heterogenous generic items in an array
struct Box<T> {
let value: T
}
// This works:
let boxes1 = [Box(value: 42), Box(value: "hi")]
// This works:
let boxes2 = [Box(value: 42), Box(value: "hi")] as [Box<Any>]
// This doesn't work: "'Int' is not identical to 'String'"
let boxes3 = [Box<Int>(value: 42), Box<String>(value: "hi")]
// This doesn't work: "'String' is not identical to 'Any'"
let boxes4 = [Box<Int>(value: 42), Box<String>(value: "hi")] as [Box<Any>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment