develooper Front page | perl.perl5.porters | Postings from October 2012

Re: sub signatures - type checks

From:
Reini Urban
Date:
October 29, 2012 08:49
Subject:
Re: sub signatures - type checks
Message ID:
CAHiT=DG5SVzozLrMLD+dNppB-0tLnfyspyaihPGqSsjbQfAD3A@mail.gmail.com
On Mon, Oct 29, 2012 at 9:22 AM, Peter Martini <petercmartini@gmail.com> wrote:
> On Mon, Oct 29, 2012 at 10:10 AM, Reini Urban <rurban@x-ray.at> wrote:
>>
>> On Sun, Oct 28, 2012 at 5:07 PM, Peter Martini <petercmartini@gmail.com>
>> wrote:
>> > On Sun, Oct 28, 2012 at 5:57 PM, Eirik Berg Hanssen
>> > <Eirik-Berg.Hanssen@allverden.no> wrote:
>> >>
>> >>   And if it first escapes without strictures, it'll be difficult to add
>> >> them later.  The conceptual space of Perl 5 signatures is valuable real
>> >> estate.  Don't throw it away on something that's bound to remain "less
>> >> than
>> >> signatures".
>> >
>> > Types are going to be far too controversial to do now, so I'm just
>> > asking
>> > here for future-proofing reasons:
>> >
>> > If a sub had a signature like sub foo(Int $bar) {}
>> >
>> > is foo(undef) legal?  And would it coerce it to an int, or leave it as
>> > undef?
>>
>> I don't know about Int, because that looks like a MooseX::Type.
>> I can only tell you about the core types int, str and num, which disallow
>> undef.
>>   sub foo(int $bar) {}
>>   foo(undef) would be a compile-time error.
>
> Is there a way to specify int but also allow undef?  Something like the SQL
> 'NULL / NOT NULL'?
>
> (And I don't mean any particular implementation of types, just fishing for
> general ideas, so since we're talking, let's talk core types)

Not in the coretype int, since it would make type checks slower.
The promoted type Int can hold undef though.
  http://cog.cognitivity.com/perl6/val.html
and it promotes automatically to BigInt, BigRat, ...

qore, which is a fast and typed perl5 variant uses softint for such
weakened core or "primitive" types.
http://www.qore.org/manual/current/lang/html/data_type_declarations.html#softint_type
-- 
Reini Urban
http://cpanel.net/   http://www.perl-compiler.org/



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