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?



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


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


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!


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.


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 :: .