develooper Front page | perl.perl5.porters | Postings from July 2020

Re: Types in Cor (and Perl)

Thread Previous | Thread Next
Darren Duncan
July 13, 2020 05:02
Re: Types in Cor (and Perl)
Message ID:
On 2020-07-12 7:07 p.m., Branislav ZahradnĂ­k wrote:
> Great point, question IMHO is do we mean/need/want types or constraints?

Yes we definitely do.

> Difference:
> - constraint is evaluated runtime
> - type is declared and trusted

Not necessarily.

I see types and constraints are fundamentally one and the same thing.

A type is fundamentally just a set of values, such as the set of integer values 
or the set of array values or the set of all values.

The most generic form of a type definition is a predicate (boolean-resulting) 
monadic function that takes any value as input and results in true or false if 
the value is a member of the type (set of values) or not.

Whether or not anything is evaluated at runtime or not is entirely dependent on 
whether an input to that evaluation comes from the user or the environment, 
whereas if everything we need to know is available at compile time it CAN be 
evaluated at compile time.  The simplest implementations would evaluate at 
runtime but the more optimized the system is the more it moves to compile time. 
This is related to the concept of folding.

> Child type can via inheritance break a constraint imposed by parent type.

That depends on how the child and parent types are defined.  If the child is 
defines as a subset of the parent, then it can't.

> Examples with primitive types look nice, but ... who uses primitives and strong 
> types ?

Primitives and strong types are very frequently used.

The whole concept of primitive types is mostly arbitrary.  For example, is a 
character string a primitive type or is it an array of characters?

Strong types are not tied to primitive types, they are very separate concepts.

Generally strong typing means that if you declare something expects an actual 
string, it will only accept a string and not just something that can stringify; 
strong typing would require you to explicitly cast the other thing to the 
string.  Whereas weak typing would just let you use the thing that isn't a 
string in contexts that specifically ask for a string.

Don't confuse generic types with weak typing; the former is explicitly saying a 
wider range of values is accepted; the latter is more about ignoring declared 
intentions and pounding square pegs into round holes.

-- Darren Duncan

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