ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments
@ 2013-06-22 19:30 sawa (Tsuyoshi Sawada)
  2013-06-23  0:41 ` [ruby-core:55598] [ruby-trunk - Feature #8563][Assigned] " nobu (Nobuyoshi Nakada)
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2013-06-22 19:30 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563

Author: sawa (Tsuyoshi Sawada)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55598] [ruby-trunk - Feature #8563][Assigned] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
@ 2013-06-23  0:41 ` nobu (Nobuyoshi Nakada)
  2013-06-23  6:24 ` [ruby-core:55600] [ruby-trunk - Feature #8563] " phluid61 (Matthew Kerwin)
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2013-06-23  0:41 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by nobu (Nobuyoshi Nakada).

Category set to syntax
Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)
Target version set to Next Major


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40087

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55600] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
  2013-06-23  0:41 ` [ruby-core:55598] [ruby-trunk - Feature #8563][Assigned] " nobu (Nobuyoshi Nakada)
@ 2013-06-23  6:24 ` phluid61 (Matthew Kerwin)
  2013-06-23 11:34 ` [ruby-core:55601] " matz (Yukihiro Matsumoto)
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: phluid61 (Matthew Kerwin) @ 2013-06-23  6:24 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by phluid61 (Matthew Kerwin).


=begin
Question: would this be valid?

  def foo(@foo=@foo) end

Asking here, because #5825 as written only talks about initialize method, and the above construct wouldn't make sense there.
=end

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40089

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55601] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
  2013-06-23  0:41 ` [ruby-core:55598] [ruby-trunk - Feature #8563][Assigned] " nobu (Nobuyoshi Nakada)
  2013-06-23  6:24 ` [ruby-core:55600] [ruby-trunk - Feature #8563] " phluid61 (Matthew Kerwin)
@ 2013-06-23 11:34 ` matz (Yukihiro Matsumoto)
  2013-06-23 13:21 ` [ruby-core:55602] " sawa (Tsuyoshi Sawada)
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2013-06-23 11:34 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by matz (Yukihiro Matsumoto).


From my POV:

  def initialize(@foo, @bar)
  end

does not express intention of instance variable initialization.  I'd rather add a method like

  define_attr_initialize(:foo, :bar)

to define a method of

  def initialize(foo, bar)
    @foo = foo
    @bar = bar
  end

Matz.

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40090

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55602] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (2 preceding siblings ...)
  2013-06-23 11:34 ` [ruby-core:55601] " matz (Yukihiro Matsumoto)
@ 2013-06-23 13:21 ` sawa (Tsuyoshi Sawada)
  2013-06-23 13:37 ` [ruby-core:55603] " nobu (Nobuyoshi Nakada)
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2013-06-23 13:21 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by sawa (Tsuyoshi Sawada).


It could also be used besides `initialize`:

    def update_something foo
       do_update_something(@foo = foo)
       ...
    end

would become

    def update_something @foo
       do_update_something(@foo)
       ...
    end
----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40091

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55603] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (3 preceding siblings ...)
  2013-06-23 13:21 ` [ruby-core:55602] " sawa (Tsuyoshi Sawada)
@ 2013-06-23 13:37 ` nobu (Nobuyoshi Nakada)
  2013-06-23 16:29 ` [ruby-core:55604] " headius (Charles Nutter)
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2013-06-23 13:37 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by nobu (Nobuyoshi Nakada).


phluid61 (Matthew Kerwin) wrote:
> Question: would this be valid?
> 
>   def foo(@foo=@foo) end

In generic,

  foo = foo

is valid always.
----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40092

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55604] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (4 preceding siblings ...)
  2013-06-23 13:37 ` [ruby-core:55603] " nobu (Nobuyoshi Nakada)
@ 2013-06-23 16:29 ` headius (Charles Nutter)
  2013-06-23 16:30 ` [ruby-core:55605] " boris_stitnicky (Boris Stitnicky)
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: headius (Charles Nutter) @ 2013-06-23 16:29 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by headius (Charles Nutter).


Worth pointing out that blocks used to support this:

1.times { |@foo| ... }

Basically, it supported anything you can have on the LHS of a normal assignment:

foo.bar { |a, @b, @@c, D, e.val, f[0]| ... } 

I believe it was taken out in 1.9 because it made argument processing a lot more complicated, but then 1.9 added the ability to do multiple-assignment grouping, default values, and other masgn features to all argument lists anyway. It doesn't seem like it would be too terribly complicated to add this back in, but I wonder about the OTHER reasons that non-local variable assignment was removed from argument lists in the first place.

As for the utility of the feature, I'd like it but I can live without it. It does seem rather un-Ruby to have to declare locals and do the assignment when all you want is to set an instance variable...especially when you have a lot of instance variables.

Note also that for a trivial initialize, where the only line is a multiple-assignment to set instance variables, every initialize call pays the cost of creating an Array for the masgn (see my rejected request in https://bugs.ruby-lang.org/issues/6668).

def initialize(a, b, c, d, e)
  @a, @b, @c, @d, @e = a, b, c, d, e # creates transient array every time
end
----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40093

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55605] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (5 preceding siblings ...)
  2013-06-23 16:29 ` [ruby-core:55604] " headius (Charles Nutter)
