[erlang-questions] Re: What atom name to represent a null value

Masklinn masklinn@REDACTED
Sat Feb 27 21:54:02 CET 2010


On 27 Feb 2010, at 20:48 , Joe Armstrong wrote:
> 
> No no no .... ^ 100
> 
> For three reasons.
> 
> a) Think types. The type of
> 
>> foo(1) -> 2;
>> foo(2) -> 3;
>> foo(3) -> 1.
> 
> is   int() -> int()
> 
> But the type of
> 
>> foo(1) -> 2;
>> foo(2) -> 3;
>> foo(3) -> 1.
>> foo(_) -> undefined.
> 
> is int() -> int() | 'undefined'  (which is a crazy type)
> 
Actually, it corresponds to the very simple Haskell type
(Integer -> Maybe Integer). There's nothing crazy about it if it's what 
is needed by the application

> b) foo(4) has NO VALUE. but if you say foo(4() -> undefined. Then foo(4)
> HAS a value (namely undefined).
> 
> The best way to express this in Erlang is as follows:
> 
> foo(1) -> 2;
> foo(2) -> 3;
> foo(3) -> 1;
> foo(X) -> exit({ebadArgToFoo, X}).
> 
> This expresses the required relation that foo is oof type int()  -> int()
> and that evaluation foo(4) is illegal and raises and exeception.
> 
> The the point of exit - it was designed for *exactly* this situation.
> 
Well not if Tim doesn't want his function to blow up, which was the use case he
considered.

> Now any good erlang programmer would not write foo like this, they would write
> 
> foo(1) -> 2;
> foo(2) -> 3;
> foo(3) -> 1.
> 
> And *nothing* else - since evaluating foo(4) will raise an exception and the
> default exception will be sufficient to identify the error.
> 
Unless, of course, you don't want an error.

> c ) There is also a worse problem. If you use the following:
> 
>> foo(1) -> 2;
>> foo(2) -> 3;
>> foo(3) -> 1.
>> foo(_) -> undefined.
> 
> Then calling foo(4) will not cause the program to crash *immediately* but
> somewhat later. For example, suppose you write this:
> 
Or it won't cause the program to crash at all because at some point when
the result is needed it will be matched against `undefined` and a specific
operation will be performed based on that knowledge.

I'm on Robert's side in this case: whether or not this "pattern" should be
applied depends on the application and the semantic meanings of the function
call.


More information about the erlang-questions mailing list