[erlang-questions] Bit Syntax and compiler Erlang/OTP R21.1

Valentin Micic v@REDACTED
Tue Aug 20 20:44:02 CEST 2019


> On 20 Aug 2019, at 18:55, empro2@REDACTED wrote:
> 
> On Tue, 20 Aug 2019 17:04:26 +0200
> Valentin Micic <v@REDACTED> wrote:
> 
>> (tsdb_1_1@REDACTED)448> is_binary( <<0:1>> ).
>> 
>> false
> 
> "is elephant?" is encoded in Erlang as: is_bitstring/1

I see…  nevertheless, I prefer is_duck/1, but thank you :-)

> 
> 75> is_bitstring(<<0:1>>).
> is_bitstring(<<0:1>>).
> true
> 
> 
>> But, if you put two elephants next to each other, you get
>> — a duck!
>> 
>> is_binary( <<0:5, 0:3>> ). 
>> 
>> true
> 
> Read "is_binary/1" as
> "is_bitstring/1 and (bitlength_of(Elephants) mod 8 == 0)"
> 
> :-)
> 
>> Given all this, why would anyone find bitstrings useful?
> 
> That is why the default bitstring is a bytestring or
> octetstring or binary:
> 
> 98> is_binary(<<"blah">>) and is_binary(<<1, 2, 3>>).
> is_binary(<<"blah">>) and is_binary(<<1, 2, 3>>).
> true
> 
> As long as you do not tell the compiler to take bites of
> memory that are no bytes, with that ":", the two of you seem
> to agree there.
> 
> Might help when extracting flag bits? (or when talking to
> some 9-bit byte PDP ... or was that 11? ... ;-) Anyway I
> would rather deprecate and obsolete away dot notation for
> record elements …
> 

Yes, you could use bit-syntax to extract flags, however, I doubt this will ever be presented to Erlang as a stream of bits (well, at least not nowadays) — unless, of course,  Erlang runs on top of the underlying hardware  that natively supports 9-bit bytes, of course. And how many of these are available to us today?

By contrast, the record dot notation is just a syntactic sugar, not a data type. You could chose not to use it (I never do, so no love lost if, or when they remove it). However, a defective usage of record dot syntax may not accidentally introduce a semantic problem, as compiler would definitely help you with identifying a problematic syntax (well,  I know, strictly speaking this is not quite true, but go with it, as this is true for record syntax in general, but not to dot notation as such).  Unlike situation I’ve described, where I have accidentally introduced a semantic, but not a syntax error (for the reasons Frank explained). 

Oh, please, don’t get me wrong, I like the ability to write <<World:2, Group:2, User:2, _:2>> = <<SomeFlags>>.
However, with this construct, you could still extract your flags, but this would not require any new data type (such as bitstring) —  e.g. is_binary(<<World:2, Group:2, User:2, _:2>> =  <<SomeFlags>>)  will merrily report true.

Contrast this with <<World:2, Group:2, User:2>> = Some6BitBitstring;

which, In order to work, must have Some6BitBitstring set to exactly a six-bit Bitstring, which could only be set from within Erlang runtime, thus fairly artificial construct with respect to the environment, e.g. OS, socket, files, etc.

Thus, my assertion is that bitstring is a pretty pointless data type. There.

> /Michael
> 
> -- 
> 
> Normality is merely a question of quantity,
> not of quality.
> 

But, according to Hegelian dialectics, quantity transforms into quality and vice-versa :-)

> 
> 
> 
> 
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190820/3401d7b3/attachment.htm>


More information about the erlang-questions mailing list