@ 2013-06-23 16:30 ` boris_stitnicky (Boris Stitnicky)
  2013-06-23 23:49 ` [ruby-core:55615] " phluid61 (Matthew Kerwin)
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: boris_stitnicky (Boris Stitnicky) @ 2013-06-23 16:30 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by boris_stitnicky (Boris Stitnicky).


@Matz: If `define_attr_initialize` is an option, then there is a question of named / ordered qualifier, either as:

    define_attr_initialize :foo, :bar, as: :named
    define_attr_initialize :baz, :quux, as: :ordered

or as (and I like this second option better):

    attr_init_named :foo, :bar
    attr_init_ordered :baz, :quux

Both of these would obviously stand for:

    def initialize( @baz, @quux, @foo: nil, @bar: nil )
      ...

Besides that, I feel that `attr_reader`, `attr_writer`, `attr_accessor` could use:

    attr_reader :foo, :bar, initialize: :named
    attr_reader :baz, :quux, initialize: :ordered

Which also brings to my mind, that now that we have named args firmly in place, following
syntactic flavors of `Module#attr_...` beg to exist:

    attr_reader foo: 42, bar: 43  # specifying starting values explicitly
----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40094

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55615] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (6 preceding siblings ...)
  2013-06-23 16:30 ` [ruby-core:55605] " boris_stitnicky (Boris Stitnicky)
@ 2013-06-23 23:49 ` phluid61 (Matthew Kerwin)
  2013-06-24  4:56 ` [ruby-core:55624] " boris_stitnicky (Boris Stitnicky)
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: phluid61 (Matthew Kerwin) @ 2013-06-23 23:49 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by phluid61 (Matthew Kerwin).


boris_stitnicky (Boris Stitnicky) wrote:
> Which also brings to my mind, that now that we have named args firmly in place, following
> syntactic flavors of `Module#attr_...` beg to exist:
> 
>     attr_reader foo: 42, bar: 43  # specifying starting values explicitly

