From: sonots@... Date: 2018-04-26T07:22:00+00:00 Subject: [ruby-core:86693] [Ruby trunk Feature#14701] If the object is not frozen, I want to be able to redefine the compound assignment operator. Issue #14701 has been updated by sonots (Naotoshi Seo). As first thing to say, we do not expect changing behavior of `+=` for ruby built-in types such as string or array. We want to have an extension point to redefine `+=` for our library such as NArray. How it should behave is the design choice and responsibility of such libraries. > Most Rubyists know and expect that Array#+ is non-destructive, so I think that it is unacceptable to allow this. I mean that It is even possible to redefine `+` operator to be destructive. It is design choice and responsibility of libraries. > Do you mean introducing another set of destructive operators, like a +! 1 instead of a += 1? It is not very cool, but it seems more reasonable to me. Yes, as a compromise. I still prefer `+=`, though. > IMO, the fundamental flaw is to split + and +=. IMO, it is design responsibility of libraries. But, I understand what you says partially. I will think of this. ---------------------------------------- Feature #14701: If the object is not frozen, I want to be able to redefine the compound assignment operator. https://bugs.ruby-lang.org/issues/14701#change-71652 * Author: naitoh (Jun NAITOH) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- If the object is not frozen, I want to be able to redefine the compound assignment operator (e.g. +=, -=, *=, /=, ..etc ). https://docs.ruby-lang.org/ja/latest/doc/spec=2foperator.html * Redefinable operator (method) ~~~ | ^ & <=> == === =~ > >= < <= << >> + - * / % ** ~ +@ -@ [] []= ` ! != !~ ~~~ * use case ~~~ > require 'numo/narray' > a = Numo::Int32[5, 6] => Numo::Int32#shape=[2] [5, 6] > a.object_id => 70326927544920 > a += 1 => Numo::Int32#shape=[2] [6, 7] > a.object_id => 70326927530540 > a.inplace + 1 => Numo::Int32(view)#shape=[2] [7, 8] > a.object_id => 70326927530540 ~~~ With Numo::NArray, using "inplace" instead of "+=" will update the same object so it will be faster. I want to write "a += 1" instead of "a.inplace + 1". However, Ruby can not redefine "+=". -- https://bugs.ruby-lang.org/ Unsubscribe: