Skip to content

Instantly share code, notes, and snippets.

@yostane
Last active June 24, 2024 17:58
Show Gist options
  • Select an option

  • Save yostane/c2619f36b41875606075e1f4baf2b93a to your computer and use it in GitHub Desktop.

Select an option

Save yostane/c2619f36b41875606075e1f4baf2b93a to your computer and use it in GitHub Desktop.
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