The wikipedia articles are terribly written (for math loves or people who just need to refresh their knowledge).

What is a “sum” of types? What is a product of types? Is it possible to Cat x Dog or Cat + Dog? What does that even mean?

  • @[email protected]
    cake
    link
    fedilink
    73 months ago

    In higher math, sum and product don’t necessarily have anything to do with the sum and product we know, but are just operations with certain properties.

    In this case, a product is just a collection of multiple types, e.g. a tuple. An example would be a pair of index (integer) and value (e.g. a string) when iterating over a list.

    A sum on the other hand is more of an or. In many languages, this is called something like an enum. If for example, your program should support both integers and floating point numbers, you would need the sum type int | float.

    • xigoi
      link
      fedilink
      53 months ago

      Union types and sum types are two distinct concepts. Int | Int is the same type as Int, but Int + Int is not the same type as Int.