develooper Front page | perl.perl6.users | Postings from July 2020

Re: cannot create an instance of subset type

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
July 10, 2020 21:37
Subject:
Re: cannot create an instance of subset type
Message ID:
CAD2L-T2iwTRTOjMgt_MquEFibhD3-B3g8DJxd7mQ5T8h8E1FLw@mail.gmail.com
Subset types are not object types.

A subset is basically a bit of checking code and base type associated with
a new type name.

In something like:

    my ABC $a .= new;

That is exactly the same as:

    my ABC $a = ABC.new;

Well there is no functional `.new` method on any subset types, so
`DEF.new()` isn't going to do anything.

    DEF.new # ERROR

---

The worst part of your code is that you are using a `subset` without a
`where` clause. Which is almost completely pointless.

I honestly think that there is an argument to be made that it shouldn't
even be possible to write a `subset` without a `where` clause.

It isn't like other languages where you can create something like a
lightweight clone or lightweight subclass of a type.
It also isn't an alias.

It's whole purpose is to attach a `where` clause as an extra check to type
based things.



If you want an alias, use an alias

    my constant DEF = ABC;

    my constant DEF = ABC:D;

If you want a lightweight subclass, create a lightweight subclass.

    my class DEF is ABC {}

If you want an extra check, then and only then does it make sense to use a
`subset`.

    my subset GHI of Int where -10 ≤ $_ ≤ 10;

---

A `subset` without a `where` clause only makes certain language features
become unavailable.
Unless that is exactly what you want to accomplish, use something else.

On Fri, Jul 10, 2020 at 3:18 PM Marcel Timmerman <mt1957@gmail.com> wrote:

> Hi,
>
> Using the next code I get an error on the instantiation of $d2;
>
> ---
> use v6;
>
> class ABC {
>    method s ( Int $i ) { say $i + 10; }
> }
>
> subset DEF of ABC;
>
> my ABC $a .= new;
> $a.s(10);   # 20
>
> my DEF $d1 = $a;
> $d1.s(11);  # 21
>
> my DEF $d2 .= new;
> $d2.s(12);
> ---
>
> Error is
>
> You cannot create an instance of this type (DEF)
>    in block <unit> at xt/subset-test.pl6 line 15
>
> Why is this?
>
> Regards,
> Marcel
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About