develooper Front page | perl.perl5.porters | Postings from November 2019

Re: Type and Value Constraints and Coercions

Thread Previous | Thread Next
From:
hv
Date:
November 29, 2019 17:12
Subject:
Re: Type and Value Constraints and Coercions
Message ID:
201911291705.xATH5TP26853@crypt.org
Dave Mitchell <davem@iabyn.com> wrote:
:=item isa <Class::Name>
:
:    sub f ($x isa Class::name)
:
:is roughly shorthand for
:
:    sub f ($x where    defined $_
:                    && ref($_)
:                    && $_->isa('Class:name'))
:
:Although if Paul Evan's 'isa' infix keyword is accepted into core, then
:the signature 'isa' trait should become exactly shorthand for:
:
:    sub f ($x where $_ isa Class::Name) { ... }
:
:and any rules regarding whether the class name is quoted and/or
:has a trailing '::' should be the same.

Will this permit '$x isa $expr' (or does it depend on whether we get
Paul's keyword)? If so, is the expression resolved to a constant at
compile time or at runtime?

:=item is <constraint-type-name>
[...]
:This means that (for example), if a Math::BigInt value is passed as
:the argument for $i, the resulting parameter will just be a plain int
:and any extra data or behaviour will have been lost.

I use bigints a lot, and most functions that don't check their params
too carefully work just fine. I'd love to see an easy way to bypass
the coercion when I know what I'm doing, but fear I won't get one.

The majority don't know or care about bigints, so I anticipate that
Int declarations will get sprayed about liberally primarily as a means
of writing more self-documenting code.

Hugo

Thread Previous | 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