An "is_string" BIF?

Joe Armstrong (AL/EAB) <>
Thu Mar 10 10:37:37 CET 2005



> -----Original Message-----
> From: 
> [mailto:]On Behalf Of Thomas Johnsson
> XA (LN/EAB)
> Sent: den 10 mars 2005 09:22
> To: 
> Subject: RE: An "is_string" BIF?
> 
> 
> This really exposes an ugly wart on the language, in that 
> one cannot use any expression in a guard, making it impossible
> to use abstractions there, e.g.
> 
> glorbgraph(Graph) when mygraph:curiousproperty(Graph) -> ...
> 

It's not an ugly wart - it's deliberate.

Guards are merely extensions of pattern matching - they must be side effect free
and capably of efficient compilation.

If you allowed user defined guards people would soon write things like this:

guard(a, P) -> P ! foo, false;
guard(b, _) -> true.

and use them

foo(X, Y) when guard(X, Y) ->
	....
foo(X, Y) ->	
	...

evaluating foo(a, P) would cause code in the second clause to trigger,
but a side effect started in the first clause would also happen.

The only way of forbidding this is either to delay any side effects in
the user clause (ie delay Pid ! foo until the value of the guard 
is true) or restrict guards to side-effect free code.

Guard tests are also inlined in the pattern matching algorithms
in what should be an optimal manner.
 
/Joe



 -- Thomas
> 
> 
> 
> -----Original Message-----
> From: 
> [mailto:]On Behalf Of Corrado Santoro
> Sent: den 10 mars 2005 09:02
> To: 
> Subject: An "is_string" BIF?
> 
> 
> Dear all,
> 
> I need an "is_string" function, something that behaves as follows:
> 
> is_string ("ABC") -> true
> is_string ([65,66,67]) -> true
> is_string (['1','2',three]) -> false
> ...
> 
> I know that I could do that by defining a suitable function, 
> but I would 
> like to use it in a guard. Is there a sort of BIF?
> 
> Cheers,
> --Corrado
> 
> -- 
> 



More information about the erlang-questions mailing list