Another question: "How to inherit from built-in types?".
I actually have two questions, but they are both from the same one I play with.
First, I can make a subset of the type when I want to further restrict it. I do this with MyInt , which accepts all that Int . I declare the variable using MyInt and assign it to it, but when I check its name, I get Int instead. So what's up with that?
subset MyInt where * ~~ Int; my MyInt $b = 137; put 'Name: ', $b.^name;
But I really need a class called MyInt that does the same thing. I might want to add methods
class MyInt is Int {} # empty subclass my MyInt $b = 137; put 'Name: ', $b.^name;
This seems to work, but I get an error:
Type check failed in assignment to $b; expected MyInt but got Int (137)
I understand what he is saying, but I donโt understand why I didnโt get the same error when I used subset . This question is 1.5.
I would really like assignment 137 to be automatically included in MyInt when I assign it. I know that I can build it explicitly, but it is a little annoying that the parent class still turns it into Int instead of using a type of a more derived type:
class MyInt is Int {}
I can override new (taken directly from Int.pm ), but I find it difficult to change the type:
class MyInt is Int { method new ( $value --> MyInt ) { my $v = callsame;
I can bless self, but it doesnโt keep the meaning (and I didnโt think that it would ever be considered). Look Int.pm , I donโt see how it stores the value. It looks like it relies on a built-in type and cannot traditionally be subclassed:
class MyInt is Int { method new ( $value --> MyInt ) { my $v = callsame;
There is rebless , but this is not part of the chain available for Int or ClassHow :
class MyInt is Int { method new ( $value --> MyInt ) { my $v = callsame;