On Wed, Aug 10, 2005 at 10:31:40PM +0200, TSa wrote:
> >    my $x is typed;
> >    $x = "123";
> >    $x = length($x);
> >
> >Would be a type error.  If it's in the dynamic world (as in Perl5),
> >that's just fine.  Does that difference make sense to you?
> 
> The question remains, when exactly does $x acquire its constraint
> and how long does it persist? For my vars it's easy to say: "from
> first assignment of none(Undef).does(Item) to end of scope".
> Which in the above code gives: the first assignment puts a Str
> into $x and if &length returns an Int the second assignment fails.

Hm, in a language with type inference, the process is more like
inventing an anonymous type variable (let's call it ::X for now) for $x,
and noting the three constraints:

    Str.does(::X)   # store from "123"
    ::X.does(Str)   # fetch into length()
    Int.does(::X)   # store from length()

No monotype satisfies the constraints, so unification fails and an
exception is raised.

> This is what I understand. But how usefull is this feature?
> I mean the programmer who reads 'my Int $x' knows that everything
> that is true for an Int is true wherever $x is subsequently used.
> A 'my $x is typed' pretty much means: "read on and find out what
> type $x aquires".

Yes, but without this, you have to write down the full type every time
you declare a variable in order to get any typechecking at all.  In many
cases the compiler already knows it for you.

Really this is about path of least resistance.  Without inference,
we are asking the user to choose between:

    1) Verbose annotation and type safety
    2) Convenience (no annotation) and unsafe behaviour

Adding inference ("is typed") to the mix massively sweetens the first
option, which would be a good thing.

> Do you have a non-trivial application for this in mind?

Yes.  But I can only show trivial examples:

    use traits <typed>;
    my $test = Test::Builder.new();

Now, without inferencing (the first line above), $Test would have to be
dynamically typed (no typechecks possible), essentially forcing the user
who wants typechecks to write out by hand:

    my Test::Builder $test = Test::Builder.new();

This is silly.  Or, take another trivial (but common) example:

    method skip (--> Test::Builder::Test::Skip) {
        my Test::Builder::Test::Skip $skip = Test::Builder::Test::Skip.new;
        my Test::Builder::Test::Base::Status $status = $skip.status;
        $status.do_something; $skip.do_something; # ...
        return $skip;
    }

With inferencing:

    use traits 'typed'; 
    method skip (--> Test::Builder::Test::Skip) {
        my $skip  .= new;
        my $status = $skip.status;
        $status.do_something; $skip.do_something; # ...
        return $skip;
    }

Which language do you want to write in? :-)

> It reminds me a bit to the 'bind a variable once in the constraint
> store' of the constraint programming paradigma.

No, I didn't have that in mind; I think it's just local type inferences.

> But there it serves as guiding the control flow. What is the purpose
> in a typed, imperative language as Perl6?

The purpose is that we don't have to be strong typists to enjoy Strong
Typing.  To make Perl6 easier to type, and easier to Type.

Thanks
,/Autrijus/

Attachment: pgp49kvfyEaqv.pgp
Description: PGP signature

Reply via email to