[erlang-questions] "prefix" ++ Rest = Something
Raimo Niskanen
raimo+erlang-questions@REDACTED
Wed Oct 25 13:53:09 CEST 2017
On Wed, Oct 25, 2017 at 12:54:59PM +0200, Pierre Fenoll wrote:
> It's the same with binaries:
>
> <<"a", Rest/binary>> = <<"abc">>
> %%=> Rest = <<"bc">>
>
> a() -> <<"a">>
> Prefix = <<(a())/binary>>
> PrefixSize = byte_size(Prefix) %% Compilation fails if size is not provided
> <<Prefix:PrefixSize/binary, Rest/binary>> = <<"abc">>
> %%=> Rest = <<"bc">>
The only time an unspecified size is allowed in the bit syntax is for the
last field in a bit expression.
>
> Not sure what the differences may be due to compilation v. in the REPL.
>
> I never understood however why this was "just sugar" or why PrefixSize is
> explicitly needed:
> in
> f(Prefix) when is_list(Prefix) ->
> Prefix ++ Rest = "my_list_thing",
> Rest.
> why isn't the compiler able to generate a pattern match? Is it missing
> some concept, some structures?
> What are the missing pieces and what can be done to add them?
"abc" ++ Rest
is compiled to
[$a, $b, $c | Rest]
The variable Prefix is a complete list hence there is only a pointer to the
head in runtime, so the only way to append to it is via lists:append/2 i.e
the actual operator erlang:'++'/2, which creates a new list. So this can
not be described as a pattern.
The visual appearence of "abc"++Rest vs Prefix++Rest is misleading:
[$a, $b, $c | Rest] vs lists:append(Prefix, Rest)
>
> Same question with binaries:
> since we have a "prefix-match of binaries only providing PrefixSize at
> runtime" instruction,
> * Why don't we have one for lists?
> * And oh God why do we have to provide that PrefixSize "manually"?
> (binding the variable ourselves, when the compiler could do that itself)
> * Why isn't suffix-matching of binaries implemented yet? (how
> different to prefix-matching can it be?)
>
> I could never find the answer to all of these questions.
> WRT binaries my thinking is that they are actually a mix of "bytes" and
> references to binaries,
> making some crucial operations O(log n) instead of O(1)... but prefix match
> exists...
>
> I really want to be able to write things like:
> <<"{", Name/binary, "}">> = PathToken
>
> %%=> Name = <<"id">> given PathToken = <<"{id}">>
>
>
>
> Cheers,
> --
> Pierre Fenoll
>
>
> On 25 October 2017 at 09:27, Kostis Sagonas <kostis@REDACTED> wrote:
>
> > On 10/25/2017 08:56 AM, Stefan Hellkvist wrote:
> >
> >> Is it because this syntactic sugar is transformed more or less as a
> >> preprocessing step where the value of Prefix needs to be known,
> >>
> >
> > Yes, that's the reason. It's a purely _syntactic_ thing.
> >
> > Kostis
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list