News for hackers. Stuff that matters.
Re:WASM_of_OCaml by tome on Monday July 24, 2023 @12:54PM (#36850953)
I think it's a great convention. It means you write

    i = int_of_string s
where the "i" is next to the "int" and the "s" is next to the "string".

    i = string_to_int s
just looks backwards to me.
Re:WASM_of_OCaml by esrauch on Monday July 24, 2023 @01:33PM (#36851497)
The topic here isn't so much the order but whether "of" in int_of_string matches a common/typical usage of the word.

From the other comments sounds like its local style for OCaml maybe because of some French lineage, but that "int_of_string" is a function that converts a string into an int was definitely not something that I would have assumed (as opposed to 'int_from_string' or something).

Re:WASM_of_OCaml by IshKebab on Monday July 24, 2023 @02:25PM (#36852277)
It doesn't sound especially weird to me and I'm not French.

It's just "[create an] int [out] of [a] string"

"from" would have made more sense.

Re:WASM_of_OCaml by esrauch on Monday July 24, 2023 @02:32PM (#36852382)
The words in brackets are doing all of the heavy lifting in your example though: "Of" without "out of" means "constituted by". If in isolation I saw "array_of_string" I'd definitely parse that as somehow being about str[], not "f(str) -> char[]"
Re:WASM_of_OCaml by tome on Monday July 24, 2023 @02:48PM (#36852599)
Well, if the word order is the most important aspect (as it is to me) then you have to think of another word you can use instead of "of". I know that in my early Haskell programming I was writing `intOfString` where other Haskellers wrote `stringToInt`. I'm a native English speaker so it certainly had nothing to do with French! "Of" just seemed to fit naturally there. Do you think "From" or some other English word would be more natural?
Re:WASM_of_OCaml by marcosdumay on Monday July 24, 2023 @03:05PM (#36852849)
The Haskell culture clearly dictates "i = fromString s".

And as a consequence, Rust has it standardized as "i = from s".

Re:WASM_of_OCaml by tome on Monday July 24, 2023 @04:05PM (#36853478)
What do you do if you want to disambiguate the return type? I suppose `intFromString` works. I'm not sure what I chose `intOfString`. It's not because I'm French!
Re:WASM_of_OCaml by marcosdumay on Monday July 24, 2023 @09:02PM (#36856440)
You would declare the type of the returning value:

(i :: Int) = fromString s

Although once in a while it's useful to make a short synonym for disambiguating the type, x_of_y is certainly not a short synonym, so it's not commonly used.

Re:WASM_of_OCaml by abecedarius on Monday July 24, 2023 @08:16PM (#36856045)
Better yet, compare

    f = glork_to_floob (crumb_to_glork c)
versus

    f = floob_of_glork (glork_of_crumb c)
and picture the first style inside a more complex function with more constructors/conversions.
Re:WASM_of_OCaml by tome on Wednesday July 26, 2023 @06:28AM (#36875382)
Yes, even better example, thanks! In Haskell you get

    f = floob_of_glork . glork_of_crumb . crumb_of_zing
which is much better than

    f = glork_to_floob . crumb_to_glork . zing_to_crumb
Re:WASM_of_OCaml by nequo on Wednesday July 26, 2023 @07:19AM (#36875825)
Interesting. The pipe operator is more idiomatic in OCaml where this would be

  crumb_of_zing x |> glork_of_crumb |> floob_of_glork
versus

  zing_to_crumb x |> crumb_to_glork |> glork_to_floob
Re:WASM_of_OCaml by abecedarius on Wednesday July 26, 2023 @09:14AM (#36877113)
Yeah, fair. Forth especially really commits to that left-to-right order in its general style -- I like it when a language and its culture are conscious about readability in this way.
Re:WASM_of_OCaml by tome on Wednesday July 26, 2023 @09:26AM (#36877294)
It is interesting that the "of" style is common in Ocaml where the left-to-right style is common. They seem to clash!

I would like to know what it's like to work in a fully left-to-right language, for example, to define `my_floob`:

      let my_crumb |> zing_to_crumb |> crumb_to_glork |> glork_to_floob = my_floob
Re:WASM_of_OCaml by nequo on Wednesday July 26, 2023 @10:28AM (#36878315)
In addition to Forth, R can do this too:

  x |> zing_to_crumb() |> crumb_to_glork() |> glork_to_floob() -> my_floob
But it is not idiomatic.