Re: How do subroutines check types?

2005-07-19 Thread TSa (Thomas Sandlaß)

Ingo Blechschmidt wrote:
How do I have to annotate the type specification in the 
declaration of the subroutine to not include the class Foo, but 
only allow instances of Foo? 


My understanding is that Foo.does(Foo) is false and sub params
are checked with .does(). This automatically excludes class args.
That is you have to explicitly order them:

sub blarb (Foo|Class[Foo] $foo, $arg) # or with CLASS[Foo] ?
{
   ...;   # Do something with instance or class $foo
}

Foo|Class[Foo] is a supertype of Foo and Class[Foo] and as
such allows its subtypes Foo and the Class[Foo] parametric
role instanciation. Note that Foo|Class allows calls like
blarb( Int, "HaHa" ).

Actually I'm not convinced that class Foo should automatically
constitute a (proper) type other than Any. But since .does falls
back to .isa it comes out the same since the only way here to get
a Foo doer is by instanciating the class Foo.
--
TSa (Thomas Sandlaß)




How do subroutines check types?

2005-07-19 Thread Ingo Blechschmidt
Hi, 
 
class Foo {...} 
Foo.new.isa(Foo);   # true 
Foo.isa(Foo);   # true (see [1]) 
Foo.does(Class);# true 
 
sub blarb (Foo $foo, $arg) { 
  ...;   # Do something with instance $foo 
} 
 
blarb Foo.new(...), ...; 
# No problem 
 
blarb Foo,  ...; 
# Problem, as &blarb expects an *instance* of Foo, 
# not the class Foo. 
 
How do I have to annotate the type specification in the 
declaration of the subroutine to not include the class Foo, but 
only allow instances of Foo? 
 
Or is the default way to check the types of arguments 
something like the following, in which case my first question 
doesn't arise? 
 
if $param ~~ $expected_type and not $param ~~ Class { 
  # ok 
} else { 
  die "Type error: ..."; 
} 
 
(But this feels special-casey...) 
 
(And, just curious -- how can I override the default checking 
routine?) 
 
 
--Ingo 
 
[1] http://www.nntp.perl.org/group/perl.perl6.language/0 
 
--  
Linux, the choice of a GNU | Row, row, row your bits, gently down the 
generation on a dual AMD   | stream...   
Athlon!|