Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Would a ':=' operator break anything?

Reply
Thread Tools

Would a ':=' operator break anything?

 
 
Phil Tomson
Guest
Posts: n/a
 
      04-09-2004

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

 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      04-09-2004
Hi,

In message "Would a ':=' operator break anything?"
on 04/04/09, Phil Tomson <(E-Mail Removed)> writes:

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


 
Reply With Quote
 
 
 
 
Bedo Sandor
Guest
Posts: n/a
 
      04-09-2004
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

--
bSanyI


 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      04-09-2004
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



 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      04-09-2004
In article <(E-Mail Removed) .jp>,
Yukihiro Matsumoto <(E-Mail Removed)> wrote:
>Hi,
>
>In message "Would a ':=' operator break anything?"
> on 04/04/09, Phil Tomson <(E-Mail Removed)> writes:
>
>|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


 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      04-09-2004
In article <(E-Mail Removed)>,
Hal Fulton <(E-Mail Removed)> wrote:
>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.

>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
 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      04-09-2004
In article <(E-Mail Removed)>,
Bedo Sandor <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New C operator -- would it be a good idea? mike3 C Programming 28 09-14-2012 09:11 PM
Would Anyone Use a _FuncRetType Operator? Shao Miller C Programming 10 02-25-2012 12:52 PM
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
Can we use break statement in conditional operator? jayapal C Programming 6 10-30-2007 12:31 PM



Advertisments