pufuwozu/roy — Issue #84: Unused variable marker in pattern match
Author: eegg
Created: 2011-12-15 22:59:51+00:00
State: open

A la Haskell, writing the Roy

```haskell
data Maybe a = Nothing | Just a
data Tree a = Nil | Tree (Tree a) a (Tree a)
let rootValue t = match t
case Nil = Nothing
case (Tree _ v _) = Just v
```

should produce JS in which no new variables are introduced for the left and right subtree in the second case of the pattern match. Instead, it treats `_` as a variable name, producing

```javascript
var Nothing = function(){};
var Just = function(a){this._0 = a};
var Nil = function(){};
var Tree = function(Tree, a, Tree){this._0 = Tree;this._1 = a;this._2 = Tree};
var rootValue = function(t) {
return (function() {
if(t instanceof Nil) {
return new Nothing();
} else if(t instanceof Tree) {
var _ = t._0;
var v = t._1;
var _ = t._2;
return new Just(v);
}
})();
}
```

It's a bit unfortunate that the underscore is a valid JS variable name, so we'd want another character for this in Roy. Relatively unobtrusive keyboard characters that are not valid variable names include `-`, `~`, and `.`

The `_` should also be usable on its own to accept any pattern at the end of a list of matches.