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

Re: Is there a way to generate an object without new?

Thread Previous
Larry Wall
October 27, 2005 14:45
Re: Is there a way to generate an object without new?
Message ID:
On Thu, Oct 27, 2005 at 01:30:44PM -0600, Luke Palmer wrote:
: On 10/27/05, Yiyi Hu <> wrote:
: > What I want to do, is a bit like...
: >
: > class A {
: > has $.b;
: > method show { "$.b".say };
: > };
: >
: > A( b => 5 ).show;`
: Yes, I definitely want this to be the way that case type instances are
: created: all those .news everywhere could get annoying.

On the other hand, it potentially makes these mean very different things:

    A @x

and that's a problem.  Semantically, it would certainly be possible
to teach any "kind" to respond to .() and attempt to clone or bless
itself into a new object, but syntactically if we introduce A as a
list operator we can never use it as a bare type mark on a variable
like @x without some declarator out front.  So either we rule out
bare "A $x" as a coercion or cast, or we rule out bare "A" as a list
operator, which would make the parens required.

But even with parens, we can't easily make A(*@args) mean both of
these simultaneously:


Well, okay, maybe if we're really sneaky about our MMD, we could make
that work most of the time.  But it kind of loses the distinction
of making a new A out of something else vs making the current thing
look like an A without necessarily cloning it.  That probably needs
syntactic relief anyway.  Maybe A! is a hard cast and A? is a soft cast.
Needs more waffling.  Er, more thought.

: Anyway, assuming that class and sub namespaces are separate, which may
: be a dubious assumption:
:     our &A ::= &A::new.assuming(A);

The "variables" are separate in the symbol table, but both of them
try to warp the grammar to recognize a bare 'A'.  It's not clear
what will happen in that case.  If the original ^A is really ^*A,
then defining &A will likely just hide the outer meaning of A.  On the
other hand, with our definition of package aliasing, the global name
includes version and author, and the current scope has an A alias to
that long name, and that might collide with the A name of &A in
the current scope.

: Or, more explicitly (and more readably IMO):
:     sub A (\$x) {*$x) }

Sorry, &A is introduced immediately, so you've just written a nice
infinite recursion.  I'd suggest

   sub A (\$x) {*$x) }


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