class Foo {
private string|int bar;
static create(string|int newBar) {
bar = newBar;
}
}
In other words, as I understand it, you can type the variable bar as
either an int or a string.
Aside from simply, "my $bar;", will we be able to repeat this behavior
in Perl6 programs?
Let me guess, though, this is one of those instances where I should have
read A1..5 and E1..4 more closely. :-)
Well, I'm still hopeful Larry will approve superpositions. In which case,
since types in Perl 6 are first-class, you would be able to write
the same thing something like:
class Foo {
attr any(str,int) $bar;
method SETUP(any(str,int) $newBar) {
$bar = $newBar;
}
}
Damian
I want superpositions too :). But, what would this mean?
my all(str, int) $foo;
#...
Luke
But how on earth would you implement such a thing? :-)
On Fri, Aug 16, 2002 at 04:05:04PM -0700, dam...@conway.org wrote:
> On Fri, 16 August 2002, Luke Palmer wrote:
>
> > I want superpositions too :). But, what would this mean?
> >
> > my all(str, int) $foo;
> > #...
>
> That you need some *serious* psychotherapy! ;-)
"Mr Palmer, the doctor is ready to see you now..."
oops, wrong sort of doctor.
Nicholas Clark
--
Even better than the real thing: http://nms-cgi.sourceforge.net/
> I want superpositions too :). But, what would this mean?
>
> my all(str, int) $foo;
> #...
That you need some *serious* psychotherapy! ;-)
Actually, it would mean that $foo can only contain values
whose type is simultaneously C<str> and C<int>. That is, $foo
can only contain something like C<any(1,"str")>.
Damian
So,
my all(str, int) $foo = $!;
would be fine? I'm forgetting what has been said about $! in p6, of
course, but there you have it.
> But how on earth would you implement such a thing? :-)
I imagine that type specifiers require that values assigned to
the corresponding variable satisfy: value.isa(type). Using a
superposition as a type means that the result of that test comes
back as a superposition too. If that returned superposition is
true, the assignment proceeds.
Damian
> So,
>
> my all(str, int) $foo = $!;
>
> would be fine?
I'd expect so.
> I'm forgetting what has been said about $!
Typically contains an object with both string and integer conversions.
Whether convertability to both types is enough to satisfy a
superpositional type is an interesting question. I suspect it *is*.
Damian
Then I'd assume that multiple inheritance of both types would also
conform? So if $! is Errno, where class Errno is str is int (or is that
Errno is str, int?), that would work too?
Wow. There's more than one way to do it--even static(-ish) typing. Who
woulda thunk it. :-)
Trey
Has any thought yet gone into the builtin Perl types and what they will
be called in Perl 6? Will there be a difference between the C<ref()> of
something and the type(s) that C<isa()> returns?
In keeping with the lower case C<str> and C<int> examples above, will
the builtin Perl types ARRAY, HASH, SCALAR, etc., have lower case
equivalents? Is this an opportunity to replace the ill-named C<ARRAY>
with C<LIST>, or rather C<list>? (I'm sure I've heard Larry say that,
with hindsight, he wishes he had spelled 'ARRAY' as 'LIST', so I hope
I'm not speaking out of line). Could we also come up with a snappier
name than C<SCALAR> to denote a single item reference? Perhaps C<item>,
or just plain C<ref>?
Presumably, C<ref()> would continue to work as it always has, while C<isa()>
returns the most specific type for a variable when called in scalar context,
or a list of the type and all supertypes when called in list context.
var ref isa
-------------------------------------------------
\@foo ARRAY list / ref
\%bar HASH hash / ref
\$baz SCALAR item / ref
my Dog $spot Dog Dog / Animal / obj / ref
"blah blah" - str
3.14 - num
42 - int / num
The only RFC I can find that's related is 224, where Damian talks about
making ref() more magical. http://dev.perl.org/rfc/224.pod
Any other thoughts on this floating around out there?
A
> Then I'd assume that multiple inheritance of both types would also
> conform?
Yes.
> So if $! is Errno, where class Errno is str is int
> that would work too?
I very much doubt you'll be able to inherit from the builtin types.
> (or is that Errno is str, int?),
Multiple inheritance will be:
class Derived is Base1 is Base2
or possibly:
class Derived is Base1 Base2
Damian
How about class Derived is all(Base1, Base2);
--
Piers
"It is a truth universally acknowledged that a language in
possession of a rich syntax must be in need of a rewrite."
-- Jane Austen?
> I want superpositions too :). But, what would this mean?
>
> my all(str, int) $foo;
> #...
Perhaps it would be a way to store both str AND int values in $foo?
I've been wondering how Perl6 will let me do that, anyways (cf.
p5/Scalar::Util).
R.
Close, but no cigar. You meant:
class Derived is any(Base1, Base2);
"Why" is left as an exercise for the reader.
Damian