[erlang-questions] Why can't you have a variable before a string with pattern matching?

Darius Bacon <>
Thu May 31 22:11:24 CEST 2007


Javier =?iso-8859-1?q?Par=EDs?= <> wrote:
> El Thursday 31 May 2007 19:37:14 Jordan Wilberding escribió:
> > Hello,
> >
> > I was just wondering why having
> >
> > fcn("text" ++ String) -> ..
> >
> > is legal, but doing
> >
> > fcn(String ++ "text")
> >
> > is not legal. Is this for performance reasons, or something else?
> 
> This is an speculation, but it's probably because it's easy for the compiler 
> to see that the first pattern is time-bounded, while the second is not 
> (because it depends on the lenght of the string).

Matching already can take that much time, e.g. 
  fcn(A, A) -> ...

However, the second pattern above requires allocating new structures
on the heap, while the first one shares structure with the argument. I
don't know if that's the actual rationale. Anyway, trying to make ++
fully general in patterns would open up possibilities like
fcn(A ++ "x" ++ B).

Darius



More information about the erlang-questions mailing list