Re: cannot create an instance of subset type

2020-07-11 Thread Ben Davies
It looks like you're trying to create an alias for a type. I'd use a 
constant for this, not a subset, for reasons Brad has already explained. 
Your code runs fine for me when DEF is written like my constant DEF = ABC.


Re: cannot create an instance of subset type

2020-07-11 Thread Elizabeth Mattijsen
> On 10 Jul 2020, at 23:37, Brad Gilbert  wrote:
> 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.

Making the "where" clause non-optional, is remarkably simple: removing a '?'

However, there appear to be quite a lot of tests in roast that depend on this 
behaviour, so either there *is* a perceived use of subsets without "where" 
clause, or we need to fix roast first.

Re: cannot create an instance of subset type

2020-07-11 Thread Marcel Timmerman

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 > 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  at xt/subset-test.pl6 line 15

Why is this?

Regards,
Marcel





Re: cannot create an instance of subset type

2020-07-10 Thread Brad Gilbert
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  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  at xt/subset-test.pl6 line 15
>
> Why is this?
>
> Regards,
> Marcel
>


cannot create an instance of subset type

2020-07-10 Thread Marcel Timmerman

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  at xt/subset-test.pl6 line 15

Why is this?

Regards,
Marcel