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.

Where Next?

Popular in Questions 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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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 I...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement