Skip to content

Instantly share code, notes, and snippets.

@rknLA
Created August 11, 2016 15:34
Show Gist options
  • Save rknLA/97bedeb002e5e378bc6236d4e77aca5a to your computer and use it in GitHub Desktop.
Save rknLA/97bedeb002e5e378bc6236d4e77aca5a to your computer and use it in GitHub Desktop.
return a specified generic without passing a type?
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
struct VarType<T> {
let name: String
let val: T
}
class Container {
let a: VarType<Int> = VarType<Int>(name: "a", val: 12)
let b: VarType<Float> = VarType<Float>(name: "b", val: 1.2)
let f: VarType<Bool> = VarType<Bool>(name: "f", val: true)
let c: VarType<String> = VarType<String>(name: "c", val: "hi")
func getVarNamed<T>(name: String) -> VarType<T> {
switch name {
case "a":
return self.a
case "b":
return self.b
case "c":
return self.c
default:
return self.f
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment