Initialize immutable struct - shorthand


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

examples/struct/initialize_struct_shorthand.cr
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)