John Williams wrote:
    class Dog {
        has $.foo
            will FETCH { ... }
            will STORE { ... }
        ;
    }

I'm not saying there is anything wrong with that, but John Siracusa is
asking for something different, I think.  A simple accessor which looks
like a method without having to play with Proxy, FETCH, STORE, etc.
If it still looks like $obj.foo = 1 outside the class, that's good too.
(Delphi and C# can do it; why can't we?)

C# does it as


    public int buffersize {
        get {
            return my_buffer.length();
        }
        set {
            //[1]
            double sqrt=Math.sqrt(value);    //value is a keyword in C#
                                             // because of this feature.
            if(Math.floor(sqrt) == sqrt) {
                my_buffer=new byte[value];
            }
            else {
                throw new InvalidArgumentException(
                    "Value is not a power of two"
                );
            }
        }
    }

How different is that from:

    method buffersize()
        will fetch { +$.buffer.bytes }
        will store {
            my $sqrt=$^v.sqrt;
            die "$^v is not a power of two" unless int($sqrt) == $sqrt;
            $.buffer = "\x[0]" x $^v;
        }
    {}

Or even (my suggestion):

    method buffersize()
        will store {
            my $sqrt=$^v.sqrt;
            die "$^v is not a power of two" unless int($sqrt) == $sqrt;
            $.buffer = "\x[0]" x $^v;
        }
    { +$.buffer.bytes }

?

Of course, the best way to implement that would probably be something like:

     our type Buffer ::= ByteString where
         { int sqrt +.bytes == sqrt +.bytes };
     has Buffer $.buffer;

But that's neither here nor there...


[1] I'm certain there's a more efficient way to do this test, probably involving bit twiddling. Whatever.


--
Brent "Dax" Royal-Gordon <[EMAIL PROTECTED]>
Perl and Parrot hacker

Oceania has always been at war with Eastasia.

Reply via email to