← Back to lesson 06: Parse a number safely

Parse a number safely: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Trim a binary, turn it into an integer, and catch invalid input.

Run one Erlang parser with <<" 42 ">> and <<"not-a-number">>, then get {ok, 42} and {error, not_an_integer}.

Parse valid text and catch invalid text

string:trim/1 names a function in documentation. string:trim(Text) calls it with one argument.

Original full example
%% Bind a complete parser to ParseInteger
ParseInteger =
  fun(Text) ->
    try binary_to_integer(string:trim(Text)) of
      Number -> {ok, Number}
    catch
      error:badarg -> {error, not_an_integer}
    end
  end.

%% Run both paths
{ParseInteger(<<" 42 ">>), ParseInteger(<<"not-a-number">>)}.

The valid input is trimmed to <<"42">>, parsed, and returned as {ok, 42}.

The invalid input raises error:badarg inside the try expression.

The catch clause turns that error into {error, not_an_integer}.

Steps for the full example

  1. string:trim/1 and binary_to_integer/1 are name-and-arity forms used in documentation. Calls use parentheses and do not include /1.
  2. A successful integer reaches the of clause and becomes {ok, Number}.
  3. Invalid text raises error:badarg, so the matching catch clause returns {error, not_an_integer}.
  4. Both paths return tagged tuples that the next piece of code can match.

Original lesson notes

string:trim/1 removes whitespace from both ends of a text binary. binary_to_integer/1 turns clean integer text into an integer.

Invalid integer text makes binary_to_integer/1 raise the error reason badarg. A try ... of ... catch ... end expression can turn that expected error into data.

Bind the full anonymous function to ParseInteger. Valid and invalid calls then return tagged tuples instead of making the shell stop at an exception.

Symbols and terms
string:trim/1
The name-and-arity form used in documentation: module string, function trim, one argument.
binary_to_integer/1
A one-argument function that parses an integer binary or raises badarg.
fun(Text) -> ... end
Create a full anonymous function whose argument is called Text.
catch error:badarg ->
Handle only an error whose class is error and whose reason is badarg.
arity
The number of arguments a function takes. /1 says that string:trim/1 and binary_to_integer/1 each take one argument.
anonymous function
A function written with fun ... end and bound to a variable such as ParseInteger.
try/of/catch
Run an expression, handle its normal result after of, and handle a named error after catch.

Original full exercise

Complete SafeNumber so <<" 7 ">> returns {ok, 7} and <<"seven">> returns {error, not_an_integer}.

Exercise starter
SafeNumber =
  fun(Text) ->
    try ____(string:____(Text)) of
      Number -> {ok, Number}
    catch
      ____ -> {error, not_an_integer}
    end
  end.

{SafeNumber(<<" 7 ">>), SafeNumber(<<"seven">>)}.

The result should be {{ok, 7}, {error, not_an_integer}}.

Show hint

Use binary_to_integer, trim, and the catch pattern error:badarg.

Show original answer
Reference answer
%% Handle only the conversion error we expect
SafeNumber =
  fun(Text) ->
    try binary_to_integer(string:trim(Text)) of
      Number -> {ok, Number}
    catch
      error:badarg -> {error, not_an_integer}
    end
  end.

{SafeNumber(<<" 7 ">>), SafeNumber(<<"seven">>)}.
Why does the catch clause use error:badarg instead of _ : _?

The parser expects one conversion error. Matching only error:badarg keeps unrelated errors visible so they can be fixed.

Original lesson takeaways
Design trade-offs and language comparisons
DESIGN DECISION

Why this shape?

Why put /1 after a function name?

Choice

BEAM identifies an entry point by module, function name, and argument count. trim/1 means the trim function that takes one argument.

Cost and boundary

The same name with another argument count is another function. Default arguments generate arities at compile time; they do not make calls loosely sized.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
Methods can overload by parameter types and count.
What BEAM changes
BEAM first finds a name and arity, then clauses can match data shapes.
False friend
run/1 and run/2 are distinct entry points.

Python

Familiar starting point
One function often uses defaults or *args.
What BEAM changes
Arity is part of a function's identity and appears in exports and docs.
False friend
Leaving out an argument does not discover another arity automatically.

JavaScript

Familiar starting point
Functions often tolerate missing or extra arguments.
What BEAM changes
A BEAM call must find the exact arity.
False friend
&1 is a capture placeholder; it is not another spelling of /1.
Original course references