Compound data type: Difference between revisions

Content added Content deleted
(→‎{{header|D}}: add crystal implementation with a link to the language reference)
Line 516: Line 516:
CL-USER> (point-y b)
CL-USER> (point-y b)
3</lang>
3</lang>

=={{header|Crystal}}==
Crystal's structs work very similarly to objects, but are allocated on the stack instead of the heap, and passed by value instead of by reference. More potential caveats are noted in the [https://crystal-lang.org/reference/syntax_and_semantics/structs.html language reference].

<lang ruby>struct TwoTuple(T)
getter fst : T
getter snd : T
def initialize(@fst, @snd)
end
end

puts TwoTuple(Int32|String).new 42, "foo" #=> TwoTuple(Int32 | String)(@fst=42, @snd="foo")</lang>


=={{header|D}}==
=={{header|D}}==