Structs Are Restricted Maps
A struct is a specific kind of map. Where a map can have any combination of keys, every struct that conforms to a type has a specific list of keys.
Structs are ideal for holding metadata for a type. Let’s put structs into practice to build an SVG graphic.
We’re going to create an SVG graphic representation that we’ll eventually put in a text file that looks like this:
| <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> |
| <polygon points="100,100 150,25 150,75 200,0" |
| fill="none" stroke="black" /> |
| </svg> |
defstruct Defines a Struct
Let’s switch back to our graphics project. Open up a file called shape.ex and key this in:
| defmodule Shape do |
| defstruct([:points, :stroke, :fill]) |
| end |
We’ve defined ...
Get Programmer Passport: Elixir 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.