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

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement