String Concat Pattern Matching In Elixir
This is a neat pattern matching trick in elixir, its best explained with a simple example:
invoice_message = "You owe $34"
"You owe " <> dollar_amount = invoice
IO.inspect(dollar_amount)
# => "$34"
With a slightly different situation, It may seem like you could do this:
invoice_message = "You owe 34 dollars"
"You owe " <> dollar_amount <> " dollars"= invoice
IO.inspect(dollar_amount)
# => ** (ArgumentError) the left argument of <> operator inside
# a match should always be a literal binary because its size can't be verified. Got: dollar_amount
But sadly you'll need to use regex to do that because elixir will throw an error.
Tweet