
OCaml Modules: Think Immutable Singletons - eatonphil
http://blog.eatonphil.com/2015/05/24/ocaml-modules-think-immutable-singletons/
======
bad_user
One problem is that it first starts with a Person class, then it says _nope,
modules are not like this_. So it increases the cognitive load for absolutely
no reason.

Another problem is that an immutable singleton could have interesting
properties, like it could have a type, it could be polymorphic (e.g. think
Scala's objects), but this article presents Ocaml modules to be nothing more
than namespaces. So first of all, that's not true and furthermore, I don't see
the usefulness in that compared to any other language that can do namespaces.
When reading about some construct of a language I don't know, I want to see
how that construct is different or useful for me.

As I'm writing this, the article has 42 points and I'm left wondering why.
I've long suspected that HN votes articles by keywords happening in the
titles, without reading the articles.

~~~
mercurial
> As I'm writing this, the article has 42 points and I'm left wondering why.
> I've long suspected that HN votes articles by keywords happening in the
> titles, without reading the articles.

I haven't read the article, but it would be great if whoever submits an
article also had to enter one or more questions (with yes/no answers or one-
word unambiguous answers) about it. Then, upon submitting a vote for the
article, HN would prompt users to answer them. A suggestion to read the fine
article would be given to people who cannot answer, and no upvote would
happen.

------
hbbio
Hacker News, circa 2015.

Someone writes a blog post, using JavaScript to explain how modules are used
in OCaml. Just in the case it changed during the last 20 years or so.

Edit: I'm glad more people learn about OCaml. A few years ago, I kept hearing
that no one will contribute to my open source OCaml projects...

~~~
Avshalom
Well... yeah. What do you want, it's trying to explain by analogy. If it used
OCaml it'd be "ocaml modules are simple, they're just like ocaml modules"
which is... unhelpful. They could have used C but then they'd have to
implement a singleton pattern in C first at which point thats like a dozen
things to explain.

------
lindig
The OCaml code for the Person module in that post is not not idiomatic. Also,
a module is quite different from a class or JS object and it would be more
helpful to think about a module implementing an (abstract) data type like in C
or other procedural languages. There is no inheritance with modules and no
dynamic dispatch. Once compiled, there is no selection of functions/methods at
runtime.

~~~
anonymousDan
So how does an ocaml module compare to a Haskell type class?

~~~
lindig
OCaml modules and Haskell type classes are quite separate, too. I would make
more sense to compare OCaml and Haskell modules. Haskell type classes
implement ad-hoc polymorphism and OCaml doesn't have that at all. You can't
implement in OCaml something like a show function that works on integers and
strings and list of integers and so on like in Haskell using the Show type
class.

~~~
DonPellegrino
You sort of can with functors or GADTs.

~~~
lindig
Like you say, sort of. This would imply that you wrap all the basic types in a
sum type. It's quite heavy. Using the PPX mechanism the compiler can also be
extended to generate this code automatically but again, this isn't very
natural at the moment.

------
tel
This is pretty wacky. I cannot help but feel that it would be far better to
just learn OCaml modules without this metaphor. It seems too far from
meaningful.

~~~
smosher_
I agree. The title is downright bizarre until you realize the article is
consumed more by JS idiosyncrasies than about the OCaml modules.

~~~
tel
Well, it certainly spent effort explaining JS oddities, but it's ostensibly
about OCaml modules. So, it's either stream of consciousness and dependent on
commentary to intuit meaning or merely failed in its goal.

------
woah
Is JavaScript becoming a lingua franca, used to explain other completely
unrelated languages?

~~~
Arcsech
Even if any given developer doesn't use it as a primary development language,
then as something they need a little bit of to do something in a browser, so
I'd bet most programmers have at least seen it. And even if you've never seen
Javascript before, it's superficially similar to C++/Java/C# so you can kind
of make sense of it in simple examples like this.

------
runn1ng
What I get from this article is that OCaml modules are sort of like browserify
modules or ECMAScript 6 modules.

Cool... I guess?

~~~
ufo
ES6 modules aren't higher order AFAIK

