SlideShare a Scribd company logo
1 of 40
Download to read offline
The Perl6 type system
my $scalar;
our @array;
my %hash;
$scalar = “hi there”;
@array = 1, 2, “a string”;
%hash = one => 1, two => “2”;
my Any $scalar;
our Any @array;
my Any %hash;
$scalar = “hi there”;
@array = 1, 2, “a string”;
%hash = one => 1, two => “2”;
my Int $scalar;
our Int @array;
my Int %hash;
$scalar = “hi there”;
@array = 1, 2, “a string”;
%hash = one => 1, two => “2”;
my Int $scalar;
our Int @array;
my Int %hash;
$scalar = “hi there”;
@array = 1, 2, “a string”;
%hash = one => 1, two => “2”;
Type check failed in assignment to
'$scalar'; expected 'Int' but got 'Str'
in block <unit> at types.pl:4
AST
Any
Array
Associative
Backtrace
Backtrace::Frame
Bag
BagHash
Baggy
Blob
Block
Bool
Buf
Callable
Capture
Channel
Code
Complex
Cool
CurrentThreadScheduler
Cursor
Date
DateTime
Dateish
Duration
Enum
EnumMap
Exception
Failure
FatRat
Grammar
Hash
IO
IO::Handle
IO::Pipe
IO::Socket
Instant
Int
Iterable
Iterator
Junction
Label
List
Lock
Macro
Match
Metamodel::C3MRO
Metamodel::ClassHOW
Metamodel::Finalization
Metamodel::Naming
Metamodel::Primitives
Method
Mix
MixHash
Mixy
Mu
Nil
Num
Numeric
ObjAt
Pair
PairMap
Parameter
Parcel
Pod::Block
Pod::Item
Positional
Proc
Proc::Async
Proc::Status
Promise
Proxy
QuantHash
Range
Rat
Rational
Real
Regex
Routine
Scheduler
Signature
Str
Thread
Version
Whatever
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3); 6
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“3”);
6
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“3”);
6
6
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“3”);
say sum_0_to_n(“foo”);
6
6
sub sum_0_to_n($n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“3”);
say sum_0_to_n(“foo”);
6
6
Cannot convert string to number:
base-10 number must begin with valid
digits or '.' in '⏏foo' (indicated by ⏏)
In method Numeric at m-CORE:1576
In sub infix:<-> at m-CORE:5277
In sub infix:<-> at m-CORE:5277
In sub sub_0_to_n at sum.pl:3
In block <unit> at sum.pl:7
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“foo”);
6
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(“foo”);
6
===SORRY!=== Error while compiling
sum.pl
Calling 'sum_0_to_n' will never work
with argument types (str)
Expected: :(Numeric $n)
at summer.pl:10
------> say ⏏sum_0_to_n("foo");
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
my $arg = “foo”;
say sum_0_to_n($arg);
6
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
my $arg = “foo”;
say sum_0_to_n($arg);
6
Type check failed in binding $n;
expected 'Numeric' but got 'Str'
in sub sum_0_to_n at sum.pl:1
in block <unit> at sum.pl:8
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(3.5);
6
sub sum_0_to_n(Numeric $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(3.5);
6
Hangs… infinite loop
sub sum_0_to_n(Int $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(3.5);
6
sub sum_0_to_n(Int $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(3.5);
6
===SORRY!=== Error while compiling
sum.pl
Calling 'sum_0_to_n' will never work
with argument types (Rat)
Expected: :(Int $n)
at sum.pl:9
sub sum_0_to_n(Int $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
sub sum_0_to_n(Int $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
Hangs… Infinite loop
sub sum_0_to_n(Int $n where * >= 0) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
sub sum_0_to_n(Int $n where * >= 0) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
Constraint type check failed for
parameter '$n'
in sub sum_0_to_n at sum.pl:4
in block <unit> at sum.pl:9
subtype NonNegInt of Int where * >= 0;
sub sum_0_to_n(NonNegInt $n) {
return 0 unless $n;
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
Constraint type check failed for
parameter '$n'
in sub sum_0_to_n at sum.pl:4
in block <unit> at sum.pl:9
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) { 0 }
multi sub sum_0_to_n(NonNegInt $n) {
return $n + sum_0_to_n($n – 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
Constraint type check failed for
parameter '$n'
in sub sum_0_to_n at sum.pl:4
in block <unit> at sum.pl:9
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) { 0 }
multi sub sum_0_to_n(NonNegInt $n) {
return $n + sum_0_to_n($n – 1);
}
multi sub sum_0_to_n(Int $n where * < 0) {
return $n + sum_0_to_n($n + 1);
}
say sum_0_to_n(3);
say sum_0_to_n(-3);
6
-6
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) { 0 }
multi sub sum_0_to_n(NonNegInt $n) {
return $n + sum_0_to_n($n – 1);
}
multi sub sum_0_to_n(Int $n where * < 0) {
return $n + sum_0_to_n($n + 1);
}
sub MAIN(Int :$value!) {
say sum_0_to_n($value);
}
> perl6 sum.pl
Usage:
sum.pl –value=<Int>
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) { 0 }
multi sub sum_0_to_n(NonNegInt $n) {
return $n + sum_0_to_n($n – 1);
}
multi sub sum_0_to_n(Int $n where * < 0) {
return $n + sum_0_to_n($n + 1);
}
sub MAIN(Int :$value!) {
say sum_0_to_n($value);
}
> perl6 sum.pl
Usage:
sum.pl –value=<Int>
> perl6 sum.pl –value=foo
Usage:
sum.pl --value=<Int>
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) { 0 }
multi sub sum_0_to_n(NonNegInt $n) {
return $n + sum_0_to_n($n – 1);
}
multi sub sum_0_to_n(Int $n where * < 0) {
return $n + sum_0_to_n($n + 1);
}
sub MAIN(Int :$value!) {
say sum_0_to_n($value);
}
> perl6 sum.pl
Usage:
sum.pl –value=<Int>
> perl6 sum.pl –value=foo
Usage:
sum.pl –value=<Int>
> perl6 sum.pl – value=4
10
subtype NonNegInt of Int where * >= 0;
multi sub sum_0_to_n(0) returns Int { 0 }
multi sub sum_0_to_n(NonNegInt $n) returns Int {
return $n + sum_0_to_n($n – 1);
}
multi sub sum_0_to_n(Int $n where * < 0) returns Int {
return $n + sum_0_to_n($n + 1);
}
sub MAIN(Int :$value!) {
say sum_0_to_n($value);
}
class Dog {
has Str $.name;
has Int $.age is rw;
}
class Dog {
has Str $.name;
has Int $.age is rw;
}
class Owner {
has Str $.name;
has Dog @.dogs;
}
class Dog {
has Str $.name;
has Int $.age is rw;
}
class Owner {
has Str $.name;
has Dog @.dogs;
}
my $dog = Dog.new(name => 'Fido',
age => 'foo');
Type check failed in assignment to '$!age'; expected 'Int' but got 'Str'
in block at src/gen/m-CORE.setting:1009
in method BUILDALL at src/gen/m-CORE.setting:992
in method bless at src/gen/m-CORE.setting:981
in method new at src/gen/m-CORE.setting:967
in block <unit> at dogs.pl:10
class Dog {
has Str $.name;
has Int $.age is rw;
}
class Owner {
has Str $.name;
has Dog @.dogs;
}
my $dog = Dog.new(name => 'Fido',
age => '2');
$dog.age = “foo”;
Type check failed in assignment to '$!age'; expected 'Int' but got 'Str'
In block <unit> at dogs.pl:11
class Dog {
has Str $.name;
has Int $.age is rw;
}
class Owner {
has Str $.name;
has Dog @.dogs;
}
my $dog = Dog.new(name => 'Fido',
age =>2);
my $owner = Owner.new(name => 'Bob',
dogs => ($dog, 'foo'));
Type check failed in assignment to '@!dogs'; expected 'Dog' but got 'Str'
in method REIFY at src/gen/m-CORE.setting:10861
in method reify at src/gen/m-CORE.setting:9471
in method gimme at src/gen/m-CORE.setting:9954
in method sink at src/gen/m-CORE.setting:10438
in method BUILDALL at src/gen/m-CORE.setting:1083
in method bless at src/gen/m-CORE.setting:981
in method new at src/gen/m-CORE.setting:967
in block <unit> at dogs.pl:11
class Dog {
has Str $.name;
has Int $.age;
}
class Owner {
has Str $.name;
has Dog @.dogs;
}
my $dog = Dog.new(name => 'Fido',
age =>2);
my $owner = Owner.new(name => 'Bob',
dogs => ($dog));
If ($owner ~~ Dog) {
say “owner is-a Dog”;
} elsif ($owner ~~ Owner) {
say “owner is-a Owner”;
} else {
say “owner is-a something else”;
}
given $thing {
when Dog { say '$thing is-a Dog' }
when 3 { say '$thing is 3' }
when "blah" { say '$thing is blah' }
when /pattern/ { say '$thing matches /pattern/' }
when (10 .. 20) { say '$thing is between 10 and 20' }
when any(1, 4, 6) { say '$thing is 1, 4 or 6' }
}
class Dog {
has Str $.name;
has Int $.age;
}
class Owner {
has Str $.name;
has Dog @.dogs;
method ACCEPTS(Dog $d) {
return $d ~~ any(@.dogs);
}
}
class Dog {
has Str $.name;
has Int $.age;
}
class Owner {
has Str $.name;
has Dog @.dogs;
method ACCEPTS(Dog $d) {
return $d ~~ any(@.dogs);
}
}
my $fido = Dog.new(name => 'Fido', age => 3)
my $bob = Owner.new(name => 'Bob', dogs => ($fido));
my $fred = Owner.new(name => 'Fred');
given $fido {
When $fred { say “owned by Fred” }
When $bob { say “owned by Bob” }
}

More Related Content

What's hot

Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP haters
Lin Yo-An
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
Kang-min Liu
 

What's hot (20)

Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP haters
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Business Rules with Brick
Business Rules with BrickBusiness Rules with Brick
Business Rules with Brick
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 
What's New in Perl? v5.10 - v5.16
What's New in Perl?  v5.10 - v5.16What's New in Perl?  v5.10 - v5.16
What's New in Perl? v5.10 - v5.16
 

Similar to The Perl6 Type System

Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
Taras Kalapun
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
tinypigdotcom
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdfphp global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
anjalitimecenter11
 

Similar to The Perl6 Type System (20)

Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty it
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Php functions
Php functionsPhp functions
Php functions
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an array
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
PHP for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python Developers
 
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdfphp global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
php global $bsize,$playerToken,$myToken,$gameOver,$winArr,$rowAr.pdf
 
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)
 
Climbing the Abstract Syntax Tree (IPC Fall 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)Climbing the Abstract Syntax Tree (IPC Fall 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)
 
Climbing the Abstract Syntax Tree (DPC 2017)
Climbing the Abstract Syntax Tree (DPC 2017)Climbing the Abstract Syntax Tree (DPC 2017)
Climbing the Abstract Syntax Tree (DPC 2017)
 
Top 10 php classic traps
Top 10 php classic trapsTop 10 php classic traps
Top 10 php classic traps
 
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
 
wget.pl
wget.plwget.pl
wget.pl
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

The Perl6 Type System

  • 1. The Perl6 type system
  • 2. my $scalar; our @array; my %hash; $scalar = “hi there”; @array = 1, 2, “a string”; %hash = one => 1, two => “2”;
  • 3. my Any $scalar; our Any @array; my Any %hash; $scalar = “hi there”; @array = 1, 2, “a string”; %hash = one => 1, two => “2”;
  • 4. my Int $scalar; our Int @array; my Int %hash; $scalar = “hi there”; @array = 1, 2, “a string”; %hash = one => 1, two => “2”;
  • 5. my Int $scalar; our Int @array; my Int %hash; $scalar = “hi there”; @array = 1, 2, “a string”; %hash = one => 1, two => “2”; Type check failed in assignment to '$scalar'; expected 'Int' but got 'Str' in block <unit> at types.pl:4
  • 7. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3);
  • 8. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); 6
  • 9. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“3”); 6
  • 10. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“3”); 6 6
  • 11. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“3”); say sum_0_to_n(“foo”); 6 6
  • 12. sub sum_0_to_n($n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“3”); say sum_0_to_n(“foo”); 6 6 Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏foo' (indicated by ⏏) In method Numeric at m-CORE:1576 In sub infix:<-> at m-CORE:5277 In sub infix:<-> at m-CORE:5277 In sub sub_0_to_n at sum.pl:3 In block <unit> at sum.pl:7
  • 13. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“foo”); 6
  • 14. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(“foo”); 6 ===SORRY!=== Error while compiling sum.pl Calling 'sum_0_to_n' will never work with argument types (str) Expected: :(Numeric $n) at summer.pl:10 ------> say ⏏sum_0_to_n("foo");
  • 15. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); my $arg = “foo”; say sum_0_to_n($arg); 6
  • 16. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); my $arg = “foo”; say sum_0_to_n($arg); 6 Type check failed in binding $n; expected 'Numeric' but got 'Str' in sub sum_0_to_n at sum.pl:1 in block <unit> at sum.pl:8
  • 17. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(3.5); 6
  • 18. sub sum_0_to_n(Numeric $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(3.5); 6 Hangs… infinite loop
  • 19. sub sum_0_to_n(Int $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(3.5); 6
  • 20. sub sum_0_to_n(Int $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(3.5); 6 ===SORRY!=== Error while compiling sum.pl Calling 'sum_0_to_n' will never work with argument types (Rat) Expected: :(Int $n) at sum.pl:9
  • 21. sub sum_0_to_n(Int $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6
  • 22. sub sum_0_to_n(Int $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6 Hangs… Infinite loop
  • 23. sub sum_0_to_n(Int $n where * >= 0) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6
  • 24. sub sum_0_to_n(Int $n where * >= 0) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6 Constraint type check failed for parameter '$n' in sub sum_0_to_n at sum.pl:4 in block <unit> at sum.pl:9
  • 25. subtype NonNegInt of Int where * >= 0; sub sum_0_to_n(NonNegInt $n) { return 0 unless $n; return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6 Constraint type check failed for parameter '$n' in sub sum_0_to_n at sum.pl:4 in block <unit> at sum.pl:9
  • 26. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) { 0 } multi sub sum_0_to_n(NonNegInt $n) { return $n + sum_0_to_n($n – 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6 Constraint type check failed for parameter '$n' in sub sum_0_to_n at sum.pl:4 in block <unit> at sum.pl:9
  • 27. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) { 0 } multi sub sum_0_to_n(NonNegInt $n) { return $n + sum_0_to_n($n – 1); } multi sub sum_0_to_n(Int $n where * < 0) { return $n + sum_0_to_n($n + 1); } say sum_0_to_n(3); say sum_0_to_n(-3); 6 -6
  • 28. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) { 0 } multi sub sum_0_to_n(NonNegInt $n) { return $n + sum_0_to_n($n – 1); } multi sub sum_0_to_n(Int $n where * < 0) { return $n + sum_0_to_n($n + 1); } sub MAIN(Int :$value!) { say sum_0_to_n($value); } > perl6 sum.pl Usage: sum.pl –value=<Int>
  • 29. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) { 0 } multi sub sum_0_to_n(NonNegInt $n) { return $n + sum_0_to_n($n – 1); } multi sub sum_0_to_n(Int $n where * < 0) { return $n + sum_0_to_n($n + 1); } sub MAIN(Int :$value!) { say sum_0_to_n($value); } > perl6 sum.pl Usage: sum.pl –value=<Int> > perl6 sum.pl –value=foo Usage: sum.pl --value=<Int>
  • 30. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) { 0 } multi sub sum_0_to_n(NonNegInt $n) { return $n + sum_0_to_n($n – 1); } multi sub sum_0_to_n(Int $n where * < 0) { return $n + sum_0_to_n($n + 1); } sub MAIN(Int :$value!) { say sum_0_to_n($value); } > perl6 sum.pl Usage: sum.pl –value=<Int> > perl6 sum.pl –value=foo Usage: sum.pl –value=<Int> > perl6 sum.pl – value=4 10
  • 31. subtype NonNegInt of Int where * >= 0; multi sub sum_0_to_n(0) returns Int { 0 } multi sub sum_0_to_n(NonNegInt $n) returns Int { return $n + sum_0_to_n($n – 1); } multi sub sum_0_to_n(Int $n where * < 0) returns Int { return $n + sum_0_to_n($n + 1); } sub MAIN(Int :$value!) { say sum_0_to_n($value); }
  • 32. class Dog { has Str $.name; has Int $.age is rw; }
  • 33. class Dog { has Str $.name; has Int $.age is rw; } class Owner { has Str $.name; has Dog @.dogs; }
  • 34. class Dog { has Str $.name; has Int $.age is rw; } class Owner { has Str $.name; has Dog @.dogs; } my $dog = Dog.new(name => 'Fido', age => 'foo'); Type check failed in assignment to '$!age'; expected 'Int' but got 'Str' in block at src/gen/m-CORE.setting:1009 in method BUILDALL at src/gen/m-CORE.setting:992 in method bless at src/gen/m-CORE.setting:981 in method new at src/gen/m-CORE.setting:967 in block <unit> at dogs.pl:10
  • 35. class Dog { has Str $.name; has Int $.age is rw; } class Owner { has Str $.name; has Dog @.dogs; } my $dog = Dog.new(name => 'Fido', age => '2'); $dog.age = “foo”; Type check failed in assignment to '$!age'; expected 'Int' but got 'Str' In block <unit> at dogs.pl:11
  • 36. class Dog { has Str $.name; has Int $.age is rw; } class Owner { has Str $.name; has Dog @.dogs; } my $dog = Dog.new(name => 'Fido', age =>2); my $owner = Owner.new(name => 'Bob', dogs => ($dog, 'foo')); Type check failed in assignment to '@!dogs'; expected 'Dog' but got 'Str' in method REIFY at src/gen/m-CORE.setting:10861 in method reify at src/gen/m-CORE.setting:9471 in method gimme at src/gen/m-CORE.setting:9954 in method sink at src/gen/m-CORE.setting:10438 in method BUILDALL at src/gen/m-CORE.setting:1083 in method bless at src/gen/m-CORE.setting:981 in method new at src/gen/m-CORE.setting:967 in block <unit> at dogs.pl:11
  • 37. class Dog { has Str $.name; has Int $.age; } class Owner { has Str $.name; has Dog @.dogs; } my $dog = Dog.new(name => 'Fido', age =>2); my $owner = Owner.new(name => 'Bob', dogs => ($dog)); If ($owner ~~ Dog) { say “owner is-a Dog”; } elsif ($owner ~~ Owner) { say “owner is-a Owner”; } else { say “owner is-a something else”; }
  • 38. given $thing { when Dog { say '$thing is-a Dog' } when 3 { say '$thing is 3' } when "blah" { say '$thing is blah' } when /pattern/ { say '$thing matches /pattern/' } when (10 .. 20) { say '$thing is between 10 and 20' } when any(1, 4, 6) { say '$thing is 1, 4 or 6' } }
  • 39. class Dog { has Str $.name; has Int $.age; } class Owner { has Str $.name; has Dog @.dogs; method ACCEPTS(Dog $d) { return $d ~~ any(@.dogs); } }
  • 40. class Dog { has Str $.name; has Int $.age; } class Owner { has Str $.name; has Dog @.dogs; method ACCEPTS(Dog $d) { return $d ~~ any(@.dogs); } } my $fido = Dog.new(name => 'Fido', age => 3) my $bob = Owner.new(name => 'Bob', dogs => ($fido)); my $fred = Owner.new(name => 'Fred'); given $fido { When $fred { say “owned by Fred” } When $bob { say “owned by Bob” } }