Pattern Matching Against Records as Tuples in Erlang (Elixir)

elixir, erlang, pattern-matching, record, tuples

Solution

Cesarini and Thompson say in their book Erlang Programming to never rely on the fact that records are implemented as tuples, since that implementation could change. See the accepted answer to this question.

Problem

Is it a bad practice to pattern match against record as tuples in Erlang (or Elixir)? Sample code: Assume we have defined this module: ``` defmodule Ween do defrecord TimeSpot, at: nil, tag: nil def proper?({ _, _, "home"} = v), do: { true, "succeeded with: #{inspect v}" } def proper?(other), do: { false, "failed with: #{inspect other}" } def sample, do: TimeSpot.new(tag: "home") end ``` If we define a test as follow: ``` defmodule WeenTest do use ExUnit.Case require Ween test "records are tuples" do case Ween.proper?(Ween.sample) do { true, v } -> IO.puts v assert(true) { false, v } -> IO.puts v assert(false) end end end ``` It will succeed. Edit 1: @parroty The reason that here pattern matching against tuples tested is to simulate some "sort of" duck typing for records; the ultimate thing I wished for, should look like this: ``` defmodule Ween do defrecord TimeSpot, at: nil, tag: nil def proper?([tag: "home"] = v), do: { true, "succeeded with: #{inspect v}" } def proper?([tag: "1234"] = v), do: { true, "succeeded with: #{inspect v}" } def proper?(other), do: { false, "failed with: #{inspect other}" } def sample, do: TimeSpot.new(tag: "home") end ``` So every record with a 'tag' field baring a "home" value would match the first clause. Of-course it seems there would be some performance penalties there.

Original source