Project

General

Profile

Actions

Feature #22404

open

Make Data (and/or Struct, etc.) true "first-class citizens" and thank you!

Feature #22404: Make Data (and/or Struct, etc.) true "first-class citizens" and thank you!

Added by asoffa (Aaron Soffa) about 4 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:126919]

Description

First of all, I would like to give a huge shoutout to the Ruby community for staying true to Ruby's mission of increasing developer productivity and thereby happiness and thereby enrichment of people's lives, especially mine in so many ways! THANK YOU!

As a scientist (who likes data), I have ultimately so far discovered only this (relatively minor!) nitpick about the Ruby programming language: data/value objects are not "first-class citizens" for constants in the way that general modules and classes are.

The current solution is to write something like

D = Data.define(:x, :y) do
  self::D_CONSTANT = 1
end

where the self:: is required to keep D_CONSTANT in the scope of D here.

Alternatively, we can write the following:

D = Data.define(:x, :y)
class D
  D_CONSTANT = 1
end

which reopens the D class to keep D_CONSTANT scoped to D or otherwise

class D < Data.define(:x, :y)
  D_CONSTANT = 1
end

which creates an anonymous class (which is not generally desired).

My proposal is for something like the following:

class D = Data.define(:x, :y)
  D_CONSTANT = 1
end

to avoid the above complications.

This enhancement request may or may not make sense depending on how frequently Data (and/or Struct, etc.) are to be considered "first-class citizens" in Ruby.

My thought is that the = in the class definition here very nicely dovetails with the < for creating subclasses and also with the relatively new = syntax now allowed for method definitions (e.g. def f = 1).

This enhancement would be analogous to allowing upgrading

C = Class.new do
  self::C_CONSTANT = 1
end

to

class C
  C_CONSTANT = 1
end

for Data, Struct, etc. and would address the last points mentioned in [1].

[1] https://bugs.ruby-lang.org/issues/16122


Related issues 1 (0 open — 1 closed)

Is duplicate of Ruby - Feature #20993: Allow `class <constant-path> = <expression>` syntaxRejectedActions

Updated by mame (Yusuke Endoh) about 1 hour ago Actions #1

  • Is duplicate of Feature #20993: Allow `class <constant-path> = <expression>` syntax added
Actions

Also available in: PDF Atom