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

How do subroutines check types?

Thread Next
From:
Ingo Blechschmidt
Date:
July 19, 2005 05:54
Subject:
How do subroutines check types?
Message ID:
loom.20050719T143727-491@post.gmane.org
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/22220 
 
--  
Linux, the choice of a GNU | Row, row, row your bits, gently down the 
generation on a dual AMD   | stream...   
Athlon!                    | 


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About