Created
August 11, 2016 15:34
-
-
Save rknLA/97bedeb002e5e378bc6236d4e77aca5a to your computer and use it in GitHub Desktop.
return a specified generic without passing a type?
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
//: 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