[erlang-questions] erlang sucks

Richard A. O'Keefe ok@REDACTED
Tue Mar 18 05:24:16 CET 2008


On 18 Mar 2008, at 1:35 pm, David Holz misunderstood:
>> f(Msg = #hello{}) -> ...
>>
>> is necessarily safe, whereas
>
> It may be "safe", but it doesn't abstract what "Hello" is.

This was presented as an example of using ABSTRACT PATTERNS, not  
RECORDS.
As such, it most definitely DOES abstract what "Hello" is

> It still relies on there being a record called "hello"

and DOESN'T rely on there being any record called "hello"

> and if the codegen decides to represent it in any other way, this  
> assumption breaks.  Hence the need to be able to defer guards to  
> external code.

and DOES allow the definition to be external.

You can very nearly think of abstract patterns as being the functions
you want, but constrained at the point of definition (and by the syntax)
so that they are safe to call, WITHOUT any runtime test, and pretty much
without any downside that I can see.

A note on implementation:
	#abspat(args)
in a clause head would be compiled as
	load current item into argument register
	normal function call
	failure address
	-- at this point function has left some stuff on the stack
	code to match those arguments
	...
failure address:
	look for next matching clause.

The function would exit in one of two ways:
	failure-return
		Return no results,
		Instead of returning to X, return to *X
	success-return N
		move the N arguments to a standard place (perhaps the argument
		registers)
		Instead of returing to X, return to X + failure address size

Note: it would be absurd, as in "what a dreadful waste of effort" for  
the
user-defined things allowed in patterns to merely return true or false.
They need to SUCCEED returning a bunch of parts or FAIL.
This is not just more efficient than returning 'true' or 'false' and  
then
going a bit Alzheimic and checking to see which it was, it is  
considerably
more expressive.

Here is an example.  Suppose we have a bunch of messages including
	{hello,From,_,_,_}
	{teenaa_koe,_,From,_}
	{nihao,_,_,From,_,_}
and we would like (a) to classify all these as 'hello' messages and (b)
recover the From field from them.
	#hello(From) -> {hello,From,_,_,_};
	#hello(From) -> {teenaa_koe,_,From,_};
	#hello(From) -> {nihao,_,_,From,_,_}.

	#hello() -> #hello(_).

Now we can not only write

	f(Msg = #hello()) -> ... From = hello_from(Msg) ...

but we can also write

	f(Msg = #hello(From)) -> ...

Abstract patterns not only do not rely on -record,
they were intended to replace it!





More information about the erlang-questions mailing list