If you propose this as a feature, I will +1 it.  Also I have some questions about it which probably should not pollute the current feature request.
----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40104

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:55624] [ruby-trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (7 preceding siblings ...)
  2013-06-23 23:49 ` [ruby-core:55615] " phluid61 (Matthew Kerwin)
@ 2013-06-24  4:56 ` boris_stitnicky (Boris Stitnicky)
  2016-01-27  9:47 ` [ruby-core:73529] [Ruby trunk " nobu
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: boris_stitnicky (Boris Stitnicky) @ 2013-06-24  4:56 UTC (permalink / raw
  To: ruby-core


Issue #8563 has been updated by boris_stitnicky (Boris Stitnicky).


> If you propose this as a feature, I will +1 it.  Also I have some questions about it which probably should not pollute the current feature request.

I have proposed is as #8564.


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-40112

Author: sawa (Tsuyoshi Sawada)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: syntax
Target version: Next Major


=begin
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

    def initialize foo, bar, buz
      @foo, @bar, @buz = foo, bar, buz
      ...
    end

I propose to let method definition take instance variables as arguments so that:

    def initialize @foo, @bar, @buz
      ...
    end

would be equivalent as above.
=end



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:73529] [Ruby trunk - Feature #8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (8 preceding siblings ...)
  2013-06-24  4:56 ` [ruby-core:55624] " boris_stitnicky (Boris Stitnicky)
@ 2016-01-27  9:47 ` nobu
  2016-07-19  9:46 ` [ruby-core:76443] [Ruby trunk Feature#8563] " eregontp
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: nobu @ 2016-01-27  9:47 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by Nobuyoshi Nakada.

Description updated

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-56743

* Author: Tsuyoshi Sawada
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:76443] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (9 preceding siblings ...)
  2016-01-27  9:47 ` [ruby-core:73529] [Ruby trunk " nobu
@ 2016-07-19  9:46 ` eregontp
  2016-07-19 13:43 ` [ruby-core:76448] " rr.rosas
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: eregontp @ 2016-07-19  9:46 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by Benoit Daloze.


Anonymous wrote:
> @Matz: If `define_attr_initialize` is an option, then there is a question of named / ordered qualifier, either as:
> 
> ~~~Ruby 
> define_attr_initialize :foo, :bar, as: :named
> define_attr_initialize :baz, :quux, as: :ordered
> ~~~
> 

You could have simply:

~~~Ruby
define_attr_initialize :baz, :quux, foo: nil, bar: nil
~~~

But this starts to look much like a macro to me.

One problem with the proposed `define_attr_initialize` is once some extra behavior needs to be added to `initialize`,
there is no choice but to desugar everything (write ivar assignments manually).
The original proposition does not have this issue.

IMHO many constructors usually need some custom behavior after some time,
and so paying the "cost" upfront of doing manual assignments is worth it in many cases.

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-59690

* Author: Tsuyoshi Sawada
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:76448] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (10 preceding siblings ...)
  2016-07-19  9:46 ` [ruby-core:76443] [Ruby trunk Feature#8563] " eregontp
@ 2016-07-19 13:43 ` rr.rosas
  2016-07-19 13:44 ` [ruby-core:76449] " rr.rosas
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: rr.rosas @ 2016-07-19 13:43 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by Rodrigo Rosenfeld Rosas.


Even if define_attr_initialize would accept a block to enhance the initializer besides initializing instance variables it doesn't support default arguments. I'd certainly prefer the original proposal which I find more useful. It's not obvious what array.each(&:to_i) does either but people get used to it because it's handy. I think it's the same about this proposal although it's quite familiar for people using this feature in other languages like CoffeeScript and Crystal among others.

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-59693

* Author: Tsuyoshi Sawada
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:76449] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (11 preceding siblings ...)
  2016-07-19 13:43 ` [ruby-core:76448] " rr.rosas
@ 2016-07-19 13:44 ` rr.rosas
  2017-11-27 14:42 ` [ruby-core:83894] " ruby-core
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: rr.rosas @ 2016-07-19 13:44 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by Rodrigo Rosenfeld Rosas.


Also it doesn't handle variable arguments, extra options that shouldn't be assigned to instance variables or &block.

----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-59694

* Author: Tsuyoshi Sawada
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:83894] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (12 preceding siblings ...)
  2016-07-19 13:44 ` [ruby-core:76449] " rr.rosas
@ 2017-11-27 14:42 ` ruby-core
  2017-11-29  6:25 ` [ruby-core:83942] " ko1
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 18+ messages in thread
From: ruby-core @ 2017-11-27 14:42 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by marcandre (Marc-Andre Lafortune).


matz (Yukihiro Matsumoto) wrote:
> Arguments are giving names to passed values, which is different from attribute (instance variables) initialization. I think they should be separated clearly.

This may be a valid theoretical distinction, but what does this change in practice?

I feel there would be about no additional cognitive load if this was accepted. It is most probably the most requested feature

This is not surprising, since a majority of `initialize` with arguments is doing that!


> And for most of the cases, only `initialize` needs this kind of initialization.

Indeed. This doesn't change the case that a majority of `initialize` methods needs this kind of initialization.

Here's an example Rails app (which I didn't write): https://github.com/WikiEducationFoundation/WikiEduDashboard/search?utf8=%E2%9C%93&q=def+initialize&type=
A gem I'm using a lot these days: https://github.com/whitequark/parser/search?utf8=%E2%9C%93&q=def+initialize&type=

Since keyword arguments, I feel that's even more `true`, since `Struct` is no longer as useful.
 
> My idea is https://bugs.ruby-lang.org/issues/8563#note-3

