Message from discussion
Binary match in function head doesn't compile
Received: by 10.204.129.9 with SMTP id m9mr5261577bks.1.1351301711628;
Fri, 26 Oct 2012 18:35:11 -0700 (PDT)
X-BeenThere: erlang-programming@googlegroups.com
Received: by 10.204.0.70 with SMTP id 6ls5149600bka.2.gmail; Fri, 26 Oct 2012
18:35:11 -0700 (PDT)
Received: by 10.204.145.140 with SMTP id d12mr5262973bkv.6.1351301711027;
Fri, 26 Oct 2012 18:35:11 -0700 (PDT)
Received: by 10.204.145.140 with SMTP id d12mr5262972bkv.6.1351301711010;
Fri, 26 Oct 2012 18:35:11 -0700 (PDT)
Return-Path: <erlang-questions-boun...@erlang.org>
Received: from hades.cslab.ericsson.net (hades.cslab.ericsson.net. [192.121.151.104])
by gmr-mx.google.com with ESMTP id v13si312935bkw.0.2012.10.26.18.35.10;
Fri, 26 Oct 2012 18:35:10 -0700 (PDT)
Received-SPF: pass (google.com: domain of erlang-questions-boun...@erlang.org designates 192.121.151.104 as permitted sender) client-ip=192.121.151.104;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of erlang-questions-boun...@erlang.org designates 192.121.151.104 as permitted sender) smtp.mail=erlang-questions-boun...@erlang.org
Received: from hades.cslab.ericsson.net (hades [192.121.151.104])
by hades.cslab.ericsson.net (Postfix) with ESMTP id DCA2C5C05B;
Sat, 27 Oct 2012 03:35:01 +0200 (CEST)
X-Original-To: erlang-questi...@erlang.org
Delivered-To: erlang-questi...@erlang.org
Received: from mail-gw1.its.unimelb.edu.au (mail-gw1.its.unimelb.edu.au
[128.250.5.150])
by hades.cslab.ericsson.net (Postfix) with ESMTP id 878915C008
for <erlang-questi...@erlang.org>; Sat, 27 Oct 2012 03:34:58 +0200 (CEST)
Received: from old-delphi.eng.unimelb.edu.au (old-delphi.eng.unimelb.edu.au
[128.250.70.61])
by mail-gw1.its.unimelb.edu.au (Postfix) with ESMTP id 090ABF5A;
Sat, 27 Oct 2012 12:34:53 +1100 (EST)
Received: by old-delphi (Postfix, from userid 5001)
id 8441B42C129; Sat, 27 Oct 2012 12:34:53 +1100 (EST)
X-Spam-Checker-Version: SpamAssassin 3.1.7-deb3 (2006-10-05) on
old-delphi.eng.unimelb.edu.au
X-Spam-Level:
X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham
version=3.1.7-deb3
Received: from mulga.csse.unimelb.edu.au (mulga.csse.unimelb.edu.au
[128.250.1.22]) by old-delphi (Postfix) with ESMTP id AD43742C0B4;
Sat, 27 Oct 2012 12:34:52 +1100 (EST)
Received: from localhost (localhost [127.0.0.1]) by mulga.csse.unimelb.edu.au
with ESMTP id q9R1Ypj2005875; Sat, 27 Oct 2012 12:34:52 +1100 (EST)
Received: from mulga.csse.unimelb.edu.au ([127.0.0.1])
by localhost (mulga.csse.unimelb.edu.au [127.0.0.1]) (amavisd-new, port 10024)
with LMTP id dJdDaKacUpjW; Sat, 27 Oct 2012 12:34:51 +1100 (EST)
Received: from mulga.csse.unimelb.edu.au (localhost [127.0.0.1]) by
mulga.csse.unimelb.edu.au with ESMTP
id q9R1YeAP005725; Sat, 27 Oct 2012 12:34:40 +1100 (EST)
Received: (from jws@localhost)
by mulga.csse.unimelb.edu.au (8.13.8+Sun/8.13.8/Submit) id q9R1YdCY005701;
Sat, 27 Oct 2012 12:34:39 +1100 (EST)
Date: Sat, 27 Oct 2012 12:34:39 +1100
From: Jeff Schultz <j...@csse.unimelb.edu.au>
To: Erik Pearson <e...@defunweb.com>
Message-ID: <20121027013439.GA15...@mulga.csse.unimelb.edu.au>
References: <CA+dr1pvhSwe2ZVwVREdzFH1c2yPVuVqRUfNx=_xaS24D-4C...@mail.gmail.com>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <CA+dr1pvhSwe2ZVwVREdzFH1c2yPVuVqRUfNx=_xaS24D-4C...@mail.gmail.com>
User-Agent: Mutt/1.5.16 (2007-06-09)
Cc: erlang-questions Questions <erlang-questi...@erlang.org>
Subject: Re: [erlang-questions] Binary match in function head doesn't compile
X-BeenThere: erlang-questi...@erlang.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: General Erlang/OTP discussions <erlang-questions.erlang.org>
List-Unsubscribe: <http://erlang.org/mailman/options/erlang-questions>,
<mailto:erlang-questions-requ...@erlang.org?subject=unsubscribe>
List-Archive: <http://erlang.org/pipermail/erlang-questions>
List-Post: <mailto:erlang-questi...@erlang.org>
List-Help: <mailto:erlang-questions-requ...@erlang.org?subject=help>
List-Subscribe: <http://erlang.org/mailman/listinfo/erlang-questions>,
<mailto:erlang-questions-requ...@erlang.org?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: erlang-questions-boun...@erlang.org
Sender: erlang-questions-boun...@erlang.org
On Fri, Oct 26, 2012 at 11:27:06AM -0700, Erik Pearson wrote:
> test(<<Field:Len/binary, Rest/binary>>, Len) ->
> {Len, Field, Rest}.
> does not compile, complaining that "variable 'Len' is unbound", while this
Short answer: Erlang isn't a general constraint solver.
> test(<<Field:2/binary, Rest/binary>>, Field) ->
> {Field, Rest}.
> does. For some reason the compiler doesn't see the Len from the match spec
> in the arguments, but it does see Field. Is that by design?
That depends on what you mean by "see." It actually treats that clause
as if it were something like
test(<<Field:2/binary, Rest/binary>>, Arg2),
Field = Arg2
->
{Field, Rest}.
(Which is not Erlang, but I hope you get the idea.)
And, of course, your problem clause looks like
test(<<Field:Len/binary, Rest/binary>>, Arg2),
Len = Arg2
->
{Len, Field, Rest}.
which makes clear why the compiler sees Len as unbound when used in
the pattern match.
It's obviously easy to re-arrange the data-flow to make this sort of
thing compilable. I wouldn't expect to run across a need for it in
practice though.
Jeff Schultz
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions