Proposal: new operator: '<-' (for assignments)

Since we can’t override ‘=’ (and it’s a good idea that we can’t) it seems
that it would be nice to have another overridable operator for assigning
values in classes. So, I’m proposing that we allow a new operator ‘<-’
(less-than minus) which can be overridden to allow assignment, for
example:

class Foo
def initialize(value)
@val = value
end

def <-(newVal)
@val = newVal
end
end

foo = Foo.new(“this value”)
…later…
foo <- “some new value”

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that information
or make users of the class have to remember the name of the variable
’val’. It just seems much cleaner in many cases to have an assignment
operator. Up till now I’ve been overriding ‘<<’ to do this, but now I’ve
run into a case where I’d like to pass on operators to the underlying
value (@val in the example above) using method_missing so the underlying
value can be shifted (and since ‘<<’ is overriden, that’s kind of tough).

Thoughts?

Phil

Array, String, and Hash use #replace to do this.

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.20 at 22:29:11

···

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that
information or make users of the class have to remember the name
of the variable ‘val’. It just seems much cleaner in many cases to
have an assignment operator. Up till now I’ve been overriding ‘<<’
to do this, but now I’ve run into a case where I’d like to pass on
operators to the underlying value (@val in the example above)
using method_missing so the underlying value can be shifted (and
since ‘<<’ is overriden, that’s kind of tough).

Hi,

···

At Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

foo ← “some new value”

It conflicts with ‘<’ and ‘-’, for example:

foo<-1


Nobu Nakada

foo = Foo.new(“this value”)
…later…
foo <- “some new value”

···

----- Original Message -----

This looks backwards to me. What I mean is, shouldn’t the arrow point the
other way? It seems closer to how hashes literals work, for example. The
"variable" points to the “value”.

