[erlang-questions] "prefix" ++ Rest = Something

Pierre Fenoll pierrefenoll@REDACTED
Wed Oct 25 12:54:59 CEST 2017


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">>

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?

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171025/971c3f9f/attachment.htm>


More information about the erlang-questions mailing list