June 2022
Intermediate to advanced
130 pages
2h 45m
English
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> |
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 ...
Read now
Unlock full access