News for hackers. Stuff that matters.
Re:WASM_of_OCaml by Athas on Monday July 24, 2023 @12:26PM (#36850500)
Sorry in advance for bikeshedding. This is a cool project and the name is clearly derived from js_of_ocaml, but does anyone know why js_of_ocaml ended up with such an awkward name in the first place? I am vaguely familiar with the "x_of_y" naming scheme in OCaml (which I also find awkward, but perhaps it sounds better in French) - is that the only reason? I can't find any discussion online, so maybe I'm really the only one who finds the name awkward.
Re:WASM_of_OCaml by frou_dh on Monday July 24, 2023 @12:31PM (#36850590)
It's just a dodgy French to English translation which then because a convention. Instead of e.g. to_string's buddy being called from_string, it was called of_string.

Here's a thread of reddit about it: https://www.reddit.com/r/ocaml/comments/6es88t/what_does_the...

Re:WASM_of_OCaml by nequo on Monday July 24, 2023 @12:41PM (#36850747)
I assume it also has a connection to how algebraic data types are written in OCaml. For example, the equivalent of Haskell's `Maybe` and Rust's `Option` is

  type 'a option = None | Some of 'a
So calling this project "WASM of OCaml" is one way of saying that it "wraps" OCaml code in WASM.
Re:WASM_of_OCaml by giraffe_lady on Monday July 24, 2023 @01:33PM (#36851508)
Right but why do they use "of" in that way for type declarations? Ends up being the same answer: it's a reasonable translation from french for a non-native english speaker.
Re:WASM_of_OCaml by wk_end on Monday July 24, 2023 @01:55PM (#36851812)
...except that the original ML uses the same syntax, and it's Scottish.
Re:WASM_of_OCaml by giraffe_lady on Monday July 24, 2023 @02:06PM (#36851974)
Oh interesting, nevermind then. This makes me question the french explanation for the other thing too then.
Re:WASM_of_OCaml by c54 on Monday July 24, 2023 @03:11PM (#36852911)
It's fairly colloquial english to say you have "some of something", I think this matches the ocaml language convention pretty nicely
Re:WASM_of_OCaml by dan-robertson on Monday July 24, 2023 @06:40PM (#36855248)
It’s somewhat hard for me to decide how unnatural a translation this is as I’m used to seeing it in ocaml. My concise dictionary doesn’t have a definition that fits this use well. My non-concise dictionary describes the history of the senses of the word as “exceedingly complex”.

It has been used as a translation of the French de since the 11th century and I think it gets some senses (eg derivation) from that. With a verb meaning create, this ocaml construction is fine in modern English (eg ‘make a string of an int’) and dates back a long way (OED offers a source from 893; this is sense I. [VII.] 20. a. in the second edition). So I think partly the question is whether omitting make in make_string_of_int (or String.make_of_int) is an acceptable abbreviation in programming.

Re:WASM_of_OCaml by baruchel on Monday July 24, 2023 @12:32PM (#36850599)
It doesn't sound better in french. Any reasonable french version should be translated as "from".
Re:WASM_of_OCaml by arthurcolle on Monday July 24, 2023 @12:47PM (#36850852)
String: ficelle

Rope (advanced string): corde

Thread: fil

So string from object would like ficelle d'objet which is yeah, string from object. But the "of" thing in Ocaml is well documented. I vaguely remember them when I had an Ocaml project in my coursework.

Re:WASM_of_OCaml by brmgb on Monday July 24, 2023 @03:29PM (#36853107)
No, “de” in French has the same meaning as “of” and sounds exactly as weird here. That’s clearly not where this comes from. Also the translation of string with this meaning is “chaîne de caractères”.
Re:WASM_of_OCaml by dan-robertson on Monday July 24, 2023 @06:44PM (#36855287)
Is «chaîne de caractères» what French programmers call strings? (That would surprise me much as English-speaking programmers calling them ‘strings of characters’ would)
Re:WASM_of_OCaml by dawnofdusk on Monday July 24, 2023 @08:57PM (#36856396)
Yeah it's the French word. You can always say just "chaîne" when it's unambiguous. Or you can just use the English word
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.
Re:WASM_of_OCaml by brennvin on Monday July 24, 2023 @01:10PM (#36851177)
I assumed it was meant to evoke William of Occam.
Re:WASM_of_OCaml by progval on Monday July 24, 2023 @01:36PM (#36851539)
I am French, but I feel this matches definitions 2a and 3 on https://www.merriam-webster.com/dictionary/of :

* "used as a function word to indicate origin or derivation, eg. a man of noble birth" (js code comes from ocaml code)

* "used as a function word to indicate the component material, parts, or elements or the contents, eg. throne of gold / cup of water" (js application made of ocaml code)

Re:WASM_of_OCaml by hajile on Monday July 24, 2023 @02:40PM (#36852492)
"Cup of water" isn't a great example because it would imply then cup was made from water. This makes more sense when you realize "cup of water" is lazy speech for "cup full of water".

"of" usually implies a state of being while "from" implies a transformation.

"JS of Ocaml" implies that the JS is Ocaml, but that's not true as it is transformed. "JS from Ocaml" would be more accurate.

Re:WASM_of_OCaml by afiori on Wednesday July 26, 2023 @03:26PM (#36883490)
The of in Cup of water act as a normal genitive. It communicates that the cup relates to water and the only reasonable relation between a cup and an edible liquid is that the cup contains the liquid
Re:WASM_of_OCaml by l_dopa on Monday July 24, 2023 @02:18PM (#36852167)
I always assumed this convention was just using "of" in the same sense as function composition. From wikipedia:

> The notation g ∘ f is read as "g of f "...

Re:WASM_of_OCaml by Foobar8568 on Monday July 24, 2023 @04:00PM (#36853440)
Considering how math heavy is OCaml and how formal is/were French mathematics, especially post high school, this is most likely the reason.
Re:WASM_of_OCaml by baby on Monday July 24, 2023 @02:23PM (#36852239)
I believe this is because in French you tend to say "x of y".
Re:WASM_of_OCaml by Tommah on Monday July 24, 2023 @03:22PM (#36853016)
It is named like the <x>_of_<y> functions, yes. "Of" and "from" are both "de" in French. A native English speaker would use "from" there, but whoever named those functions used "of."
Re:WASM_of_OCaml by devmunchies on Monday July 24, 2023 @04:07PM (#36853504)
Yeah most likely just french translation. A more accurate translation would have been "from" in this context.

`js_from_ocaml`, `string_from_int`, etc would have been clearer in english.

If i didn't speak Spanish it would bother me a lot more.

Re:WASM_of_OCaml by dgan on Monday July 24, 2023 @07:33PM (#36855691)
Even the standard library isn't consistent on that. There are plenty of "Something.to_string"

But "standard library" never was a strong point of OCaml anyway so I guess meh shrugs