Last active
June 24, 2024 17:58
-
-
Save yostane/c2619f36b41875606075e1f4baf2b93a 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
| func computeMax<T: Comparable>(a: T, b: T) -> T{ | |
| return if a > b { | |
| a | |
| } else { | |
| b | |
| } | |
| } | |
| // Here, swift inferred that T is Float (or Double ?) | |
| print(computeMax(a: 100, b: 20.0)) | |
| struct Stack<T> { | |
| var items: [T] = [] | |
| mutating func push(_ item: T) { | |
| items.append(item) | |
| } | |
| mutating func pop() -> T { | |
| return items.removeLast() | |
| } | |
| } | |
| // Swift cannot infer T here | |
| var stackOfStrings = Stack<String>() | |
| stackOfStrings.push("I") | |
| stackOfStrings.push("❤️") | |
| stackOfStrings.push("Swift") | |
| print(stackOfStrings.pop()) | |
| print(stackOfStrings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment