does carbon support type erasure for Comparable? #4535
Answered
by
danakj
fililili
asked this question in
Getting started
-
hi, all. I found Swift supports such usage for type erasure. It's interesting for me and I am looking for other language that supports same usage. So, Is such usage supported by carbon, too? thanks. func selfc<T: Comparable>(_ a: T) -> Bool {
print(a)
return a < a;
}
/* cannot compile
func selfca(_ a: any Comparable) -> Bool {
return a < a;
}
*/
struct Person: Comparable {
var name: String
var age: Int
static func < (lhs: Person, rhs: Person) -> Bool {
return lhs.age < rhs.age
}
}
let person1 = Person(name: "Alice", age: 30)
let person2 = Person(name: "Bob", age: 25)
var cs : [any Comparable] = [person1, person2, 1, 2.4]
for c in cs {
print(selfc(c))
} |
Beta Was this translation helpful? Give feedback.
Answered by
danakj
Nov 18, 2024
Replies: 1 comment 3 replies
-
Yes, what you're describing is what Carbon calls checked generics. See https://github.com/carbon-language/carbon-lang/tree/trunk/docs/design/generics for the design. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I think what you're looking for then is variadics with checked generics which is not finished but is mentioned here with some links: https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#variadic-arguments
Edit: If not, then maybe you're looking for a container, which would use checked generics as its type. That should follow from the original link, it may just not be called "type erasure" directly. But a checked generic can be used as a type for a container.