`B ] = `B what does "B" mean? Why do we need this? Yours faithfull...">

What does "B" mean?

In toplevel, I get the following output:

#`B - : [> `B ] = `B 

what does "B" mean? Why do we need this?

Yours faithfully!

+4
source share
2 answers

An identifier prefixed by type `B B` is a type constructor for the polymorphic variant . It looks like an algebraic type constructor:

 type abc = A | B | C 

However, you can use the values ​​of the polymorphic variant without declaring them, and in general they are much more flexible than ordinary algebraic types. The trade-off is that they are also quite difficult to use.

One thing people use for them is how simple named values ​​are, such as enum values ​​in C. Or, more precisely, as atoms in Lisp. You can use the usual algebraic types for this, but you need to carefully observe your definitions and protect against duplication. With polymorphic options you do not need to do any of them. You can use them without declaring them, and the constructors need not be unique (two different types can have the same constructor).

Constructors of polymorphic variants can also take parameters, like algebraic constructors can be. This way you can also write (`B 77) , a constructor with one int parameter.

This is a pretty big topic - see the OCaml manual section above for more information.

+6
source

This is a polymorphic option. From the doc:

The options presented in Section 1.4 are a powerful tool for building data structures and algorithms. However, sometimes they do not have the flexibility to use in modular programming. This is because each constructor reserves a name that will be used with a unique type. You cannot use the same name in another type or consider a value of a certain type to belong to another type with more constructors.

With polymorphic variants, this initial assumption is removed. That is, the variant tag does not belong to any type in particular, the type system will simply verify that this is a valid value in accordance with its use. You do not need to determine the type before using the variant tag. The type of variant will be displayed independently for each of its applications.

+3
source

All Articles