Compound data type: Difference between revisions

Content added Content deleted
(→‎{{header|D}}: add crystal implementation with a link to the language reference)
m (→‎{{header|Crystal}}: i missed that the task specifically calls for a Point data type)
Line 520: Line 520:
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].
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)
<lang ruby>struct Point(T)
getter fst : T
getter x : T
getter snd : T
getter y : T
def initialize(@fst, @snd)
def initialize(@x, @y)
end
end
end
end


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


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