Live data from Hacker News

Sum Types in Julia and Rust

andreaskroepelin.de

1–10 of 70 posts

Re: Sum Types in Julia and Rust

#2
Great informative content, Keep doing the good work. https://chennaitraining.in/java-training-in-chennai/ https://chennaitraining.in/python-training-in-chennai/ https://chennaitraining.in/r-programming-training-in-chennai... https://chennaitraining.in/selenium-training-in-chennai/ https://chennaitraining.in/digital-marketing-training-in-che... https://chennaitraining.in/angular-js-training-in-chennai/ https://chennaitraining.in/aws-training-in-chennai/ https://chennaitraining.in/big-data-training-in-chennai/ https://chennaitraining.in/ccna-training-in-chennai/ https://chennaitraining.in/hadoop-training-in-chennai/ https://chennaitraining.in/informatica-training-in-chennai/ https://chennaitraining.in/pmp-training-in-chennai/ https://chennaitraining.in/scrum-master-training-in-chennai/ https://chennaitraining.in/seo-training-in-chennai/ https://chennaitraining.in/tally-training-in-chennai/ https://chennaitraining.in/dotnet-training-in-chennai/ https://chennaitraining.in/etl-testing-training-in-chennai/ https://chennaitraining.in/salesforce-admin-training-in-chen... https://chennaitraining.in/salesforce-developer-training-in-... https://chennaitraining.in/sap-hana-training-in-chennai/

Re: Sum Types in Julia and Rust

#3
I may be wrong, but I don't think this is what people mean by 'sum types'.

I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... )

I guess enums are a type of sum type, but seems this is more specifically about enums

Re: Sum Types in Julia and Rust

#5
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

I think the use of an enum (in the Rust example) makes this a sum type situation: https://tonyarcieri.com/a-quick-tour-of-rusts-type-system-pa...

Re: Sum Types in Julia and Rust

#6
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

I believe another word for enums (in the Rust sense, not in the C++ or Java sense) are tagged unions.

Re: Sum Types in Julia and Rust

#7
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

Rust enums are full union types. i.e. while each possibility in a (say) Java enum must be of the same type, each possibility in a Rust enum can be of a different type.

i.e.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re: Sum Types in Julia and Rust

#8
When I think of sum types, I like the categorical definition the best, which is that a sum A+B has two morphisms (i.e. constructors) Inl : A -> A+B and Inr : B -> A+B, with a simple commuting diagram[0]. Or in Rust,

  enum Sum {
     Inl(A),
     Inr(B),
  }
Why do I prefer this definition? Well, category theory abstracts away irrelevant details, and sums have a "universal property" associated with them. Roughly speaking that means that it doesn't matter how you define sum types in your language, if they fit the universal property of sums (up to isomorphism) then they truly can be considered sum types. In the Rust PlayerClass example the corresponding sum is (Solarian + (Polarian + Centaurian)), and morphisms

  Sol  = Inl . Inl
  Pol  = Inl . Inr
  Cent = Inr . Inr
[0] https://en.wikipedia.org/wiki/Coproduct

Re: Sum Types in Julia and Rust

#9
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

Union types aren't sum types. One difference is A | A = A, but A + A = 2 * A.

Re: Sum Types in Julia and Rust

#10
post #3

I may be wrong, but I don't think this is what people mean by 'sum types'. I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h... ) I guess enums are a type of sum type, but seems this is more specifically about enums

Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing.

1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`.

2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not themselves types: they are like a struct field name. It's quite possible to have a sum type with N tags, each wrapping `int`.

So with this understanding Rust has sum types but not union types, while TypeScript has union types but not sum types.

Post reply on HN