From: Nat Pryce Date: 2002-10-31T01:33:47+09:00 Subject: Re: feature idea: custom literals I see. I think this is pretty superfluous, and also dramatically reduces the legibility of the code, especially if 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