In any case, I don’t think you can have left arrows like <- and <=: <= is
taken (and isn’t an arrow :slight_smile: and <- might be used in code like ‘a<-b’ (is
a' less than negativeb’?).

So how would you feel about -> ?

Honestly, I’d be a little surprised if this got in to the language.
Depending on your situation (you’re trying to make a special-purpose
language, right?) it might be better to write your own interpreter on top of
Ruby, snarfing generously.

For example, if you have a string of code like:

foo << { doSomethingHere }

you could just split the string on the first ‘<<’ and eval the substrings.
Depending on how robust you need your language to be (I’m assuming not very,
since you were writing it for people who aren’t really programmers, IIRC),
this should be a relatively easy task, and perfect for Ruby. There’s no
reason the code used has to be readable by the actual Ruby interpreter, if
you can write a couple-hundred line interpreter as a go-between.

Just a thought,

Chris

I’m still not enamoured of the concept of using this instead of
a #replace method, but what about “<:” instead?

class Foo
def initialize(value)
@val = value
end

def <:(newVal)
  @val = newVal
end

end

foo = Foo.new(“this value”)

…later…

foo <: “some new value”

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.21 at 14:56:27

···

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

Since we can’t override ‘=’ (and it’s a good idea that we can’t)
it seems that it would be nice to have another overridable
operator for assigning values in classes. So, I’m proposing that
we allow a new operator ‘<-’ (less-than minus) which can be
overridden to allow assignment, for example:

Hi –

Since we can’t override ‘=’ (and it’s a good idea that we can’t) it seems
that it would be nice to have another overridable operator for assigning
values in classes. So, I’m proposing that we allow a new operator ‘<-’
(less-than minus) which can be overridden to allow assignment, for
example:

class Foo
def initialize(value)
@val = value
end

def <-(newVal)
@val = newVal
end
end

foo = Foo.new(“this value”)
…later…
foo ← “some new value”

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that information
or make users of the class have to remember the name of the variable
‘val’. It just seems much cleaner in many cases to have an assignment
operator. Up till now I’ve been overriding ‘<<’ to do this, but now I’ve
run into a case where I’d like to pass on operators to the underlying
value (@val in the example above) using method_missing so the underlying
value can be shifted (and since ‘<<’ is overriden, that’s kind of tough).

Thoughts?

This thread may have played out (you have to be awfully quick on the
button in ruby-talk these days!), but let me chime in anyway…

I don’t think that what you’re describing sounds like assignment; that
is:

foo ← “some new value”

isn’t actually assigning anything to foo. So it just becomes, in a
sense, a kind of status-bestowing (because operator-like) method name,
which in the absence of its actually doing assignment I think is
potentially misleading.

Also, using a regular method name doesn’t necessarily mean it has to
be named after a particular variable, so concerns about exposing
variable names wouldn’t have to arise. I’m thinking of things like:

def reset_to(x)
@val = x
end

and so on, which don’t use the attr_* family of shortcuts but might be
more suitable in a given case.

David

···

On Fri, 21 Mar 2003, Phil Tomson wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

In article 2003320223415.480988@PADD,

···

Austin Ziegler austin@halostatue.ca wrote:

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that
information or make users of the class have to remember the name
of the variable ‘val’. It just seems much cleaner in many cases to
have an assignment operator. Up till now I’ve been overriding ‘<<’
to do this, but now I’ve run into a case where I’d like to pass on
operators to the underlying value (@val in the example above)
using method_missing so the underlying value can be shifted (and
since ‘<<’ is overriden, that’s kind of tough).

Array, String, and Hash use #replace to do this.

Perhaps, but I’m looking for an operator because:

  1. it’s shorter
  2. the intent of ‘<-’ is pretty clear (in fact it’s often used for this in
    pseudocode)

Phil

It’d be easier to make a preprocessor to transform, say
a <== val
into
a.value=(value)

which moreover allows
a <== val1, val2, val3
which might be useful.

···

On Sat, Mar 22, 2003 at 12:12:16AM +0900, Chris Pine wrote:

Honestly, I’d be a little surprised if this got in to the language.
Depending on your situation (you’re trying to make a special-purpose
language, right?) it might be better to write your own interpreter on top of
Ruby, snarfing generously.

For example, if you have a string of code like:

foo << { doSomethingHere }

you could just split the string on the first ‘<<’ and eval the substrings.
Depending on how robust you need your language to be (I’m assuming not very,
since you were writing it for people who aren’t really programmers, IIRC),
this should be a relatively easy task, and perfect for Ruby. There’s no
reason the code used has to be readable by the actual Ruby interpreter, if
you can write a couple-hundred line interpreter as a go-between.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

martin@bdsi.com (no longer valid - where are you now, Martin?)
– from /usr/src/linux/drivers/cdrom/mcd.c

In article 024d01c2efbc$2fa7a040$6401a8c0@MELONBALLER,

foo = Foo.new(“this value”)
…later…
foo ← “some new value”

This looks backwards to me. What I mean is, shouldn’t the arrow point the
other way? It seems closer to how hashes literals work, for example. The
“variable” points to the “value”.

Hmmm… I guess I read it as 'foo takes the value “some new value”

In any case, I don’t think you can have left arrows like ← and <=: <= is
taken (and isn’t an arrow :slight_smile: and ← might be used in code like ‘a<-b’ (is
a' less than negative b’?).

Aye, there’s the rub…

So how would you feel about → ?

Honestly, I’d be a little surprised if this got in to the language.
Depending on your situation (you’re trying to make a special-purpose
language, right?) it might be better to write your own interpreter on top of
Ruby, snarfing generously.

For example, if you have a string of code like:

foo << { doSomethingHere }

you could just split the string on the first ‘<<’ and eval the substrings.
Depending on how robust you need your language to be (I’m assuming not very,
since you were writing it for people who aren’t really programmers, IIRC),
this should be a relatively easy task, and perfect for Ruby. There’s no
reason the code used has to be readable by the actual Ruby interpreter, if
you can write a couple-hundred line interpreter as a go-between.

I’m specifically trying to avoid writing a parser. I want RHDL to be pure
Ruby.

I think what I’ll end up doing is using ‘<<’ and then alias the original
‘<<’ to ‘shiftl’.

Phil

···

Chris Pine nemo@hellotree.com wrote:

----- Original Message -----

In article 2003320223415.480988@PADD,

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that
information or make users of the class have to remember the name
of the variable ‘val’. It just seems much cleaner in many cases to
have an assignment operator. Up till now I’ve been overriding ‘<<’
to do this, but now I’ve run into a case where I’d like to pass on
operators to the underlying value (@val in the example above)
using method_missing so the underlying value can be shifted (and
since ‘<<’ is overriden, that’s kind of tough).

Array, String, and Hash use #replace to do this.

Perhaps, but I’m looking for an operator because:

  1. it’s shorter
  2. the intent of ‘<-’ is pretty clear (in fact it’s often used for this in
    pseudocode)

Interesting, but I’m not sure it improves the
language enough to justify the changes.

It would surprise me if Matz liked this.

Another possibility might be to establish a standard
accessor name as Javascript does and as I did with
my super-iterator class.

class Array
def value=(val)
self.replace val
end
end

It would surprise me if you liked this. :slight_smile:

Cheers,
Hal

···

----- Original Message -----
From: “Phil Tomson” ptkwt@shell1.aracnet.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 2:41 AM
Subject: Re: Proposal: new operator: ‘<-’ (for assignments)

Austin Ziegler austin@halostatue.ca wrote:

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

I don’t think that it’s clear; I use ← in pseudocode to represent
assignment, not replacement.

If your class doesn’t need <=, you could redefine that, though
you’ll get a “void context” warning.

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.21 at 10:12:18

···

On Fri, 21 Mar 2003 17:41:21 +0900, Phil Tomson wrote:

Austin Ziegler austin@halostatue.ca wrote:

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:
[request for new operator <-]
Array, String, and Hash use #replace to do this.
Perhaps, but I’m looking for an operator because:

  1. it’s shorter
  2. the intent of ‘<-’ is pretty clear (in fact it’s often used for
    this in pseudocode)

It’d be easier to make a preprocessor to transform, say
a <== val
into
a.value=(value)

which moreover allows
a <== val1, val2, val3
which might be useful.

···

----- Original Message -----
From: “Mauricio Fernández” batsman.geo@yahoo.com


Yep, that would work, too. The “interpreter” I was advocating was little
more that a preprocessor, anyway. :slight_smile:

Either way (preprocessor or interpreter), Phil may want some totally
different syntax from what he showed the list, if he doesn’t have to hammer
it straight into the Ruby interpreter anymore.

So often, thought, these things start small, then grow and grow… Depending
on the situation, it might be easier in the long run to write a simple
interpreter now, rather than switch over to one later when preprocessing is
getting too hairy. Preprocessing is basically translation from one language
to another. Eventually, interpretation becomes easier to work with than
translation, which is why Ruby is interpreted rather than translated to C
and then compiled.

Ultimately it’s specific to the situation; only Phil can say which is more
appropriate.

Chris

what about
class blah
def <= (val)

end
end

If <= isn’t enough like <- for your tastes, then I don’t know what to say.

~Me!

In article 003701c2ef8a$e05cd220$0300a8c0@austin.rr.com,

From: “Phil Tomson” ptkwt@shell1.aracnet.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, March 21, 2003 2:41 AM
Subject: Re: Proposal: new operator: ‘<-’ (for assignments)

In article 2003320223415.480988@PADD,

Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that
information or make users of the class have to remember the name
of the variable ‘val’. It just seems much cleaner in many cases to
have an assignment operator. Up till now I’ve been overriding ‘<<’
to do this, but now I’ve run into a case where I’d like to pass on
operators to the underlying value (@val in the example above)
using method_missing so the underlying value can be shifted (and
since ‘<<’ is overriden, that’s kind of tough).

Array, String, and Hash use #replace to do this.

Perhaps, but I’m looking for an operator because:

  1. it’s shorter
  2. the intent of ‘<-’ is pretty clear (in fact it’s often used for this in
    pseudocode)

Interesting, but I’m not sure it improves the
language enough to justify the changes.

It would surprise me if Matz liked this.

Yeah, I didn’t figure the chances were too great, but it was worth a try
anyway :wink:

Another possibility might be to establish a standard
accessor name as Javascript does and as I did with
my super-iterator class.

class Array
def value=(val)
self.replace val
end
end

It would surprise me if you liked this. :slight_smile:

Well, I’ll probably just stick with using ‘<<’ as a synonym or ‘assign’
and alias the original ‘<<’ shift functionality of Fixnum to ‘shift’

Phil

···

Hal E. Fulton hal9000@hypermetrics.com wrote:

----- Original Message -----

Austin Ziegler austin@halostatue.ca wrote:

On Fri, 21 Mar 2003 11:20:27 +0900, Phil Tomson wrote:

In article 13383d7a.0303210852.688a2f58@posting.google.com,

···

matt mhm26@drexel.edu wrote:

what about
class blah
def <= (val)

end
end

If <= isn’t enough like ← for your tastes, then I don’t know what to say.

~Me!

True, in fact I did this at first but then I couldn’t do
less-than-or-equal comparisons on my signals either so I went to using an
‘assign’ method.

Phil