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

Re: Parameter and trait questions - just how 'only' _is_ 'read-only'?

Thread Previous | Thread Next
From:
=?UTF-8?B?VGhvbWFzIFNhbmRsYcOf?=
Date:
March 29, 2005 09:41
Subject:
Re: Parameter and trait questions - just how 'only' _is_ 'read-only'?
Message ID:
42499332.5060305@orthogon.com
Luke Palmer wrote:
> So if you want things modified, you'd have to pass in a reference.
> Arrays and hashes would not generally have this restriction, since we
> pass references of those guys anyway.

But I would really like to see a declaration of any possible modification
in the interface of a sub. Otherwise you might find quite surprising things
in your array, or terribly miss things. So after calling a sub that takes
an array parameter the array argument should be in exactly the same state
as before the call! And any attempt to use modifying operations in the sub
should result in a compile time error. This is good for the optimizer as well.

I don't see the above as a restriction. There are easy ways to achieve
modifyable values by 'is copy' for temporary modifications in the sub and
'is rw' and 'is ref' for full access. But the caller clearly sees that in
the signature. And the typechecker might throw exceptions when type
safety can't be maintained through the sub call:

sub foo (@a is rw) { @a[7] = "heaven" } # type of "" is Str

my Int @i;

foo( @i ); # type error/warning

foo( @i as Array of Any ); # programmer takes responsibility

foo( \@i ); # I'm not sure about that, but could be short of the above
-- 
TSa (Thomas SandlaƟ)




Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About