Saturday, July 14, 2012

Checking arguments for null in Nemerle: revisited

It's turned out that Nemerle has full fledged design-by-contract feature! Let's see how the code from the previous post can be rewritten:

It's really both elegant and powerful compared to hand-written "if (arg == null) throw new ArgumentNullException(...)" or using the Microsoft contracts library.

Contracts may be bond right to arguments themselves:

Which causes: "Nemerle.Core.AssertionException: assertion ``name != null && name.Length > 0'' failed in file Main.n, line 107: The ``Requires'' contract of parameter `name' ha s been violated." Amazing! :)

However, if checking for null value is what's only needed, there's one more option: the NotNull macro attribute:

No comments: