Checking arguments for null in Nemerle
An impossible thing in languages like C# and F#:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public macro not_null(obj) | |
syntax ("not_null", obj) | |
{ | |
def var_name = $"$obj"; | |
<[ | |
when ($obj == null) | |
throw System.ArgumentNullException($var_name); | |
$obj; | |
]> | |
} | |
class Person | |
{ | |
public Name: string; | |
public this(name: string) | |
{ | |
Name = not_null name; | |
} | |
} | |
// causes ArgumentNullException exception with argument name set to "name"! | |
def person = Person(null); |
Comments