Nope, printf is done with special macro system OCaml has just for format strings, you can see the compiler hooks here: github.com/ocaml/ocaml/blob/trunk/...
You cannot actually write printf-like function yourself, or customize the ones ocaml has, without either modifying the compiler, or using an external macro system like camlp4 (or I guess ppx now, I haven't touched that in years).
As for putting names in modules, this doesn't actually solve this issue, as when you want to pass S some_str to a polymorphic function, you'd need to know exact type of polymorphism it supports, like foo (SIF.S s) vs bar (SI.S s).
This isn't required by any theoretical reason, Haskell deals with this perfectly fine, and even SML had some limited type classes IIRC.
I'm not sure there's one standard OCaml style like with Python. I double checked it with OCaml source code, and it's full of ;;s. And tbh I'm not sure starting a lot of subsequent lines with let () = as it also sometimes does is really an improvement. I'll leave it to the readers to decide which one is more readable (first one from some tests in OCaml source):
let () = show (not_greater_equal 1.0 2.0)
let () = show (not_greater_equal 1.0 1.0)
let () = show (not_greater_equal 2.0 1.0)
let () = show (not_greater_equal 1.0 nan)
let () = print_line ()
vs
show (not_greater_equal 1.0 2.0);;
show (not_greater_equal 1.0 1.0);;
show (not_greater_equal 2.0 1.0);;
show (not_greater_equal 1.0 nan);;
print_line ();;
Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
Nope, printf is done with special macro system OCaml has just for format strings
Format strings are part of the equation, yes, but as the comment you pointed to notes, the printf functionality is defined in normal functions in the Printf/Scanf/Format modules. Format strings are just a syntax sugar; it's possible to desugar them to (something like) printf [Text "Hello, "; Format_s; Text "!\n"] name which would do the same job, just a little more verbosely.
This isn't required by any theoretical reason, Haskell deals with this perfectly fine, and even SML had some limited type classes IIRC.
Well, Haskell and OCaml explicitly are in very different design spaces here, and both have valid reasons for doing what they do. OCaml's more explicit style makes compilation faster and error messages simpler, for example. And SML doesn't have type classes, unless you're talking about the built-in non-extensible equality types.
I double checked it with OCaml source code, and it's full of ;;s
Yes, very old code still uses ;; but that has not been the convention for a long time now. For example, if you check the link you gave above, very little of that file is using ;;. And if you use ocamlformat, the standard code formatter, it will idiomatically remove ;;.
And tbh I'm not sure starting a lot of subsequent lines with let () = as it also sometimes does is really an improvement
Yes, we actually don't need to do that either. We can use ; which is the sequencing operator to do it pretty cleanly:
Nope, printf is done with special macro system OCaml has just for format strings, you can see the compiler hooks here: github.com/ocaml/ocaml/blob/trunk/...
You cannot actually write printf-like function yourself, or customize the ones ocaml has, without either modifying the compiler, or using an external macro system like camlp4 (or I guess ppx now, I haven't touched that in years).
As for putting names in modules, this doesn't actually solve this issue, as when you want to pass
S some_str
to a polymorphic function, you'd need to know exact type of polymorphism it supports, likefoo (SIF.S s)
vsbar (SI.S s)
.This isn't required by any theoretical reason, Haskell deals with this perfectly fine, and even SML had some limited type classes IIRC.
I'm not sure there's one standard OCaml style like with Python. I double checked it with OCaml source code, and it's full of
;;
s. And tbh I'm not sure starting a lot of subsequent lines withlet () =
as it also sometimes does is really an improvement. I'll leave it to the readers to decide which one is more readable (first one from some tests in OCaml source):vs
Format strings are part of the equation, yes, but as the comment you pointed to notes, the printf functionality is defined in normal functions in the
Printf
/Scanf
/Format
modules. Format strings are just a syntax sugar; it's possible to desugar them to (something like)printf [Text "Hello, "; Format_s; Text "!\n"] name
which would do the same job, just a little more verbosely.Well, Haskell and OCaml explicitly are in very different design spaces here, and both have valid reasons for doing what they do. OCaml's more explicit style makes compilation faster and error messages simpler, for example. And SML doesn't have type classes, unless you're talking about the built-in non-extensible equality types.
Yes, very old code still uses
;;
but that has not been the convention for a long time now. For example, if you check the link you gave above, very little of that file is using;;
. And if you useocamlformat
, the standard code formatter, it will idiomatically remove;;
.Yes, we actually don't need to do that either. We can use
;
which is the sequencing operator to do it pretty cleanly: