Project

General

Profile

Feature #16095

2 Features: remove (simplify) 'new' keyword and Property Shorthand

Added by D1mon (Dim F) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:94254]

Description

common use:

class A
  def initialize(arg1, arg2)
    @arg1 = arg1
    @arg2 = arg2
  end
end
A.new(1,2)

1) feature: remove 'new' keyword:

A(1,2) # shorter and more comfortable

2) feature: Property Shorthand (like ES6)

class A
  def initialize(arg1, arg2)
    # shorter and more comfortable
    @arg1, @arg2
    # or this:
    @arg1
    @arg2
  end
end

So as not to duplicate the code (words). May need to do some other syntax or character (:arg1, ^arg1, %arg1, ...)

can also be applied to other types.

Hash example:

a = 1
b = 2
h = {a:a,b:b} # common use
h = {a,b} # new syntax or {%a,%b} - any syntax of your choice

Related issues

Related to Ruby master - Feature #11105: ES6-like hash literalsRejectedActions

History

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

D1mon (Dim F) wrote:

1) feature: remove 'new' keyword:

A(1,2) # shorter and more comfortable

new is a method, not a keyword. You can already get what you want:

module Kernel
  private

  def A(arg1, arg2)
    A.new(arg1, arg2)
  end
end

Adding constructor methods to Kernel for every class would bloat the method namespace. There are also already cases where a Kernel method exists with the same name as a core class but has different behavior (e.g. Kernel#Hash vs. Hash.new), so this change could not be backwards compatible.

2) feature: Property Shorthand (like ES6)

Already requested in #5825 and #15192.

can also be applied to other types.

Hash example:

a = 1
b = 2
h = {a:a,b:b} # common use
h = {a,b} # new syntax or {%a,%b} - any syntax of your choice

Already requested in #15236.

Updated by shevegen (Robert A. Heiler) about 1 month ago

The net-gain from removing .new would be quite minimal. I also think it is less
readable too, if .new were removed - but even well aside from this, this seems to
be backwards incompatible and may have to require a long transition time if it is
possible to add in the first place.

I am also not sure whether matz would want to have this - IMO there are too many
drawbacks associated with the change, with the net benefit of writing shorter
code being a very minimal advantage, if it is one at all. There is also the issue
of functions/methods that could be capitalized such as Integer(); I don't think
we should have to consider whether this is a method, or instead Integer.new,
so personally I am against omitting ".new"; I think .new is fine, and it is
four characters - that can not be that much. (Four in the sense of A(1,2)
versus A.new(1,2) - there are four characters more in the latter).

Property Shorthand was discussed before; matz responded back then. I believe
what he wrote back then still applies. (I don't remember offhand but I think
one comment from matz was to wait and see; I think that was in regards to
javascript and how they use their shorthand variant.)

It may be better to add to the discussion at #15192 if you feel strongly about
shorthand syntax.

As for other syntax such as:

:arg1, ^arg1, %arg1, ...)

I think some of them are problematic. First one is symbol - I think people would
not expect a symbol to behave as a shortcut syntax in this context. But either way
I think it is best to discuss this at #15192, IMO.

#3

Updated by znz (Kazuhiro NISHIYAMA) about 1 month ago

Updated by D1mon (Dim F) about 1 month ago

Feature 1: I agree that you can do anything with the help of metaprogramming, and this will have to be registered in each library. Therefore, I propose to do this at the language level (Ruby core).

Updated by D1mon (Dim F) about 1 month ago

I correctly understood that this function will be added in version 2.7? https://github.com/ruby/ruby/pull/2231/commits/7da3cdc9aa132307eff0e4376ad6a3819940fc2d

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

First, please do not mix requests for irrelevant features in one ticket.

D1mon (Dim F) wrote:

I correctly understood that this function will be added in version 2.7? https://github.com/ruby/ruby/pull/2231/commits/7da3cdc9aa132307eff0e4376ad6a3819940fc2d

It's a just pull-request.
Everyone can request favorite features, but not all will be merged.

Updated by D1mon (Dim F) about 1 month ago

figured out how to do it:

%P[arg1 arg2]

in def initialize:

def initialize(arg1, arg2)
  %P[arg1 arg2] # equals to: @arg1 = arg1, @arg2 = arg2
end

in hash:

a = 1
b = 2
h = %p[a b] # equals to: h = {a: a, b: b}

what do you say ???

#8

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variables added
#9

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of Feature #5825: Sweet instance var assignment in the object initializer added
#10

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of deleted (Feature #5825: Sweet instance var assignment in the object initializer)
#11

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of deleted (Feature #15192: Introduce a new "shortcut assigning" syntax to convenient setup instance variables)
#12

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of Bug #7522: Non-core "Type()" Kernel methods return new objects added
#13

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Is duplicate of deleted (Bug #7522: Non-core "Type()" Kernel methods return new objects)

Also available in: Atom PDF