Forum: Ruby-core [ruby-trunk - Feature #9060][Open] "Assignment" operator overloading

67721c78a91de0c2ae41a7ce73bd8c56?d=identicon&s=25 Bruce Matz (texasbruce)
on 2013-10-30 02:01
(Received via mailing list)
Issue #9060 has been reported by popoxee (Bruce Metz).

----------------------------------------
Feature #9060: "Assignment" operator overloading
https://bugs.ruby-lang.org/issues/9060

Author: popoxee (Bruce Metz)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I understand assignment in ruby is assigning an object to the variable.
However there are other cases we need to use assignments. For example:

(({class Foo
end

class Bar < Foo
end

f = Foo.new
b = Bar.new
//Some operations on b}))

Now we want f to contain the same data as b. But if we use f = b, f will
not be Foo any more.
Or we can define a method like ((*Foo##loadFromBar*)), but Foo is the
super class of Bar and usually defined before Bar...

Can we have something like:
(({f := b}))
so that f will have the same shared data with b?

And we can even load data from other classes by overloading this
operator:

(({class Foo
  def :=(f)
    @data = f
  end
end
f = Foo.new
f := 2.2}))

We can always achieve this by defining a named method, but this operator
can provide some syntax sugar and convenience.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 matz (Yukihiro Matsumoto) (Guest)
on 2013-10-30 09:39
(Received via mailing list)
Issue #9060 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

I am not sure what you want.  Could you elaborate?
Doesn't defining regular method like #update do for you?

  class Foo
    def update(obj)
     @data = obj.data
    end
    attr_accessor :data
  end
  class Bar < Foo
  end

  f = Foo.new
  b = Bar.new
  ....
  f.update(b)

Reopen if you have objection.

Matz.

----------------------------------------
Feature #9060: "Assignment" operator overloading
https://bugs.ruby-lang.org/issues/9060#change-42654

Author: popoxee (Bruce Metz)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I understand assignment in ruby is assigning an object to the variable.
However there are other cases we need to use assignments. For example:

(({class Foo
end

class Bar < Foo
end

f = Foo.new
b = Bar.new
//Some operations on b}))

Now we want f to contain the same data as b. But if we use f = b, f will
not be Foo any more.
Or we can define a method like ((*Foo##loadFromBar*)), but Foo is the
super class of Bar and usually defined before Bar...

Can we have something like:
(({f := b}))
so that f will have the same shared data with b?

And we can even load data from other classes by overloading this
operator:

(({class Foo
  def :=(f)
    @data = f
  end
end
f = Foo.new
f := 2.2}))

We can always achieve this by defining a named method, but this operator
can provide some syntax sugar and convenience.
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2013-10-30 14:49
(Received via mailing list)
Issue #9060 has been updated by nobu (Nobuyoshi Nakada).

Description updated


----------------------------------------
Feature #9060: "Assignment" operator overloading
https://bugs.ruby-lang.org/issues/9060#change-42658

Author: popoxee (Bruce Metz)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I understand assignment in ruby is assigning an object to the variable.
However there are other cases we need to use assignments. For example:

 class Foo
 end

 class Bar < Foo
 end

 f = Foo.new
 b = Bar.new
 //Some operations on b

Now we want ((|f|)) to contain the same data as ((|b|)). But if we use
(({f = b})), ((|f|)) will not be (({Foo})) any more.
Or we can define a method like (({Foo##loadFromBar})), but (({Foo})) is
the super class of (({Bar})) and usually defined before (({Bar}))...

Can we have something like:
 f := b
so that ((|f|)) will have the same shared data with ((|b|))?

And we can even load data from other classes by overloading this
operator:

 class Foo
   def :=(f)
     @data = f
   end
 end
 f = Foo.new
 f := 2.2

We can always achieve this by defining a named method, but this operator
can provide some syntax sugar and convenience.
=end
This topic is locked and can not be replied to.