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

2005-10-27 Thread Larry Wall
On Thu, Oct 27, 2005 at 01:30:44PM -0600, Luke Palmer wrote:
: On 10/27/05, Yiyi Hu <[EMAIL PROTECTED]> 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
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([EMAIL PROTECTED]) mean both of
these simultaneously:

@args[0].as(A)
A.new([EMAIL PROTECTED])

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) { A.new(*$x) }

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

   sub A (\$x) { ::A.new(*$x) }

Larry


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

2005-10-27 Thread Juerd
Matt Fowles skribis 2005-10-27 15:52 (-0400):
> > This is how some other language construct objects, but not how Perl does
> > it. In other words: you should not want this.
> How does that logically follow?

They are two ways of expressing what I think. If they said exactly the
same thing, I wouldn't have felt the need to use both :)


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


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

2005-10-27 Thread Matt Fowles
Juerd~

On 10/27/05, Juerd <[EMAIL PROTECTED]> wrote:
> Yiyi Hu skribis 2005-10-28  3:17 (+0800):
> > class A {
> > has $.b;
> > method show { "$.b".say };
> > };
> > A( b => 5 ).show;`
>
> This is how some other language construct objects, but not how Perl does
> it. In other words: you should not want this.

How does that logically follow?

Matt
--
"Computer Science is merely the post-Turing Decline of Formal Systems Theory."
-Stan Kelly-Bootle, The Devil's DP Dictionary


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

2005-10-27 Thread Juerd
Yiyi Hu skribis 2005-10-28  3:17 (+0800):
> class A {
> has $.b;
> method show { "$.b".say };
> };
> A( b => 5 ).show;`

This is how some other language construct objects, but not how Perl does
it. In other words: you should not want this.

Perhaps it is possible to have a class export a sub to its "use"r.

class A {
has $.b handles { 'show' => 'say' };
eval "sub $?CLASS is export { $?CLASS.new(\$?ARGS) }";
}

Not sure about the existence of $?ARGS, or how else to write it. Well,
@_, but the signature might be different.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


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

2005-10-27 Thread Luke Palmer
On 10/27/05, Yiyi Hu <[EMAIL PROTECTED]> 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.

Anyway, assuming that class and sub namespaces are separate, which may
be a dubious assumption:

our &A ::= &A::new.assuming(A);

Or, more explicitly (and more readably IMO):

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

Luke


Is there a way to generate an object without new?

2005-10-27 Thread Yiyi Hu
What I want to do, is a bit like...

class A {
has $.b;
method show { "$.b".say };
};

A( b => 5 ).show;`


Thanks,

Xinming