Would a ':=' operator break anything?

This is from something that came up in another thread.

Someone wanted to be able to overload the ‘=’ operator. There was a time
a few year back when I wanted to be able to do this as well, but now I
agree it is ill-advised and would endanger global peace (0ops, too late).
:wink:

Would allowing for overloading a ‘:=’ operator break anything? (seems kind
of Pascalish :slight_smile:

Why would you want to do this?

I’ve got a class like so:

class Ternary #can only take on values 0,1,2
def initialize(value=0)
check_value(value)
@value = value
end

def check_value(value)
if value > 3 || value < 0
raise OutOfRangeException, "value is out of range"
end
end

def :=(value) #not currently possible, but it would be cool
check_value(value)
@value = value
end

def +(value)
Ternary.new((@value + value)%3)
end

def -(value)
Ternary.new((@value - value)%3)
end

end

This would allow you to do:

b = Ternary.new
b := 2
b := 42 #raise exception

So the ‘:=’ operator ‘looks’ close enough to assignment so that the
meaning is clear and it keeps me from having to do:

b.assign 42

Which is how I do it currently with this class and several other classes
I’ve come up with over the years.

So how about it, would allowing a ‘:=’ operator break anything?

I would tend to think not as the ‘:’ appears either as ‘::’ or :symbol so
there shouldn’t be a case where ‘:’ and ‘=’ can legally appear together
now (but I could be overlooking something).

Phil

Hi,

···

In message “Would a ‘:=’ operator break anything?” on 04/04/09, Phil Tomson ptkwt@aracnet.com writes:

Would allowing for overloading a ‘:=’ operator break anything? (seems kind
of Pascalish :slight_smile:

So the ‘:=’ operator ‘looks’ close enough to assignment so that the
meaning is clear and it keeps me from having to do:

b.assign 42

Which is how I do it currently with this class and several other classes
I’ve come up with over the years.

So how about it, would allowing a ‘:=’ operator break anything?

The point is what you want is not assignment in Ruby. It might cause
confusion that naming “:=” something which is not an assignment.
Besides that, I may use “:=” for something different in the future,
although I have no plan now (I had thought to use it once).

						matz.

I think << operator is very expressive in that case, and
world peace conformant. Am I wrong?

def <<(value)
check_value(value = value.to_i)
@value = value
end

b = Ternary.new
b << 2
b << 42

···

On Fri, Apr 09, 2004 at 02:24:16PM +0900, Phil Tomson wrote:

def :=(value) #not currently possible, but it would be cool
check_value(value)
@value = value
end

[…]

This would allow you to do:

b = Ternary.new
b := 2
b := 42 #raise exception


bSanyI

Yukihiro Matsumoto wrote:

So how about it, would allowing a ‘:=’ operator break anything?

The point is what you want is not assignment in Ruby. It might cause
confusion that naming “:=” something which is not an assignment.
Besides that, I may use “:=” for something different in the future,
although I have no plan now (I had thought to use it once).

I think I see his point, however. I will make these comments:

  1. I view his := as a kind of limited assignment.

  2. It would fully change the state of the object, but would not
    change the class or object id.

  3. In that sense, it would be like a synonym for #replace on Arrays
    and Strings:
    str := “hello” # same as str.replace(“hello”)
    arr := [1,2,3] # …

  4. Of course, this is problematic for Fixnums (etc.), as they are
    immediate values and are not mutable.

Just a few thoughts.

Hal

In article 20040409081148.GA14638@sunserv.kfki.hu,

···

Bedo Sandor bsanyi@sunserv.kfki.hu wrote:

On Fri, Apr 09, 2004 at 02:24:16PM +0900, Phil Tomson wrote:

def :=(value) #not currently possible, but it would be cool
check_value(value)
@value = value
end

[…]

This would allow you to do:

b = Ternary.new
b := 2
b := 42 #raise exception

I think << operator is very expressive in that case, and
world peace conformant. Am I wrong?

def <<(value)
check_value(value = value.to_i)
@value = value
end

b = Ternary.new
b << 2
b << 42

Yeah, that would work, though I’m used to thinking of ‘<<’ as push for
arrays.

Phil

In article 1081496723.176610.19491.nullmailer@picachu.netlab.jp,

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message “Would a ‘:=’ operator break anything?” > on 04/04/09, Phil Tomson ptkwt@aracnet.com writes:

Would allowing for overloading a ‘:=’ operator break anything? (seems kind
of Pascalish :slight_smile:

So the ‘:=’ operator ‘looks’ close enough to assignment so that the
meaning is clear and it keeps me from having to do:

b.assign 42

Which is how I do it currently with this class and several other classes
I’ve come up with over the years.

So how about it, would allowing a ‘:=’ operator break anything?

The point is what you want is not assignment in Ruby. It might cause
confusion that naming “:=” something which is not an assignment.
Besides that, I may use “:=” for something different in the future,
although I have no plan now (I had thought to use it once).

  					matz.

Yes, it would require education.

I know I personally have several classes where I have an ‘assign’ method:

someobj.assign value

I know that ‘assign’ is not actually doing the same thing as ‘=’, but it
is changing some important attribute of someobj.

Phil

In article 4076AA59.4020507@hypermetrics.com,

Yukihiro Matsumoto wrote:

So how about it, would allowing a ‘:=’ operator break anything?

The point is what you want is not assignment in Ruby. It might cause
confusion that naming “:=” something which is not an assignment.
Besides that, I may use “:=” for something different in the future,
although I have no plan now (I had thought to use it once).

I think I see his point, however. I will make these comments:

  1. I view his := as a kind of limited assignment.

  2. It would fully change the state of the object, but would not
    change the class or object id.

exactly.

  1. In that sense, it would be like a synonym for #replace on Arrays
    and Strings:
    str := “hello” # same as str.replace(“hello”)
    arr := [1,2,3] # …

  2. Of course, this is problematic for Fixnums (etc.), as they are
    immediate values and are not mutable.

Finxnums (and other immediates) wouldn’t have to implement ‘:=’ - actually
no class would have to implement it, it would be around in case people
want to override it in their own classes.

But, as someone mentioned in this thread ‘<<’ could be used for this
purpose. So maybe that’s the way to go.

Phil

···

Hal Fulton hal9000@hypermetrics.com wrote: