0

I am going through "Programming Languages" in coursera. I am having a hard time to understand what "datatype bindings" are.

https://www.coursera.org/lecture/programming-languages/datatype-bindings-vysQv

Are datatype bindings similar to type alias(in Golang)?

In SML this is the example given to explain datatype bindings.

datatype mytype = TwoInts of int * int 
    | Str of string
    | Pizza

Here, from what I understand mytype is a new custom defined datatype. It consists of two sub-parts, one of which is TwoInts of type int*int and another part Str of type string. I do not understand what Pizza is.

I tried googling about datatype bindings. While I could find many interesting articles on databindings couldn't find useful articles on datatype bindings.

I understand Golang and Ruby pretty well. So, any explain with examples from these languages if possible would be even more helpful.

Thanks

sshine
  • 15,635
  • 1
  • 41
  • 66
Surya
  • 2,429
  • 1
  • 21
  • 42
  • 1
    It is a sum type, also known as tagged union. Neither Ruby nor Go have them. Pizza in this case is a variant with no value attached. – Ivan C Dec 11 '20 at 07:04
  • Thanks, does that mean that if I have a variable of type `mytype`, then I could assign it to a value that is either a string or an `int*int`. Also, what is Pizza? @IvanC – Surya Dec 11 '20 at 07:06
  • 1
    Take for example haskell's Maybe or rust's Option. They are defined as Maybe = Some(T) || None. Just like pizza, None is a type constructor that takes no value. – Ivan C Dec 11 '20 at 07:12
  • so pizza is like the default datatype?... will read more on "tagged unions" – Surya Dec 11 '20 at 07:18
  • I have never heard the term "datatype binding", but it appears that the person who created the course uses the term "binding" in their own slightly peculiar way. – molbdnilo Dec 11 '20 at 20:06
  • Yes, normally you would use "data type" to refer to the creation of an algebraic type (consisting of sum types, product types and built-in types), and "binding" in a bunch of ways related to tying a value to a name, e.g. `val x = 42`. I suppose, when you create a `datatype foo = Foo | Bar`, you do in a sense bind the name `Foo` with a value (with no better reference than simply "`Foo`"). I've heard people say "`Foo` is a value constructor for some value we can call `Foo`", and I suppose one good point with being so pedantic is that you *also* get a *pattern constructor* named `Foo`! – sshine Dec 12 '20 at 18:54

0 Answers0