With all due respect, this is not a good idea and it can't solve the issue. As was pointed out, there are many more cases that it doesn't handle:
- keyword arguments (that's absolutely major!)
- defaults
- dealing with other arguments
- doing extra code

I'd also mention the additional cognitive load involved.

Note that it has always been trivial to implement `define_attr_initialize` in pure Ruby. I've never seen such code though.

Take the top 100 gems, a few web apps, make an inventory of the `initialize` with parameters. I'm betting that the vast majority of these methods would be simplified by allowing instance variable arguments. I doubt that `define_attr_initialize` would help any but a small minority.


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-67941

* Author: sawa (Tsuyoshi Sawada)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:83942] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (13 preceding siblings ...)
  2017-11-27 14:42 ` [ruby-core:83894] " ruby-core
@ 2017-11-29  6:25 ` ko1
  2017-11-29  6:26 ` [ruby-core:83943] [Ruby trunk Feature#8563][Rejected] " matz
  2017-11-29  6:27 ` [ruby-core:83944] [Ruby trunk Feature#8563] " ko1
  16 siblings, 0 replies; 18+ messages in thread
From: ko1 @ 2017-11-29  6:25 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by ko1 (Koichi Sasada).


Not so serious idea.

```
class Binding
  def set_attributes(attrs = self.local_variables)
    attrs.each{|attr|
      self.receiver.instance_variable_set(:"@#{attr}", self.local_variable_get(attr))
    }
  end
end

class C
  def initialize a, b
    binding.set_attributes
  end
end

p C.new(1, 2) #=> #<C:0x000001f30a8b95d0 @a=1, @b=2>
```


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-67999

* Author: sawa (Tsuyoshi Sawada)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:83943] [Ruby trunk Feature#8563][Rejected] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (14 preceding siblings ...)
  2017-11-29  6:25 ` [ruby-core:83942] " ko1
@ 2017-11-29  6:26 ` matz
  2017-11-29  6:27 ` [ruby-core:83944] [Ruby trunk Feature#8563] " ko1
  16 siblings, 0 replies; 18+ messages in thread
From: matz @ 2017-11-29  6:26 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by matz (Yukihiro Matsumoto).

Status changed from Assigned 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.


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-68004

* Author: sawa (Tsuyoshi Sawada)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [ruby-core:83944] [Ruby trunk Feature#8563] Instance variable arguments
  2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
                   ` (15 preceding siblings ...)
  2017-11-29  6:26 ` [ruby-core:83943] [Ruby trunk Feature#8563][Rejected] " matz
@ 2017-11-29  6:27 ` ko1
  16 siblings, 0 replies; 18+ messages in thread
From: ko1 @ 2017-11-29  6:27 UTC (permalink / raw
  To: ruby-core

Issue #8563 has been updated by ko1 (Koichi Sasada).


Oops.

```
class Binding
  def set_attributes(attrs = self.eval('method(__method__).parameters.map{|t, v| v}'))
    attrs.each{|attr|
      self.receiver.instance_variable_set(:"@#{attr}", self.local_variable_get(attr))
    }
  end
end

class C
  def initialize a, b
    x = y = nil
    binding.set_attributes
  end
end

p C.new(1, 2) #=> #<C:0x000001f30a8b95d0 @a=1, @b=2>
```


----------------------------------------
Feature #8563: Instance variable arguments
https://bugs.ruby-lang.org/issues/8563#change-68005

* Author: sawa (Tsuyoshi Sawada)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Often times, people want to assign given arguments to instance variables, especially inside the method `initialize`:

~~~Ruby 
def initialize foo, bar, buz
  @foo, @bar, @buz = foo, bar, buz
  ...
end
~~~

I propose to let method definition take instance variables as arguments so that:

~~~Ruby 
def initialize @foo, @bar, @buz
  ...
end
~~~

would be equivalent as above.




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2017-11-29  6:27 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-22 19:30 [ruby-core:55596] [ruby-trunk - Feature #8563][Open] Instance variable arguments sawa (Tsuyoshi Sawada)
2013-06-23  0:41 ` [ruby-core:55598] [ruby-trunk - Feature #8563][Assigned] " nobu (Nobuyoshi Nakada)
2013-06-23  6:24 ` [ruby-core:55600] [ruby-trunk - Feature #8563] " phluid61 (Matthew Kerwin)
2013-06-23 11:34 ` [ruby-core:55601] " matz (Yukihiro Matsumoto)
2013-06-23 13:21 ` [ruby-core:55602] " sawa (Tsuyoshi Sawada)
2013-06-23 13:37 ` [ruby-core:55603] " nobu (Nobuyoshi Nakada)
2013-06-23 16:29 ` [ruby-core:55604] " headius (Charles Nutter)
2013-06-23 16:30 ` [ruby-core:55605] " boris_stitnicky (Boris Stitnicky)
2013-06-23 23:49 ` [ruby-core:55615] " phluid61 (Matthew Kerwin)
2013-06-24  4:56 ` [ruby-core:55624] " boris_stitnicky (Boris Stitnicky)
2016-01-27  9:47 ` [ruby-core:73529] [Ruby trunk " nobu
2016-07-19  9:46 ` [ruby-core:76443] [Ruby trunk Feature#8563] " eregontp
2016-07-19 13:43 ` [ruby-core:76448] " rr.rosas
2016-07-19 13:44 ` [ruby-core:76449] " rr.rosas
2017-11-27 14:42 ` [ruby-core:83894] " ruby-core
2017-11-29  6:25 ` [ruby-core:83942] " ko1
2017-11-29  6:26 ` [ruby-core:83943] [Ruby trunk Feature#8563][Rejected] " matz
2017-11-29  6:27 ` [ruby-core:83944] [Ruby trunk Feature#8563] " ko1

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).