5 ms·
The benefit I see is that (at least in Erlang) pattern matching combines pre-condition asserts with programming the sunny path. For example, "/" ++ X = Htt
by mbucc 11y ago
The benefit I see is that (at least in Erlang) pattern matching combines pre-condition asserts with programming the sunny path. For example,
"/" ++ X = HttpRequest:get(path).
will crash if PATH_INFO doesn't include leading slash. Writing just the sunny path and getting asserts "for free" (without any clutter) is a fun way to program.
I also find that statement very readable.
BTW, here's some Java code that does the same thing:
String Path;
if (HttpRequest.getPathInfo().startsWith("/")) {
Path = HttpRequest.getPathInfo().substring[1:];
} else {
String emsg = "no match of right hand side value \"%s \"";
throw new IllegalStateException(emsg.format(HttpRequest.getPathInfo());
}