-
-
Save dfrib/01e4bf1020e2e39dbe9cfb14f4165656 to your computer and use it in GitHub Desktop.
Misc Swift
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
protocol Homogenous { | |
func foo(_: Self) | |
} | |
extension Homogenous { | |
func foo(_: Self) { } | |
} | |
extension Int: Homogenous { } | |
let a: Int = 1 | |
let b: String = "one" | |
/* Generally, we can't type check using 'is' against a homogenous protocol, | |
but if the variable we are type checking is an optional, we can. | |
Why is this? */ | |
let bar: Int? = a | |
bar is Homogenous // true | |
Optional(a) is Homogenous // true | |
let baz: String? = b | |
baz is Homogenous // false | |
/* generally: | |
error: protocol 'Homogenous' can only be used as a generic constraint | |
because it has Self or associated type requirements */ | |
//a is Homogenous | |
//b is Homogenous | |
/* Can we emulate it with a simple | |
enum wrapper of our own? Nope. */ | |
enum Wrapper<T> { | |
case Some(T) | |
case None | |
} | |
let foo = Wrapper.Some(a) | |
//foo is Homogenous | |
/* error: protocol 'Homogenous' can only ... */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment