Skip to main content

W0050 - Avoid using the size/1 BIF

Warning

-module(main).
-export([size/1]).

-type input() :: tuple() | binary().

-spec size(input()) -> non_neg_integer().
size(Input) ->
erlang:size(Input).
%% ^^^^^^^^^^^ 💡 warning: Avoid using the `size/1` BIF

Explanation

The size/1 BIF can be used to return the size tuples, binaries and bitstrings.

However, this BIF is not optimized by the JIT; it may report inaccurate values for bitstrings, and its use can result in worse types for dialyzer.

What to do instead:

  • For tuples, use tuple_size/1.

  • For binaries, use byte_size/1.

  • For bitstrings, use byte_size/1, but please notice that while byte_size/1 rounds up to the nearest byte, size/1 would round down:

4 = byte_size(<<0:25>>).
3 = size(<<0:25>>).

For more information, see the Erlang common caveats.