type level =
[ `Debug
| `Info
| `Warning
| `Error]
Can i remove the "`" here ?
Sincerely!
The backtick ` is a typographical mark used mainly in computing. It is also known as backquote, grave, or grave accent. The character was designed for typewriters to add a grave accent to a (lower-case) base letter, by overtyping it atop that letter.
As you may have understood now, there is no real difference between using single quotes, double quotes, or backticks. You can choose one or multiple styles based on your preference. However, It is always good to stick to a single format throughout the project to keep it neat and consistent.
Backticks ( ` ) are used to define template literals. Template literals are a new feature in ECMAScript 6 to make working with strings easier. Features: we can interpolate any kind of expression in the template literals.
Template literals are enclosed by backtick ( ` ) characters instead of double or single quotes. Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression} .
It's hard to answer this question yes or no.
You can remove the backticks and the square brackets. Then you would have
type level2 = Debug | Info | Warning | Error
In the simplest cases, this type is is very similar to your type level
. It has 4 constant constructors.
In more complex cases, though, the types are quite different. Your type level
is a polymorphic variant type, which is more flexible than level2
above. The constructors of level
can appear in any number of different types in the same scope, and level
participates in subtyping relations:
# type level = [`Debug | `Info | `Warning | `Error]
# type levelx = [`Debug | `Info | `Warning | `Error | `Fatal]
# let isfatal (l: levelx) = l = `Fatal;;
val isfatal : levelx -> bool = <fun>
# let (x : level) = `Info;;
val x : level = `Info
# isfatal (x :> levelx);;
- : bool = false
The point of this example is that even though x has type level
, it can be treated as though it were of type levelx
also, because level
is a subtype of levelx
.
There are no subtyping relations between non-polymorphic variant types like level2
, and in fact you can't use the same constructor name in more than one such type in the same scope.
Polymorphic variant types can also be open-ended. It's a big topic; if you're interested you should see section 4.2 of the OCaml manual, linked above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With