raku - 为 Perl 6 NativeCall 结构添加用户模式类型

标签 raku boxing nativecall

Perl 6 docs列出一堆类型。其中一些,例如 Str ,有更复杂的装箱/拆箱行为。

是否可以定义我自己的类型,指定我自己的装箱/拆箱例程?对于一个特定的项目,我有一堆我正在重用的类型,并且基本上一遍又一遍地剪切/粘贴我的访问器功能。

例如,C 结构使用 time_t ,并且我插入访问器方法以去往/来自 DateTime .另一个例子是一个逗号分隔的列表,我想去/从 Array并照顾好 split/join自动地。

有一个更好的方法吗?

编辑 :添加示例:

constant time_t = uint64;
constant FooType_t = uint16;

enum FooType <A B C>;

class Foo is repr('CStruct') is rw
{
    has uint32    $.id;
    has Str       $.name;
    has FooType_t $.type;
    has time_t    $.time;

    method name(Str $n?) {
        $!name := $n with $n;
        $!name;
    }

    method type(FooType $t?) {
        $!type = $t with $t;
        FooType($!type);
    }

    method time(DateTime $d?) {
        $!time = .Instant.to-posix[0].Int with $d;
        DateTime.new($!time)
    }
}

my $f = Foo.new;
$f.id = 12;
$f.name('myname');
$f.type(B);
$f.time(DateTime.new('2000-01-01T12:34:56Z'));

say "$f.id() $f.name() $f.type() $f.time()";

# 12 myname B 2000-01-01T12:34:56Z

这个有效,我可以设置CStruct的各个字段以 Perl 风格的方式(没有左值,但我可以将它们作为参数传入)。

现在我想用 time_t , FooType_t等,用于许多结构中的许多字段,并让它们以相同的方式运行。除了一遍又一遍地复制这些方法之外,还有更好的方法吗?

也许宏可以在这里提供帮助?我还没有掌握它们。

最佳答案

您可以编写一个特征来处理获取或存储属性时的自动属性转换。以下应该让你开始:

multi sub trait_mod:<is>(Attribute:D $attr, :$autoconv!) {
    use nqp;
    my $name := $attr.name;
    $attr.package.^add_method: $name.substr(2), do given $attr.type {
        when .REPR eq 'P6int' {
            method () is rw {
                my $self := self;
                Proxy.new:
                    FETCH => method () {
                        $autoconv.out(nqp::getattr_i($self, $self.WHAT, $name));
                    },
                    STORE => method ($_) {
                        nqp::bindattr_i($self, $self.WHAT, $name,
                            nqp::decont($autoconv.in($_)));
                    }
            }
        }

        default {
            die "FIXME: no idea how to handle {.^name}";
        }
    }
}

例如,以您的用例 time_t :
constant time_t = uint64;

class CTimeConversion {
    multi method in(Int $_ --> time_t) { $_ }
    multi method in(DateTime $_ --> time_t) { .posix }
    method out(time_t $_ --> DateTime) { DateTime.new($_) }
}

class CTimeSpan is repr<CStruct> {
    has time_t $.start is autoconv(CTimeConversion);
    has time_t $.end is autoconv(CTimeConversion);
}

最后,一些示例代码来展示它的工作原理:
my $span = CTimeSpan.new;
say $span;
say $span.end;

$span.end = DateTime.now;
say $span;
say $span.end;

关于raku - 为 Perl 6 NativeCall 结构添加用户模式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43821435/

相关文章:

raku - 如何将 .lines Seq 分配给变量并对其进行迭代?

通过引用传递结构时 C# 框或副本?

Scala 转换为泛型类型

c# - Box 和 Unbox 是什么意思?

casting - Perl 6 nativecast() 到具有 repr ('CPointer' ) 的对象时 GC 会破坏吗?

raku - 如何为返回整个结构的 C 函数编写 Raku 声明?

raku - 为什么我的Promise(启动 block )中的所有Shell进程都不运行? (这是一个错误吗?)

stdout - 如何在 Raku 中截获 Proc::Async 的无缓冲输出?

raku - 使用 Cro run 重建更改的客户端文件

raku - 如何将 CArray[ of-struct] 从 Raku 传递给 C?