Project

General

Profile

Feature #12578

Instance Variables Assigned In parameters ( ala Crystal? )

Added by lankford (Matt Lankford) over 1 year ago. Updated 14 days ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:76329]

Description

When I first saw the instance assignments in Crystal, I was floored... I have looked at all 18 pages in this tracker and thought it "must" have been mentioned in here... but I don't see it... pardon me if I missed it...

BUT

so in Crystal, this :

class MyStuff

  def initialize @thing_one , @thing_two
  end

end

would replace :

class MyStuff

  def initialize thing_one , thing_two
    @thing_one = thing_one
    @thing_two = thing_two
  end

end

One of the things I loved about ruby was attr_accessor / reader / writer...

I know in Crystal, it is a macro, but still at the millions of lines of boilerplate that would go away...

Has no one thought of stealing this from Crystal or is there a good reason this does not exist? ( and I will go sit down and color now )


Related issues

Is duplicate of Ruby trunk - Feature #5825: Sweet instance var assignment in the object initializerAssigned
Is duplicate of Ruby trunk - Feature #8563: Instance variable argumentsRejected
Is duplicate of Ruby trunk - Feature #12023: Allow ivars to be used as method argumentsOpen
Has duplicate Ruby trunk - Feature #12820: Shorter syntax for assigning a method argument to an instance variableRejected

History

#1 Updated by hsbt (Hiroshi SHIBATA) over 1 year ago

  • Assignee deleted (ruby-core)

#2 [ruby-core:76332] Updated by duerst (Martin Dürst) over 1 year ago

I personally think this syntax looks quite good. I wonder whether it should be limited to initialize or not.

Matt Lankford wrote:

I have looked at all 18 pages in this tracker and thought it "must" have been mentioned in here... but I don't see it... pardon me if I missed it...

Can you link some of these pages with "related issues"?

#4 Updated by nobu (Nobuyoshi Nakada) over 1 year ago

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

#5 Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Is duplicate of Feature #8563: Instance variable arguments added

#6 Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Is duplicate of Feature #12023: Allow ivars to be used as method arguments added

#7 [ruby-core:76334] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Description updated (diff)

#8 [ruby-core:76338] Updated by shevegen (Robert A. Heiler) over 1 year ago

I like the idea behind it.

I am not sure about the syntax itself. Would this break existing code? What are the constraints how it is used?

#9 [ruby-core:76352] Updated by lankford (Matt Lankford) over 1 year ago

Matthew Kerwin wrote:

Here is some history of this proposal:

You sir, are a much better "looker" than I ;)

well done!

#10 [ruby-core:76434] Updated by matz (Yukihiro Matsumoto) over 1 year ago

  • Status changed from Open to Feedback

Arguments are giving names to passed values, which is different from attribute (instance variables) initialization. I think they should be separated clearly. And for most of the cases, only initialize needs this kind of initialization.

My idea is https://bugs.ruby-lang.org/issues/8563#note-3

Matz.

#11 [ruby-core:77147] Updated by jwmittag (Jörg W Mittag) over 1 year ago

By the way, just as a historical note: up to and including Ruby 1.8.7, you could actually do this:

define_method(:initialize) {|@thing_one, @thing_two|}

I have never actually seen this, though, which you can take as an indication of the usefulness of your suggestion.

#12 Updated by nobu (Nobuyoshi Nakada) about 1 year ago

  • Has duplicate Feature #12820: Shorter syntax for assigning a method argument to an instance variable added

#13 Updated by matz (Yukihiro Matsumoto) 14 days ago

  • Status changed from Feedback to Rejected

My opinion has not been changed from https://bugs.ruby-lang.org/issues/8563#note-3
I am strongly against code like

def initialize(@foo, @bar)
end

I don't care about define_attr_initialize not being "the solution".

Matz.

Also available in: Atom PDF