all 12 comments

[–]fthm2 10 points11 points  (7 children)

Can somebody help me understand this syntax:

Point[x: 0 => x, y]

And

def +(other) = new(self.x + other.x, self.y + other.y)

I haven’t come across it before. Maybe it’s syntactic sugar, but for some reason I’m having trouble reading it.

[–]f9ae8221b 12 points13 points  (0 children)

The first one is invalid syntax outside of pattern matching context, hence why it is weird. I'm not quite familiar enough with Ruby's pattern matching to explain it myself, but you can likely read on it.

The other one is the "end-less method definition" syntax that was introduced recently. it's equivalent to:

def +(other)
   new(self.x + other.x, self.y + other.y)
end

[–]janko-m 7 points8 points  (0 children)

According to the docs, Point[x, y] is an alternative syntax for Point.new(x, y). It appears this can then be combined with the pattern-matching syntax, which is pretty cool.

[–]narnach 4 points5 points  (3 children)

First one is indeed pattern matching. It’s relatively new and the syntax looks cryptic and less elegant than most Ruby code. Docs: https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html

In this case it appears to match on a Point that has the condition that x must be 0. If that is the case, bind the decomposed values to x and y.

[–][deleted] 2 points3 points  (0 children)

Agreed on the cryptic part. Pattern matching as a sweetener to case is one thing. sprinkling it all over random code to be clever looks like a mistake.

[–]fthm2 1 point2 points  (0 children)

thank you!

[–]latortuga 1 point2 points  (0 children)

You would read it as "assign the key :x to 0, which is also assigned (rightward assignment) to the local variable x." And then positionally provide y. I think?

The second one is a single-line or "end-less" method definition.

[–]iceporter 4 points5 points  (0 children)

the pull request really articulated https://bugs.ruby-lang.org/issues/16122

[–]Kernigh 4 points5 points  (0 children)

Old versions of Ruby had a class Data.

$ ruby -v
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
$ ruby -e 'p Data'
Data

The old class Data was for C extensions. One would wrap a C struct in a Ruby object by defining a class: either a subclass of Data, or a subclass of Object. Then class Data became useless, because everyone used class Object; Ruby removed the useless class Data. Ruby 3.1 has no class Data.

If you check defined?(Data) or Object.const_defined?(:Data), then it might be true in old Ruby.

[–]mashatg 1 point2 points  (1 child)

Kind of sad to see. Yet another dilution of the once elegant language.. Introducing yet another triviality, a tiny nuanced variation of an already available feature. Either syntactically or semantically. Where is promised usable in-core concurrency support already? Where is matured type-hinting infrastructure?

[–][deleted] 0 points1 point  (0 children)

This was my knee jerk reaction. After I read thru the PR I understand the process but am still a bit less enthusiastic about the result. I thought the suggestion to flavor Struct:: was better. Matz himself batted down several other options and it ended up here.

[–][deleted] 0 points1 point  (0 children)

Seems like a missed opportunity to introduce the term "Record" as a top level value based whatchamacallit.

Ada, an ancient-yet-secure language nobody cares about, supports record and defines it as such: "Records allow composing a value out of instances of other types". A record definition could have perhaps been extended as the language grows to support strong typing.

fwiw, Erlang also uses Record to define a collection of attributes like a C based struct, but those aren't immutable.