3 ms·
Nominal types versus structural types is basically the main distinction at this point (if we ignore .net, etc). I like both languages, happy to see them converg
by dgellow 3y ago
Nominal types versus structural types is basically the main distinction at this point (if we ignore .net, etc). I like both languages, happy to see them converge while keeping their defining characteristics
- CharlieDigital 3y agoWhere do C# anonymous types fall? From a functional perspective, they are a purely structural type. Here's a snippet of C# I wrote recently to transform some JSON: var transformed = media.Select(m => { var parts = m.Split('/'); return new { addedAtUtc = "2023-05-15T05:59:31.398Z", originTripUid = parts[4], path = $"{parts[4]}/{parts[5]}", rank = "", size = 103978, stored = true, type = "document", }; }) File.WriteAllText( Path.Combine(Environment.CurrentDirectory, "output.json"), System.Text.Json.JsonSerializer.Serialize(transformed) ); It would be easy to mistake this as JS at a quick glance.
- oaiey 3y agoAnonymous types are nominal types (like all others in .NET). They are just auto-generated and auto-named. Duck Typing at runtime is possible and to a degree checkable and compile-time.
- CharlieDigital 3y agoI get that; that's why I qualified it as a "functional perspective" because even though the underlying implementation is an auto-generated, auto-named type, the functional usage of it is a structural type (with limitations).
- AshleyGrant 3y agoThey aren't structural because the following doesn't work in C#, but would work (with tweaks to make it valid code) in a structurally typed language such as TypeScript: var transformed = media.Select(m => { var parts = m.Split('/'); return new { addedAtUtc = "2023-05-15T05:59:31.398Z", originTripUid = parts[4], path = $"{parts[4]}/{parts[5]}", rank = "", size = 103978, stored = true, type = "document", }; }).ToList(); transformed.AddRange(otherMedia.Select(m => { var parts = m.Split('/'); return new { addedAtUtc = "2023-05-15T05:59:31.398Z", originTripUid = parts[4], path = $"{parts[4]}/{parts[5]}", rank = "", size = 103978, stored = true, type = "document", documentType = parts[6] }; })); Adding "documentType" breaks in C# but would work in a structurally typed language as a structural type checker can see that the second result fulfills all of the necessary properties. Doing this in C# would require creating an interface and being explicit.
- littlecranky67 3y agoUnless you were using the (yet unimplemented) exact types in TS. https://github.com/Microsoft/TypeScript/issues/12936 https://github.com/Microsoft/TypeScript/issues/12936
- AshleyGrant 3y agoSure, but that would be opt-in if it were ever implemented. I think both styles of type system have their pros and cons. I have loved the cross-pollination between C# and TS with great ideas flowing in both directions.
- RyanCavanaugh 3y agoRuntime type systems: TS has none; C# has extensive RTTI Function dispatch: TS is purely dynamic; C# has both static and dynamic OOP: Mandatory in C#, optional in TS Variance: Implicit in TS, explicit in C# Numeric types: TS has one (number); C# has the entire signed/unsigned/float x 8/16/32/64-bit matrix There's really no intentional effort to converge them.
- dgellow 3y agoYour list of differences is correct but not really what I meant. The points you listed are mostly due to JS runtime constraints. The choice of nominative vs structural type system is purely a design choice by the typescript team.
- no_wizard 3y agoTS has two. number and bigint[0], corresponding to Number and BigInt in JS, respecitvely [0]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#bigint https://www.typescriptlang.org/docs/handbook/2/everyday-type...