newref /

Filename Size Date modified Message
51 B
264 B
1.2 KB
133 B
512 B

<!-- -- markdown -- --> <!-- vim: set syntax=mkd: --> == Problem ==

OCaml uses ! before a variable to show that it is mutable (called a ref):

let s = ref true in
if !s then print_endline "s is true"

This is all nice and dandy if you have not been programming in any other language, but can be a pain if you are used to C, C++, Java, Javascript or any other language where ! means "not". It makes the difference between reading "s is true" or "s is not true" in the above example.

Often, you are even stuck writing both C and OCaml at the same time, leaving you confused in both cases.

== Solution ==

Using the awesomeness called camlp4, I've written an alternative syntax for refs, namely the dollar sign. The above example will then become:

let s = ref true in
if $s then print_endline "s is true"

Using this syntax will let you program in OCaml without going insane. (Well, not because of this aspect of the language, anyway)

== About camlp4 ==

I hope that this can serve as a minimal example of camlp4 as well. Since this is my first attempt at it, those few lines are probably riddled with bugs. Let me know if you find any!

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.