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
Aaron Sherman
March 30, 2005 08:11
Re: Parameter and trait questions - just how 'only' _is_'read-only'?
Message ID:
On Tue, 2005-03-29 at 16:00 -0700, Luke Palmer wrote:

> Unless the caller can't see the signature, as is the case with methods.

I need to understand this piece.

In this code:

	class X {
		method meth() {...}
	class Y is X {
		method meth() is rw(int $r) {...}
	sub foo { return my Y $tmp }
	my X $a = foo();
	$a.meth() = 8;

What happens? Do we impose a compiler-level restriction on the call that
prevents this, or do we always allow it, and let the run-time take care
of it?

My preference by far is to let the compiler tell you that it's illegal
because there's no valid reason for a sub-class to change the semantics
of a polymorphically invoked method like this. The sub-class might
introduce new semantics, but when manipulated as a base-class those
semantics should only come into play when they are invoked via the
interface of the base class. That sounds like an almost axiomatic truism
to me, but if I read what you wrote correctly, you don't seem to agree.

It's important to look at this as a two(more?)-stage process with
different agendas and limitations, no?

More generally, I think it's always a valid thing for the compiler to
impose the restrictions of the type it knows about, regardless of what
polymorphism might discover later on at run-time. This means that you
can't do this:

	class X { }
	class Y { method m (int $a) {...} }
	my X $ob = get_me_a_Y();

because X does not provide an m.

> Again, this can't be done unless you know the signature.  And in fact,
> we can't do type inference on methods unless we do type inference
> everywhere, which we can't do if we want an autoloader.

You can combine and autoloader with type inference. You may not choose
to, but it's not impossible. You can load the signatures ahead of time,
and link the code at run-time. What gets dicy is this:

	eval "use X";
	my X $a;

I think that's invalid too, but I'm not sure.

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