Initialize immutable struct
We can initialize some of the attributes by values received from the user and some attributes by generating the value ourselves. e.g. by using a Time object, a Random value or generating or computing it in any other way.
We can print the content of the Struct, but we have no way to access the attributes and no way to change them. Hence this struct is immutable.
- There is no way to change this struct
- There is no way to access the individual attributes as there are no getters
struct Person def initialize(name : String, email : String) @name = name @email = email @time = Time.utc end end foo = Person.new("Foo", "email@example.com") p! foo # p! foo.name # Error: undefined method 'name' for Person
foo # => Person(@name="Foo", @email="firstname.lastname@example.org", @time=2021-07-11 06:22:05.981166766 UTC)