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            # Rat (rational number)
    (1, 2, 3)       # Parcel

All "normal" built-in types begin with an upper case letter. All "normal" types inherit from Any, and absolutely everything inherits from Mu.

You can restrict the type of values that a variable can hold by adding the type name to the declaration.

    my Numeric $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.

Some types stand for a whole family of more specific types, for example integers (type Int), rationals (type Rat) and floating-point numbers (type Num) conform to the Numeric type.

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

http://perlcabal.org/syn/S02.html#Built-In_Data_Types,

[/perl-5-to-6] Permanent link

comments / trackbacks