Feature idea: custom literals

wouldn’t it be cool if you could define custom literal representations for your
classes?
Proposition:

have a method return a regex container/context that ruby would match against
garble that doesnt fall under any other category. the regular expression would
of
course yeild to a few constraints, a set of valid encapsilation/delimiting
characters, a sub-syntax, and have the groups passed (in order) to a custom
initializer:

class foo
def literal_exp
/^*(.?)*/ #or whatever
end
def literal_init(s1)
@value = s1
end
def to_s
@value
end
end
blah = rawwww
I haven’t really thought it out, but it could work, might be slugish, or
impractical, but it would be cool!#
&(*@&#@!~!!!

loats205 wrote:

wouldn’t it be cool if you could define custom literal representations for your
classes?

What problem is this meant to solve as i cant see a use for such a thing.

Hi,

···

In message “feature idea: custom literals” on 02/10/30, loats205 loats205@aol.com writes:

wouldn’t it be cool if you could define custom literal representations for your
classes?

You may feel it’s cool to have “reader macro” in Ruby, but I won’t
buy it. It gives us “too much flexibility”, like macros in Lisp.

Some kind of “restricted reader macro” may be possible. For example

%L<!@$whatever$@!>

I’m still not sure whether it’s the way to go.

						matz.

Hello Peter,

Wednesday, October 30, 2002, 12:09:05 PM, you wrote:

wouldn’t it be cool if you could define custom literal representations for your
classes?

What problem is this meant to solve as i cant see a use for such a thing.

complex = 3+7i
str =~ s/x/y/gi

another feature will be custom operators:

if 1 in [1,2,3]

···


Best regards,
Bulat mailto:bulatz@integ.ru

Yukihiro Matsumoto wrote:

You may feel it’s cool to have “reader macro” in Ruby, but I won’t

Thats what I thought he was talking about but I couldn’t see how his
method was to work.

Hello Yukihiro,

Wednesday, October 30, 2002, 2:05:51 PM, you wrote:

wouldn’t it be cool if you could define custom literal representations for your
classes?

You may feel it’s cool to have “reader macro” in Ruby, but I won’t
buy it. It gives us “too much flexibility”, like macros in Lisp.

how about an idea of some hooks in syntax parser?

···


Best regards,
Bulat mailto:bulatz@integ.ru

Using ruby to solve domain specific issues. For example, 3D stuff need a
lot of matrices and vecotrs. It would be nice to have literal for those,
now I need to do

Vector.new(3,5,6)

This can get rather verbose in the long run, defining a literal would allow
me to do

<3,5,6>

or whatever.

Yes, this would definitely be cool. And useful. I am thinking this is
somehing that should have a file based scope: you would explicitly have to
enable the syntax in each file you want to use it in.

Perhaps the simples option would be to define ‘<’ … ‘>’ and some others
as used bindable lietarals. It would be artificially resticted, but much
more implementable then a totally general case.

– Nikodemus

···

On Wed, 30 Oct 2002, Peter Hickman wrote:

wouldn’t it be cool if you could define custom literal representations for your
classes?

What problem is this meant to solve as i cant see a use for such a thing.

peter wrote:

loats205 wrote:

wouldn’t it be cool if you could define custom literal representations for
your
classes?

What problem is this meant to solve as i cant see a use for such a thing.

well it could ‘solve’ a few problems, or rather simplify a few tasks.
one being persistence, if eval()'ing a database entry in your custom literal
format, returns a new object of your its classes type. it could get really
ugly, if you have 10,000, or even 10 classes with a coresponding literal
expression in one program, but in my mind that would be the developers fault,
not the fact that literal expressions are possible. I don’t have any idea ATM
how that would really even be implemented, just an idea.

Bulat Ziganshin wrote:

What problem is this meant to solve as i cant see a use for such a thing.

complex = 3+7i
str =~ s/x/y/gi

another feature will be custom operators:

if 1 in [1,2,3]

No I can’t see what you’re trying to show. Perhaps an example?

It is the source of “too much flexibility”, unless it is well thought
and well deciplined.

						matz.
···

In message “Re: feature idea: custom literals” on 02/10/30, “Bulat Ziganshin” bulatz@integ.ru writes:

You may feel it’s cool to have “reader macro” in Ruby, but I won’t
buy it. It gives us “too much flexibility”, like macros in Lisp.

how about an idea of some hooks in syntax parser?

But the former is far more readable.

And how would custom literals be scoped? Suppose one Ruby module
defines <x,y,z> to denote a 3D vector and another defines <r,g,b> to
denote an RGB colour, how would the programmer define which literal is
used? With classes you can explicitly name a fully qualified class,
import a class by name, or import under a different name by assigning a
class reference to a constant in the local module. But that cannot be
done at the syntax level.

Cheers,
Nat.

···

On Wed, 2002-10-30 at 11:41, Nikodemus Siivola wrote:

Using ruby to solve domain specific issues. For example, 3D stuff need a
lot of matrices and vecotrs. It would be nice to have literal for those,
now I need to do

Vector.new(3,5,6)

This can get rather verbose in the long run, defining a literal would allow
me to do

<3,5,6>

or whatever.


Dr. Nathaniel Pryce, Technical Director, B13media Ltd.
Studio 3a, 22-24 Highbury Grove, London N5 2EA, UK
http://www.b13media.com

Hi,

I think things like this can be implemented by individuals and not forced
into the standard Ruby. For example, I write my own SRuby so that I can
"preprocess" a file written in Python style with the output of a valid
Ruby code. In the same way, if we like “<3, 5, 6>” to be the notation for
a vector, we can simply preprocess it to be “Vector.new(3, 5, 6)”. It is
true that the algorithm/grammar/parser can get very complicated, but the
individual can control how complex he/she wants it to be.

Regards,

Bill

···

Nikodemus Siivola tsiivola@cc.hut.fi wrote:

Using ruby to solve domain specific issues. For example, 3D stuff need a
lot of matrices and vecotrs. It would be nice to have literal for those,
now I need to do

Vector.new(3,5,6)

This can get rather verbose in the long run, defining a literal would allow
me to do

<3,5,6>

or whatever.

Yes, this would definitely be cool. And useful. I am thinking this is
somehing that should have a file based scope: you would explicitly have to
enable the syntax in each file you want to use it in.

Perhaps the simples option would be to define ‘<’ … ‘>’ and some others
as used bindable lietarals. It would be artificially resticted, but much
more implementable then a totally general case.

“Bulat Ziganshin” bulatz@integ.ru wrote in message news:96194121481.20021030141206@integ.ru

Hello Yukihiro,

Wednesday, October 30, 2002, 2:05:51 PM, you wrote:

wouldn’t it be cool if you could define custom literal representations for your
classes?

You may feel it’s cool to have “reader macro” in Ruby, but I won’t
buy it. It gives us “too much flexibility”, like macros in Lisp.

how about an idea of some hooks in syntax parser?

That way lies madness - or Perl 6, which comes to the same thing (:

martin

Hello Peter,

Wednesday, October 30, 2002, 1:40:41 PM, you wrote:

What problem is this meant to solve as i cant see a use for such a thing.

complex = 3+7i

it’s a literal for complex numbers

str =~ s/x/y/gi

it’s for string replacement in sed style

another feature will be custom operators:

if 1 in [1,2,3]

it’s check that 1 is a member of set, described by [1,2,3]

···

No I can’t see what you’re trying to show. Perhaps an example?


Best regards,
Bulat mailto:bulatz@integ.ru

Yukihiro Matsumoto wrote:

It is the source of “too much flexibility”, unless it is well thought
and well deciplined.

There are two types of Lisp programmer.

  1. Those that understand macros
  2. Those who cannot understand anything written by the above

I’m a type 2.

Per file.

– Nikodemus

···

On Wed, 30 Oct 2002, Nat Pryce wrote:

And how would custom literals be scoped? Suppose one Ruby module

Using ruby to solve domain specific issues. For example, 3D stuff need a
lot of matrices and vecotrs. It would be nice to have literal for those,
now I need to do

Vector.new(3,5,6)

This can get rather verbose in the long run, defining a literal would allow
me to do

<3,5,6>

or whatever.

But the former is far more readable.

Cheers,
Nat.

Another thing to remember is that you can always use

Vector[3,5,6]

which is even better. (This probably isn’t defined, of course, I don’t even
know what librrary we’re talking about, but you could define it yourself, and
ask your friendly library author to include it.)

Gavin

···

From: “Nat Pryce” nat.pryce@b13media.com

On Wed, 2002-10-30 at 11:41, Nikodemus Siivola wrote:

Nat Pryce wrote:

Vector.new(3,5,6)
[ versus ]

<3,5,6>

But the former is far more readable.

Yes, so a potential middle ground:

module VectorUtil def V(s) eval "[#{s}]" # or use "Vector.new(#{s})" end end include VectorUtil p V %<1,2,3> #=> [1, 2, 3]

Way crypting enough, thank you very much! :slight_smile:

···

On Wed, 2002-10-30 at 11:41, Nikodemus Siivola wrote:


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

In article app0cb$30r$2@grapevine.wam.umd.edu,

Using ruby to solve domain specific issues. For example, 3D stuff need a
lot of matrices and vecotrs. It would be nice to have literal for those,
now I need to do

Vector.new(3,5,6)

This can get rather verbose in the long run, defining a literal would allow
me to do

<3,5,6>

or whatever.

Yes, this would definitely be cool. And useful. I am thinking this is
somehing that should have a file based scope: you would explicitly have to
enable the syntax in each file you want to use it in.

Perhaps the simples option would be to define ‘<’ … ‘>’ and some others
as used bindable lietarals. It would be artificially resticted, but much
more implementable then a totally general case.

Hi,

I think things like this can be implemented by individuals and not forced
into the standard Ruby. For example, I write my own SRuby so that I can
"preprocess" a file written in Python style with the output of a valid
Ruby code. In the same way, if we like “<3, 5, 6>” to be the notation for
a vector, we can simply preprocess it to be “Vector.new(3, 5, 6)”. It is
true that the algorithm/grammar/parser can get very complicated, but the
individual can control how complex he/she wants it to be.

    • If you really think this is a good idea, then you will really
      like Perl6. IMHO, it’s the path to madness and the big reason
      that I’ve decided that I need to learn a new language. To me
      there is a significant value in restraint, language design need
      to strike a balance between flexibility and expression. If you
      create “per application or per programmer” languages and there
      is a whole school of CS theory/practice about doing this, I
      think you destroy much of the open community that developes
      around a language.
    • There is a fundemental difference in philosophy that can’t be
      resolved. IMHO, in the long run computer languages are just as
      much about communicating with other programmers as about
      communicating with the machine. One of the reasons we all
      love Ruby is that it is very readable. While this proposal
      is well intentioned, anything that compromises the readablity
      of Ruby has to be looked at very carefully.
    • Booker C. Bense
···

William Djaja Tjokroaminata billtj@y.glue.umd.edu wrote:

Nikodemus Siivola tsiivola@cc.hut.fi wrote:

Bulat Ziganshin wrote:

Hello Peter,

Wednesday, October 30, 2002, 1:40:41 PM, you wrote:

What problem is this meant to solve as i cant see a use for such a thing.

complex = 3+7i

it’s a literal for complex numbers

str =~ s/x/y/gi

it’s for string replacement in sed style

another feature will be custom operators:

if 1 in [1,2,3]

it’s check that 1 is a member of set, described by [1,2,3]

No I can’t see what you’re trying to show. Perhaps an example?

Fine. But HOW is his suggestion supposed to achieve this?