4 ms·
Why foo(X) -> foo1(X). foo1(bar) -> void; foo1(_Else) -> undefined. rather than just foo(bar) -> void; foo (_Else) -> undefined. ?
by lukasb 11y ago
Why
foo(X) -> foo1(X).
foo1(bar) -> void;
foo1(_Else) -> undefined.
rather than just
foo(bar) -> void;
foo (_Else) -> undefined.
?
- mml 11y agofoo(_Else). -> undefined. Doesn't seem very erlangy. I'd expect a runtime error, as someone's passed foo() the wrong value.
- kevinwang 11y agoBut in the given example, isn't foo(_Else) = foo1(_Else) = undefined anyways?
- simoncion 11y ago> Doesn't seem very erlangy. I'd expect a runtime error, as someone's passed foo() the wrong value. It depends. Sometimes you want to crash when handed an unexpected value. Sometimes you expect that you'll be handed unexpected values and need to either fail more gracefully, print a warning and then fail, or warn and keep going, or whatever. Erlang gives you a bunch of tools, and they all have their place. [0] :) [0] With the possible exception of the if statement... which is of questionable utility.
- asabil 11y agoThe "if" keyword should have been "when" instead. It's useful in some cases, but rarely used.
- jcizzle 11y agoIt is just a simple example to convey that a portion of foo/1 contains a case statement, and that case statement is being replaced with a function. There could be more lines in foo/1. The point is that both the case statement and the multi-clause function are identical in behavior, but the function prevents nesting at the call site.
- rdtsc 11y agoTo illustrate a point of how the two are equivalent and one could almost write an automatic translator. But yeah it ended up confusing many here. Also the _Else part I would just make it into foo(_) -> undefined.