On 2020-07-10 23:37, Brad Gilbert wrote:
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.

Thanks very much Brad, for your answer. I understand completely. It was needed to mimic a C typedef which created a new type name from another. The example 'my constant DEF = ABC;' above would just do fine. I didn't thought about 'my class DEF is ABC {}' though, which also would work but creates more code I presume.

What I understand also now is that a subset is used on places where variables are assigned or bound like in argument lists and never created anew. Then, like you said, the where clause is always useful, if not obligatory, otherwise one could use the original type.

Regards,
Marcel

On Fri, Jul 10, 2020 at 3:18 PM Marcel Timmerman <mt1...@gmail.com <mailto:mt1...@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


Reply via email to