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

Re: Type and Value Constraints and Coercions

Thread Previous | Thread Next
From:
Zefram via perl5-porters
Date:
December 11, 2019 19:28
Subject:
Re: Type and Value Constraints and Coercions
Message ID:
20191211192802.rr7se5ouccmv62wn@fysh.org
I wrote:
>Dave Mitchell wrote:
>>The built-in constraint types will also coerce the resultant parameter
>
>It's a bad idea to mix these separate concerns.  Constraint checking and
>type coercion are different ideas that should remain distinct.

I didn't give any specific plan for what to do about coercion, beyond
this statement that it should be separate from type checking.  As we have
no existing general concept of coercion in Perl 5, and no type system
on which to hang a new coercion mechanism, it seems to me that we're
best not trying to invent such a mechanism.  The job should be left to
general code, of the kind that already exists to perform operations that
amount to coercion.

Thus parameter coercion is best handled without any specific signature
feature, by ordinary code that either mutates the parameter variable or
declares a new variable to hold the coerced value.  Such code can come
straight after the signature, first thing in the body block, but that
would be inconvenient if the default value or type constraint of a later
parameter wants to refer to the coerced value.  But your proposed "?{}"
feature resolves both that and the stylistic issue, by permitting coercion
code to be embedded in the signature.  This is a solid reason to permit
general-purpose code to be embedded in the signature in that manner.

Also note that "//=" performs a kind of coercion.  As I already expressed
in another message, I think this operation on parameter variables is also
best handled via "?{}" rather than through a specific signature feature.

-zefram

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