ML differences - vilinski/nemerle GitHub Wiki

This page explains how to do things common in ML-like languages in Nemerle. It's just a set of hints.

Table of Contents

Single-option datatype

You can sometimes see code like this in OCaml

<ocaml>type height = Height of int let print_height (Height h) = print_int h </ocaml>

The obvious translation of this code to Nemerle is:

variant height {
  | Height { val : int; }
}

def print_height (_) {
  | Height (h) => print_int (h)
}

As this is lengthy and not very efficient, you should use a plain class, or better yet, a struct (which is like class, but is passed by value):

[Record]
struct height {
  public val : int;
}

def print_height (h) { print_int (h.val) }

In case you want to use pattern matching on such values, it's also possible:

[Record]
struct mypair {
  public x : int;
  public y : int;
}

def (a, b) = mypair (3, 4);

This works because Nemerle treats tuple patterns as record patterns, when a record pattern is expected.

Local polymorphic functions

They are supported, but their type needs to be specified explicitly (types of global functions always need to be specified explicitly, even if they are monomorphic).

Oh well, at least it allows for polymorphic recursion :-)

Partial application and using operators as first class values

In Nemerle you cannot supply 2 parameters to 3 parameter function and get a single parameter function back. What you can do instead, is to use partial application operator _, there is some decription on the new features page.

Also note that OCaml's (+) (or SML's op+) translates to _ + _.

Huge types

Unlike in ML languages the exact types of values can be accessed at runtime. Therefore you should avoid types growing with your data (at least faster than linearly).

⚠️ **GitHub.com Fallback** ⚠️