Last active
April 21, 2023 17:14
-
-
Save kongtomorrow/8d1ac5161021451c794c to your computer and use it in GitHub Desktop.
you're freaking me out, 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
func foo(f:()->(Int?)) { | |
println(f()) | |
} | |
let bar : ()->Int = { 3 } | |
// foo shouldn't be able to take bar, should it? | |
foo(bar) // prints Optional(3) | |
// you're freaking me out, Swift. Did it allocate a wrapping closure? | |
// 'cause say we wanted to do the coercion by hand. We'd have to do this: | |
let coercedBar = { ()->Int? in return .Some(bar()) } | |
foo(coercedBar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't it beautiful that this works? The compiler could throw an error, and force you to code the wrapping manually. However, then we would be on a slippery slope to disallow implicit wrapping of non-optionals in optionals as well, as non-optional and optional type are totally different as well. Consider: