Keyword Lists

Keyword lists are typically used in the context of options passed to functions.

 defmodule​ Canvas ​do
 
  @defaults [ ​fg:​ ​"​​black"​, ​bg:​ ​"​​white"​, ​font:​ ​"​​Merriweather"​ ]
 
 def​ draw_text(text, options \\ []) ​do
  options = Keyword.merge(@defaults, options)
  IO.puts ​"​​Drawing text ​​#{​inspect(text)​}​​"
  IO.puts ​"​​Foreground: ​​#{​options[​:fg​]​}​​"
  IO.puts ​"​​Background: ​​#{​Keyword.get(options, ​:bg​)​}​​"
  IO.puts ​"​​Font: ​​#{​Keyword.get(options, ​:font​)​}​​"
  IO.puts ​"​​Pattern: ​​#{​Keyword.get(options, ​:pattern​, ​"​​solid"​)​}​​"
  IO.puts ​"​​Style: ​​#{​inspect Keyword.get_values(options, ​:style​)​}​​"
 end
 
 end
 
 Canvas.draw_text(​"​​hello"​, ​

Get Programming Elixir 1.2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.