Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ask for the type class of a variable?

I'm trying to learn ReasonML, and I'm doing some koans to help me with that task. One of the koans I'm trying to code is about asking for the type of a list after converting it to an array. I know that there are operators in some languages like Haskell (:t) where we can ask for the type of a variable. Is there any equivalent to :t for ReasonML? Or it shouldn't make sense to check if Array.of_list returns an array because the compiler ensures that...

like image 659
gabrielperales Avatar asked Jan 27 '26 15:01

gabrielperales


1 Answers

I don't know much about Reason, but utop (an improved OCaml REPL) provides the #typeof directive to inspect the type of an expression:

let a = 42;;
val a : int = 42

#typeof "a";;
val a : int

Note that this is only a REPL tool. In OCaml, types are not available on runtime: they are stripped during compilation. You can learn more about the OCaml compilation process here.


As pointed out by @Rizo in the comments, there also is the #show directive, which is even available in the standard OCaml REPL:

let a = 43;;
val a : int = 43

#show a;;
val a : int
like image 199
Richard-Degenne Avatar answered Jan 29 '26 11:01

Richard-Degenne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!