Choking on Syntactic Sugar

Hakan Stenholm etxhste@REDACTED
Wed Mar 14 12:24:08 CET 2001


>From the documentation on Erlang Extensions since 4.4:
>
>  7.8 Literal string prefix in patterns
>  
>       This extension was added in Erlang 4.8 (OTP R5A). 
>  
>       A new construction is allowed in patterns, namely a literal
>       string as the first operand of the ++ operator. Example: 
>
>             f("prefix" ++ L) -> ...
>
>       This is syntactic sugar for the equivalent, but harder to read
>
>             f([$p,$r,$e,$f,$i,$x | L]) -> ...
>
>
>No other non-constant uses of `++' are allowed.

This is not quite true, any non-negative integer (not just 0-255) appears to
work just fine and in this case it should be possible to check at compile time.
I wrote a littel test program (OTP r7b01) to show this:

-module(test).

-export([
	 test/1
	]).

%% call test:test(String) to see that pattern match is done properly
test("foo" ++ R) -> ok;
test([101,101] ++ R) -> ok;    % "ee"
test([400,400] ++ R) -> ok;

%% test([a,b,c] ++ R) -> ok;     -> compile error
%% test([{1,2,3}] ++ R) -> ok;   -> compile error
%% test([_,_] ++ R) -> ok;       -> compile error
%% test([4.0,4.0] ++ R) -> ok;   -> compile error
%% test([-1,-1] ++ R) -> ok;     -> compile error

test([999999999999999999999999999999999] ++ R) -> ok;
test(_) -> not_ok.



Wouldn't it also be reasonable to generalize this by alowing the string argument
to be any kind of list of patterns (that might contain varibles and _) that is
known at compile time ?
Then again we don't gain anything from this (from what I can see) so there is no
apprent reason to do this ?!




More information about the erlang-questions mailing list