4 ms·
Great article! I would just like to add to the point about C# generics. > "generics in Rust are zero-cost abstractions. rustc performs a process called monomor
by davidhyde 4y ago
Great article! I would just like to add to the point about C# generics.
> "generics in Rust are zero-cost abstractions. rustc performs a process called monomorphization, where generic items (methods, structs, etc.) are "flattened" at compile time into only the types that are used. (Compare this to a language like C#, where generics are evaluated at runtime.)"
I believe that generics are evaluated at runtime in C# due to just-in-time compilation. C#'s implementation of generics (which they call reification) is more similar to Rust's monomorphization than, say Go and Java's lookup table type erasure approach.
More information about this can be found here: https://stackoverflow.com/questions/31876372/what-is-reification https://stackoverflow.com/questions/31876372/what-is-reifica...
- melony 4y agoWhat about the Dyn type parameter?
- davidhyde 4y agoGood point! Using dyn Trait syntax (aka, dynamic dispatch) uses type erasure and is kind-of like Go and Java's implementation of Generics I guess. You can definitely use that in Rust but the article is about impl Trait syntax which is for concrete types (not type erased)
- estebank 4y agoThe only nitpick I'd have is that `impl Trait` doesn't necessarily imply a concrete type. Or rather it does, but in so far `Box<dyn Trait>` is also a concrete type :) That means that you can have a function that returns `-> impl Trait` with a concrete implementation because it has a single return value, but if you add a second and don't want to create an enum for a simple version of static dispatch, you can change the returned values to be trait objects and the callers don't break.
- geodel 4y agoGo's generic are not type erasure.
- whateveracct 4y ago`reflection` was updated to capture type parameters?
- Diggsey 4y agoI think this is only true in C# for value types - I think the same generated code is used for all reference types, so eg. `List<object>` uses the same code as `List<string>` but different from `List<int>`.
- merb 4y agowell yeah. it's explained here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generics-in-the-run-time https://docs.microsoft.com/en-us/dotnet/csharp/programming-g... btw. it's not called "runtime evaluation" and more or less a kind of runtime substitution. (because nothing gets evaluated it gets substituted) and btw. c++ allows references types to have specializations, this is not possible in c#
- jhgb 4y agoThis is to be expected with what are essentially pointers to heap objects. The actual code being executed only manipulates machine-word-sized values with the same semantics, so it makes little sense to have copies of the same code.
- astrange 4y agoIt could be useful to duplicate it for devirtualization, and that's more likely with a JIT than compilation since it doesn't cost code size on disk.
- jhgb 4y agoWell, yes. But you can't devirtualize until you know the types as well as possible, which is after "de-generifying", so as to speak (I don't really know what terminology .NET uses for this).