Feature idea: custom literals

Nikodemus Siivola wrote:

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

Per file.

Chuck the “file” concept out of your head for this one. What file does
the evaled string that you recieved from a CGI form come? Ruby ain’t
Python, which has a one-module-one-file restriction and can easily do
this kind of madness.

It would be a new axis to diffrentiate along, and IMHO, it is a bad
idea…

···

On Wed, 30 Oct 2002, Nat Pryce 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)

Either you misunderstand me, or I misunderstand you…

How would you import a literal definition? If you define it in a file,
can it only be used within a file? Or can it be imported into another
file (by the require statement for example)?

If the former, then a programmer would have to keep redefining the
literal everywhere that they want to use it, which is bad programming
practice.

If the latter, then what do you do about clashing definitions? Ruby has
modules to manage name clash. How would a literal definition work with
modules? By the very fact that literals are syntax, they are
processed before modules are used for name resolution, which is a
semantic issue.

Personally, I think that the best languages are those with the smallest
amount of syntax, but where that syntax can be used and combined in very
flexible ways. E.g. Lisp, Scheme, Smalltalk or Haskell.

Cheers,
Nat.

···

On Wed, 2002-10-30 at 14:02, Nikodemus Siivola wrote:

On Wed, 30 Oct 2002, Nat Pryce wrote:

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

Per file.


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

Hi,

···

In message “Re: feature idea: custom literals” on 02/10/30, Peter Hickman peter@semantico.com writes:

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.

Me too. Even though macros increase succinctness, they consume too
much brain power, which I have little.

						matz.

Hello Peter,

Wednesday, October 30, 2002, 2:14:34 PM, you wrote:

it’s a literal for complex numbers
Fine. But HOW is his suggestion supposed to achieve this?

if you ask about user interface, it is just list of hooks such as:

def imaginary_literal(str)
if str =~ /(\d+)i/i
return Complex.new( 0, $1.to_i)
else
raise
end
end

add_literal(:imaginary_literal)

if you ask about realization details, i absolutely don’t know HOW :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

You maybe right, but just to be stubborn:

  1. No custom literals in eval
  2. Each evaled string is it’s own “file”

I prefer 2).

– Nikodemus

···

On Wed, 30 Oct 2002, Kent Dahl wrote:

Chuck the “file” concept out of your head for this one. What file does
the evaled string that you recieved from a CGI form come? Ruby ain’t

Pseudocode, assuming that only ‘<’ … ‘>’ literals are user definable:

in vector.rb:

class Vector
def initialize x,y,z
@x, @y, z = x, y, z
end
end

in sample.rb:

require ‘vector’
literal Vector.new
<0,1,2>

Is equivalent to Vector.new (0,1,2), but only in this file

and only until it is (possibly) redefined

So custom literals would be per file aliases for <>(*args).

– Nikodemus

···

On Thu, 31 Oct 2002, Nat Pryce wrote:

How would you import a literal definition? If you define it in a file,
can it only be used within a file? Or can it be imported into another
file (by the require statement for example)?

Hello Yukihiro,

Thursday, October 31, 2002, 10:13:34 AM, you wrote:

Me too. Even though macros increase succinctness, they consume too
much brain power, which I have little.

how about adding some silicon? :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

I see. I think this is pretty superfluous, and also dramatically
reduces the legibility of the code, especially if <x,y,z> creates one
type of object in one file and and another type of object in another.

You can achieve a similar effect with functions, but also control
legibility of code. At the very least, you only have to use one extra
character, at best you’d have a readable constructor function.

E.g:

Some classes:

class Color
def initialize( r, g, b )
@r = r
@g = g
@b = b
end
attr_reader :r
attr_reader :g
attr_reader :b
end

class Vector
def initialize( x, y, z )
@x = x
@y = y
@z = z
end
attr_reader :x
attr_reader :y
attr_reader :z
end

Some “literal” functions

def Vector( x, y, z )
Vector.new( x, y, z )
end

ORIGIN = Vector(0,0,0)

def Color( r, g, b )
Color.new( r, g, b )
end

BLACK = Color(0,0,0)
RED = Color(1,0,0)
GREEN = Color(0,1,0)
BLUE = Color(0,0,1)
WHITE = Color(1,1,1)

Or use abreviations:

def Vec( x, y, z )
Vector.new( x, y, z )
end

def V( x, y, z )
Vector.new( x, y, z )
end

X_AXIS = V(1,0,0)

def RGB( r, g, b )
Color.new( r, g, b )
end

CYAN = RGB(0,1,1)

This idiom is used by Ruby already, for example the Integer and String
functions.

Cheers,
Nat.

···

On Wed, 2002-10-30 at 16:04, Nikodemus Siivola wrote:

On Thu, 31 Oct 2002, Nat Pryce wrote:

How would you import a literal definition? If you define it in a file,
can it only be used within a file? Or can it be imported into another
file (by the require statement for example)?

Pseudocode, assuming that only ‘<’ … ‘>’ literals are user definable:

in vector.rb:

class Vector
def initialize x,y,z
@x, @y, z = x, y, z
end
end

in sample.rb:

require ‘vector’
literal Vector.new
<0,1,2>

Is equivalent to Vector.new (0,1,2), but only in this file

and only until it is (possibly) redefined

So custom literals would be per file aliases for <>(*args).

– Nikodemus


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

This idiom is used by Ruby already, for example the Integer and String
functions.

Cheers,
Nat.

Although I like this idiom (and suggested it myself earier, albeit
Vector[1,2,3]), I find it strange to see method names with capital letters.

That is, when I was first trying to determine how the Integer() function works
(e.g. Integer(“0123”) → 83), it took me a significant amount of time and
frustration to track it down.

‘ri Integer’ → Integer class, naturally enough
‘ri Kernel.Integer’ → Integer function

Newbies take note :slight_smile:

Gavin

···

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

Very good point. That just goes to show how hard it is to write code
that is legible to yourself, and easy for others to maintain.

···

On Thu, 2002-10-31 at 00:30, Gavin Sinclair wrote:

Although I like this idiom (and suggested it myself earier, albeit
Vector[1,2,3]), I find it strange to see method names with capital letters.

That is, when I was first trying to determine how the Integer() function works
(e.g. Integer(“0123”) → 83), it took me a significant amount of time and
frustration to track it down.


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