|
|
Log in / Subscribe / Register

How does this compare to "Accept interfaces, return structs" in Go?

How does this compare to "Accept interfaces, return structs" in Go?

Posted Apr 26, 2024 13:56 UTC (Fri) by tialaramex (subscriber, #21167)
In reply to: How does this compare to "Accept interfaces, return structs" in Go? by wahern
Parent article: Existential types in Rust

Yeah, I think this feature in Go promises only for example T = ∀X { a: X; f: (X → int); } whereas in Rust you're promised T = ∃X { a: X; f: (X → int); }

That is, in Go they're saying this has *any* type which has the specified property and it might vary, while in Rust we're saying that while we won't tell you what type it is, we promise there is *some* specific type which has that property.

The Rust compiler will learn what that type is, but it doesn't need to spell it, for the compiler this being "the type you get when you compile the closure on line #124 of this file" is fine, it's type #10295 but humans want names, both in terms of being able to understand what's going on, and in Rust because they are literally required by the language to spell the name of a type in the function signature, so hence without Existential types you cannot do this in Rust.

In C++ you don't have to spell names of types in most places, you can talk about types in terms of other things, for example decltype(a + b) is just "The type of the result if you were adding a and b together" but they still have the same distinction Rust has and for the same reason - it's expensive to handle that any type case, in a language like Go it's no cheaper when this happen but you're used to the language just not telling you the price of anything - it's girl menus at a posh restaurant, is the salad cheaper than the lobster? Who knows, the prices are on the menu the guy has.


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds