Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Initialize immutable struct - shorthand

Writing each attribute name 3 times is quite annoying, luckily Crystal provides a shorthand writing mode.

struct Person
  def initialize(@name : String, @email : String)
    @time = Time.utc
  end
end

foo = Person.new("Foo", "me@foo.bar")
p! foo
foo # => Person(@name="Foo", @email="me@foo.bar", @time=2021-07-11 06:25:04.796465667 UTC)