How can I undo an object in Perl 6?

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; # Int, but why not MyInt? 

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 {} # empty subclass my MyInt $b = MyInt.new: 137; # Still an Int put 'Name: ', $b.^name; 

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; # let superclass construct it # But, how do I make it the more specific type? } } my MyInt $b = MyInt.new: 137; # Still an Int put 'Name: ', $b.^name; 

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; # let superclass construct it put "v is $v"; # But, how do I make it the more specific type? # $v.bless (doesn't change the type, fails return type check) self.bless; # doesn't retain value } } my MyInt $b = MyInt.new: 137; # Still an Int put 'Name: ', $b.^name; put 'Value: ', $b; # 0 

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; # let superclass construct it put "v is $v"; put "self is " ~ self.^name; put "HOW is " ~ self.HOW.^name; # No such method 'rebless' for invocant # $v.rebless: self.^name; $v.HOW.rebless: self.^name; } } my MyInt $b = MyInt.new: 137; # Still an Int put 'Name: ', $b.^name; put 'Value: ', $b; # 0 
+7
inheritance perl6
source share
1 answer

Here the solution is possible:

 class MyInt is Int { }; my $x = 42; Metamodel::Primitives.rebless: $x, MyInt; dd $x; 

What produces:

 MyInt $x = 42 

There is probably a cleaner way to do what you want, but I don't know what it is.

+3
source share

All Articles