by mrkeen · 6y ago · 0 comments
The only way to make it the norm is to make it the norm.
Maybe someone sees this...
type Expr =
| Const of int
| CustomOneArg of (int -> int) * Expr
| CustomTwoArgs of (int -> int -> int) * Expr * Expr
...instead of this... interface Expr {
}
class Const extends Expr {
public Integer val;
public (Integer val) {
this.val = val;
}
}
class CustomOneArg extends Expr {
public Function<Integer, Integer> fun;
public int val;
public (Function<Integer, Integer> fun, Integer val) {
this.fun = fun;
this.val = val;
}
}
class CustomTwoArgs extends Expr {
public BiFunction<Integer, Integer, Integer> fun;
public Integer valA;
public Integer valB;
public (BiFunction<Integer, Integer, Integer> fun, Integer valA, Integer valB) {
this.fun = fun;
this.valA = valA;
this.valA = valB;
}
}
... and thinks "I should look into this more".That said, I don't want to put words into the author's mouth, but this particular article looks more "by FPers for FPers" than FP advocacy.
Many programmers have used these techniques even outside FP and would be able to understand it one concept at a time - they aren't advanced concepts to me. It's more that it isn't "easy reading"; but it seems its for an internal audience with assumed background knowledge.
The example you gave is similar to a discriminated union in ML but it is not as robust because the language will not check that every match expression (probably an if-instanceof-ladder in Java) handles every case.