Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the %A specifier in F#'s printf non-existent in OCaml's printf?

Tags:

f#

ocaml

In F#'s printf there is the format specifier %A which enables to pass in any F# type, and it would be evaluated and printed.

As an example:

type Result<'a> =
    | Failure
    | Success of 'a       

printf "%A" (Success "hello") // prints out 'Success "hello"'

Where, clearly, Result<'a> isn't a built-in type.

I can declare a similar type in OCaml, but there is no equivalent specifier for Printf.printf - instead, I would have to implement my own string_of_result function, and use the %s specifier in the format string. Moreover, since this is a polymorphic type, I would have to create a not-straightforward function that can handle any type instance of 'a.

My question is - why does OCaml lack this handy specifier? Is it because there is no incentive to implement it? Is it because there's some lacking under-the-hood mojo, which is just there in F#?

like image 760
asafc Avatar asked Apr 13 '17 16:04

asafc


People also ask

Why do we use F in print in C?

Example 1: C Output The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio. h header file using the #include <stdio.

What is %A in printf?

The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases. As an example, this code: printf("pi=%a\n", 3.14); prints: pi=0x1.91eb86p+1.

Why do we use F in Python?

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.

Why F is used with print?

In short, it is a way to format your string that is more readable and fast. The f or F in front of strings tell Python to look at the values inside {} and substitute them with the variables values if exists.


1 Answers

I'd say "lacking under-the-hood mojo" is probably the reason.

In F#, %A specifier defers printing to a reflection-based printer - it uses runtime type information to traverse and print the value. The reflection API used in that process is very much a .NET-specific thing. Also while handy, it's also a comparatively expensive mechanism - it shouldn't be used as a blanket specifier if you can use a more concrete one.

From what I know, OCaml doesn't have corresponding reflection capabilities that could be used here. Perhaps there's another mechanism that would let you implement a generic print - but I'm not familiar enough with OCaml internals to tell.

like image 55
scrwtp Avatar answered Sep 29 '22 11:09

scrwtp