To achieve this, you can use generics:
func isOptional<T>(x:T?)->Bool { return true } func isOptional<T>(x:T)->Bool { return false }
Edit
The above code can be used to find out if a variable is an optional type. The only way to find out about a variable containing a type is to use reflection:
var t1:Any.Type=(String?).self var t2:Any.Type=(String).self Mirror(reflecting: t1).description Mirror(reflecting: t2).description
The first call to Mirror gives the string "Mirror for Optional<String>.Type" and the second gives "Mirror for String.Type" .
I understand that string comparison is not a convenient way to do this check, I will try to find something more perfect again.
hariseldon78 Sep 15 '15 at 16:13 2015-09-15 16:13
source share