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

How do subroutines check types?

Thread Next
Ingo Blechschmidt
July 19, 2005 05:54
How do subroutines check types?
Message ID:
    class Foo {...};   # true 
    Foo.isa(Foo);       # true (see [1]) 
    Foo.does(Class);    # true 
    sub blarb (Foo $foo, $arg) { 
      ...;   # Do something with instance $foo 
    blarb, ...; 
    # 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 
Linux, the choice of a GNU | Row, row, row your bits, gently down the 
generation on a dual AMD   | stream...   
Athlon!                    | 

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