alamba78

alamba78

Double Colon operator

I’m getting back into learning Elixir after a couple weeks off and for the life of me I can’t remember (or maybe never came across) what the double colon :: operator is used for.

I’m reading The Little Elixir and OTP Guidebook and there is an example in Chapter 2 using it (without an explanation or introduction to the operator). Here is the snippet of code from the example:

<< “TAG”, title :: binary-size(30),
artist :: binary-size(30),
album :: binary-size(30),
year :: binary-size(4),
_rest :: binary >> = id3_tag

Can someone explain the use of the :: operator?

Thanks!

Marked As Solved

alamba78

alamba78

Wow, thanks for the link to the documentation and your explanation. Everyone that replied has really helped out and I’m sure many more people have gained an understanding of the use of :: .

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

The double colon (::) has two uses. Both could be subscribed as “being of type”:

  1. In binaries, it is used to explain what size(bit/byte/multiple bytes) an element in the binary is. An example, taken from the Parsing UDP with Binary Pattern Matching blog post:
    <<
      _header        :: size(240), # 30 bytes * 8 = 240 bits
      priority_code  :: bitstring-size(8),
      agent_number   :: little-unsigned-integer-size(32),
      message        :: bitstring-size(320)
    >> = data

When pattern matching with binaries, a size has to be specified for all but the last element to-be-matched element.

2.In specifying what return type a function has, when writing @spec, @type or @callback attributes:

@spec fibonacci(int) :: int
@type num_or_str :: number | string
@callback read_description(Entity.t) :: iodata

Here it is in the docs.

14
Post #5
jimm

jimm

It’s not an operator. Think of it as saying “is of size” So title
is-of-size 30 bits.

gregvaughn

gregvaughn

It looks like that is binary pattern matching, so the value after the double colon specifies the size of the value before it.

alamba78

alamba78

Ok, that makes sense when looking at the code. I wonder if I came across this before, maybe in Introducing Elixir or Programming Elixir 1.2 . Thus far, I’ve not come across an explanation in The Little Elixir and OTP Guidebook. And I had a heck of a time trying to search for it on Google. You guys rock!

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement