develooper Front page | perl.perl6.language | Postings from March 2005

Re: Adding linear interpolation to an array

Thread Previous | Thread Next
Dave Whipp
March 7, 2005 14:01
Re: Adding linear interpolation to an array
Message ID:
Aldo Calpini wrote:

> I don't think you need "is constant". arguments are readonly by default, 
> unless you give them the "is rw" trait. I guess that "is constant" means 
> that you can specify the index only using a literal, not a variable, eg:
>     @test[1]; # ok, 1 is a costant
>     my $idx = 1;
>     @test[$idx]; # nok, $idx is not a constant

What I was trying to do is to prevent using fractional indices in lvalue 
context. I.e. my role wouldn't be considered for "@x[0.5] = 2".

> but I may be wrong.
>> Does the explicit indexing by an "int" typed value ensure that it'll 
>> be non-recursive under MMD?
> you mean "Num" typed value? if so, I guess using an explicitly 
> non-integer index would make it win under MMD. on the other hand, your 
> method could even not be called at all with an integer index.

That's what I'm hoping. Because my code does integer indexing, and I 
wouldn't want it to call itself. I guess could be explicit:

   $value = @self.Array::postcircumfix«[]»($index);

>> If I later decare a sub as
>>   sub foo ( @in does LinearInterpolation ) { ... }
>> Would I be able to pass a normal (non-interpolating) array to this 
>> sub, and then access it using non-integer indices (i.e. is the data in 
>> the array independent of the interface through wich it is accessed).
> I don't think so. I'm afraid you have to do something like:
>     sub foo (@in) {
>         my @_in = @in;
>         if(! @in.does(LinearInterpolation) ) {
>             @_in does LinearInterpolation;
>         }
>         # go ahead using @_in
>         ...
>     }

I don't see why I need the conditional there. If I'm going to copy the 
array, I might as well declare up front the that darget does 

sub foo (Num @raw_in)
    my Num @in does LinearInterpolation = @raw_in;

Or perhaps even

sub foo (Num @in is copy does LinearInterpolation)


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About