Victor Shepelev (Zverok) has just landed an extremely useful feature in Ruby, Data.define
. You can find the merge here:
https://github.com/ruby/ruby/pull/6353
...and the Ruby discussion here:
https://bugs.ruby-lang.org/issues/16122
So what is it and what does it do? Well that's what we're going to take a look into.
What it Does
Succinctly put Data.define
creates an immutable Struct
-like type which can be initialized with either positional or keyword arguments:
Point = Data.define(:x, :y)
origin = Point.new(0, 0)
north_of_origin = Point.new(x: 0, y: 10)
Note: This does not inherit from
Struct
...and because the API supports pattern matching you can very much do things like this still:
case origin
in Point[x: 0 => x, y] # rightward assignment, pattern matching
Point.new(x:, y: y + 5) # shorthand hash syntax
else
origin
end
It can also take a block for creating additional methods if you'd like, much like Struct
:
Point = Data.define(:x, :y) do
def +(other) = new(self.x + other.x, self.y + other.y)
end
Then Why Not Struct?
You can absolutely use Struct
still. Given that why should you use Data
instead?
Because it's stricter, and the values it produces are immutable. With the advent of more functional patterns in Ruby this can be a very useful thing.
Consider data passing in Ractors which requires immutable state, this becomes incredibly useful for value objects and message passing for what may inevitably be our next generation of Puma and other web servers.
Why not dry-rb?
Interestingly the dry-rb
folks are very pragmatic about things like this. In fact some of their core folks are already talking about how to wrap this new feature into their immutable structs to build on top of it, rather than in parallel:
https://twitter.com/solnic29a/status/1577683251086376963
They did the same with pattern matching, and it's great to see how we build on top of what the language formally adopts.
What Do You Think of It?
Personally? I like it. I plan to use it a lot in the REPL whenever I need a quick data type I can match against without pulling out a full Struct
for it as often times I don't intend to mutate it and the keyword_init
flag can be a bit pesky to remember.
It reminds me a lot of case classes from Scala:
case class Book(isbn: String)
val frankenstein = Book("978-0486282114")
...and of data classes from Kotlin:
data class User(val name: String = "", val age: Int = 0)
So this is not a novel concept, but a useful one for Ruby to adopt.
Closing Thoughts
While the new language features have certainly slowed, in accordance with Matz's focus on tooling, we're still seeing some interesting movement in the language core. Of course there's a lot of fascinating tooling too, and I'm thrilled to see those coming to fruition, and perhaps soon I'll write about them as well.
In the mean time thanks to Zverok for seeing this through, and everyone who participated!
Top comments (9)
This
Data.define
stolen from Crystal. The key different with Struct is:Struct is just a conveniently way to create a class, it is reference, create on heap.
Data.define is a value, create on stack.
I love it, I was excited to see this alternative implementation of Struct. However, you noted in this article that the
Data
class inherits fromStruct
, whereas I understood from the PR that it inherits fromObject
. How did you find that out? The docs also seem to indicate thatData
is an alternative implementation:Yeah, and doing a call to
ancestors
shows that it doesn't inherit fromStruct
Article amended to reflect that. In the past it was going to loosely inherit it so I must have missed that fork
In the source code, it is written in the struct.c file - there is some relation
Based on this post, I was playing around with Data, thinking that it would be a good fit for
Ractor
. The idea that Data is immutable is sound, however the reality is that it may be misleading, as to fully gain advantage ofRactor
communication, the object needs to be fully immutable (or deeply immutable). So by defaultData
instances will still be copied to each instance ofRactor
, unlessRactor.make_shareable
was called to deep freeze the data instance.It would be useful to have a
with
method to clone an instance with new data (like Sorbet's T::Struct): bugs.ruby-lang.org/issues/19000The only thing that bugs me is that is that there's no way to set default values.
You can override initialize method to set default values.
docs.ruby-lang.org/en/3.2/Data.htm...
hope it helps.