Last active
June 5, 2017 15:07
-
-
Save kittinunf/29dc0e527cbd0a3596bb83c9b99f02e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//swift | |
protocol Addable { | |
func add(lhs: Self, rhs: Self) -> Self | |
} | |
extension String: Addable { | |
func add(lhs: String, rhs: String) -> String { | |
return lhs + rhs | |
} | |
} | |
extension Int: Addable { | |
func add(lhs: Int, rhs: Int) -> String { | |
return lhs + rhs | |
} | |
} | |
//usage | |
func printAddable(a1: Addable, a2: Addable) { | |
print(a1 + a2) | |
} | |
printAddable("Hello", "World") //print "Hello World" | |
printAddable(3, 4) //print 7 | |
//kotlin | |
interface Addable<T> { | |
func add(lhs: T, rhs: T): T | |
} | |
//this does compile in Kotlin, but it has nothing to do with Addable | |
fun String.add(lhs: String, rhs: String) = lhs + rhs | |
fun Int.add(lhs: Int, rhs: Int) = lhs + rhs | |
fun printAddable(a1: Addable, a2: Addable) { | |
print(a1 + a2) | |
} | |
//this doesn't compile | |
printAddable("Hello", "World") | |
printAddable(3, 4) | |
//proposal to Kotlin | |
implicit String : Addable<String> | |
fun String.add(lhs: String, rhs: String) = lhs + rhs | |
implicit Int : Addable<Int> | |
fun Int.add(lhs: Int, rhs: Int) = lhs + rhs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment