Would a ':=' operator break anything?

P

Phil Tomson

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).
;-)

Would allowing for overloading a ':=' operator break anything? (seems kind
of Pascalish :)

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Would a ':=' operator break anything?"

|Would allowing for overloading a ':=' operator break anything? (seems kind
|of Pascalish :)

|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.
 
B

Bedo Sandor

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
 
H

Hal Fulton

Yukihiro said:
|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
 
P

Phil Tomson

Hi,

In message "Would a ':=' operator break anything?"

|Would allowing for overloading a ':=' operator break anything? (seems kind
|of Pascalish :)

|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
 
P

Phil Tomson

Yukihiro said:
|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.

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.

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
 
P

Phil Tomson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top