How can I rebless an object in Perl 6?

吃可爱长大的小学妹 提交于 2020-01-24 03:11:05

问题


Another question might be "How do I inherit from builtin types?".

I have two questions really, but they are both from the same thing I'm playing with.

First, I can make a subset of a type when I want to constrain it further. I do that with MyInt which accepts anything that is an Int. I declare a variable to by MyInt and assign 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, what I really wanted was a class named MyInt that does the same thing. I might want to add methods to

class MyInt is Int {}   # empty subclass

my MyInt $b = 137;
put 'Name: ', $b.^name;

This almost looks like it works, but I get an error:

Type check failed in assignment to $b; expected MyInt but got Int (137)

I understand what it's saying, but don't understand why I didn't get the same error when I used subset. That's question 1.5.

What I'd really like is the assignment of 137 to automatically turn itself into a MyInt when I assign it. I know that I can explicitly construct it, but it's a bit annoying that the parent class still turns it into an Int instead of using the type of the 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 the new (taken directly from Int.pm), but I'm at a loss about 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 that doesn't retain the value (and I didn't think it would and don't think it should. Looking at Int.pm, I can't see how it stores the value. It looks like it relies on a built-in type and might not be traditionally subclassable:

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 a rebless, but that isn't part of the chain of things avialable to 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

回答1:


Here's a possible solution:

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

Which produces:

MyInt $x = 42

There's probably a cleaner way of doing what you want, but I don't know what it is.

Important update See Raku rebless doesn't work with inhertited classes anymore.



来源:https://stackoverflow.com/questions/44486985/how-can-i-rebless-an-object-in-perl-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!