Categories
Posts in this category
- Introduction
- Strings, Arrays, Hashes;
- Types
- Basic Control Structures
- Subroutines and Signatures
- Objects and Classes
- Contexts
- Regexes (also called "rules")
- Junctions
- Comparing and Matching
- Containers and Values
- Where we are now - an update
- Changes to Perl 5 Operators
- Laziness
- Custom Operators
- The MAIN sub
- Twigils
- Enums
- Unicode
- Scoping
- Regexes strike back
- A grammar for (pseudo) XML
- Subset Types
- The State of the implementations
- Quoting and Parsing
- The Reduction Meta Operator
- The Cross Meta Operator
- Exceptions and control exceptions
Sun, 21 Sep 2008
Types
Permanent link
NAME
"Perl 5 to 6" Lesson 02 - Types
SYNOPSIS
my Int $x = 3;
$x = "foo"; # error
say $x.WHAT; # 'Int()'
# check for a type:
if $x ~~ Int {
say '$x contains an Int'
}
DESCRIPTION
Perl 6 has types. Everything is an Object in some way, and has a type. Variables can have type constraints, but they don't need to have one.
There are some basic types that you should know about:
'a string' # Str
2 # Int
3.14 # Num
(1, 2, 3) # List
All "normal" built-in types begin with an upper case letter. All "normal" types inherit from Any, and absolutely everything inherits from Object.
You can restrict the type of values that a variable can hold by adding the type name to the declaration.
my Num $x = 3.4;
my Int @a = 1, 2, 3;
It is an error to try to put a value into a variable that is of a "wrong" type (ie neither the specified type nor a subtype).
A type declaration on an Array applies to its contents, so my Str @s is an array that can only contain strings.
Introspection
You can learn about the direct type of a thing by calling its .WHAT method.
say "foo".WHAT; # Str()
However if you want to check if something is of a specific type, there is a different way, which also takes inheritance into account and is therefore recommended:
if $x ~~ Int {
say 'Variable $x contains an integer';
}
MOTIVATION
The type system isn't very easy to grok in all its details, but there are good reasons why we need types:
- Programming safety
-
If you declare something to be of a particular type, you can be sure that you can perform certain operations on it. No need to check.
- Optimizability
-
When you have type informations at compile time, you can perform certain optimizations. Perl 6 doesn't have to be slower than C, in principle.
- Extensibility
-
With type informations and multiple dispatch you can easily refine operators for particular types.
SEE ALSO
Comments / Trackbacks:
Trackback URL:
/blog-en/perl-5-to-6/02-types.trackback
raiph wrote
typos etc.
> if $x ~~ Int { say '$x contains an Int' } my Str $a; $a = "4" but Int; # '$x contains an Int' my SubTypeOfInt $x; # '$x contains an Int' ? > All "normal" builtin types begin with an upper case letter. NOT normal built in types also begin with an upper case letter, right? (Junctions) > and absultely everything inherits from Object. s/absultely/absolutely > It is an error to try to but a value into a variable that is of a "wrong" type (ie neither the specified type nor a subtype). Or that "does" / "buts" the right type, right? > With type informations and multiple dispatch you can easily refine operators for particular types Missing final period.
Moritz wrote
Clarification on Types
Low level types don't begin with upper case letters (int, buf, ...) (but I didn't want to introduce them yet). I'll fix the other typos, thanks.
Write a comment
The comments on this blog post have been disabled; the comment form below will not work.