在尝试公开localtimePerl 6中的功能时,我似乎做错了什么:
use NativeCall;
my class TimeStruct is repr<CStruct> {
has int32 $!tm_sec;
has int32 $!tm_min;
has int32 $!tm_hour;
has int32 $!tm_mday;
has int32 $!tm_mon;
has int32 $!tm_year;
has int32 $!tm_wday;
has int32 $!tm_yday;
has int32 $!tm_isdst;
has Str $!tm_zone;
has long $!tm_gmtoff;
}
sub localtime(uint32 $epoch --> TimeStruct) is native {*}
dd localtime(time); # segfault
Run Code Online (Sandbox Code Playgroud)
跑下perl6-lldb-m,我得到:
Process 82758 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x5ae5dda1)
frame #0: 0x00007fffe852efb4 libsystem_c.dylib`_st_localsub …Run Code Online (Sandbox Code Playgroud) 此代码不返回任何内容
<foo bar ber>.map: { $^a.comb.map: { $^b.say}};
Run Code Online (Sandbox Code Playgroud)
它包含两个嵌套映射,它们都在接收器上下文中.它应该不起作用,因为在沉没上下文中沉没的列表是无操作.
但是,这有效:
<foo bar ber>.map: *.say; # OUTPUT: «foo?bar?ber?»
Run Code Online (Sandbox Code Playgroud)
它又是接收器上下文中的列表.那它为什么有效呢?
我正在尝试为此代码高尔夫挑战编写一个Perl 6正则表达式,该挑战用规则拆分字符串:
例如:
66667888 -> '66', '66, '7', '888'
19999999179 -> '1', '99', '99', '999', '1', '7', '9'
Run Code Online (Sandbox Code Playgroud)
我以为正则表达式m:g/(.)[$0$0<!$0>|$0?]/可以正常工作,但是在负前瞻中使用捕获似乎会破坏它,而且我不知道如何正确使用它。
根据我的使用方式,它会永远循环,抛出错误 Cannot resolve caller INTERPOLATE_ASSERTION或返回错误的结果。有没有适当的方法可以提前使用捕获,或者这是一个错误?
我试图定义一个子程序Raku,其理由是,比方说,一个阵列的诠释S(强加的约束,即拒绝那些参数不是 ArrayS的IntS)。
问题:实现这一目标的“最佳”方式是什么(最惯用的,或最直接的,或者您认为“最佳”在这里应该是什么意思)?
在RakuREPL 中运行的示例如下。
我所希望的会奏效
> sub f(Int @a) {1}
&f
> f([1,2,3])
Type check failed in binding to parameter '@a'; expected Positional[Int] but got Array ([1, 2, 3])
in sub f at <unknown file> line 1
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
另一个非工作示例
> sub f(@a where *.all ~~ Int) {1}
&f
> f([1,2,3])
Constraint type check failed in binding to parameter '@a'; …Run Code Online (Sandbox Code Playgroud) 似乎 aasub在映射内部使用时无法访问动态变量并且该映射被“返回”。
考虑这段代码:
sub start {
my $*something = 'foobar';
# WORKS
say 'first say-something:';
say-something;
# WORKS
say 'mapped say-something, no return:';
my @foo = (^2).map({say-something});
# ERROR: Dynamic variable $*something not found
say 'mapped say-something, with return:';
return (^2).map({say-something});
}
sub say-something {
say $*something;
1
}
start;
Run Code Online (Sandbox Code Playgroud)
这将输出:
first say-something:
foobar
mapped say-something, no return:
foobar
foobar
mapped say-something, with return:
Dynamic variable $*something not found
in sub say-something at main.raku line 18
in block …Run Code Online (Sandbox Code Playgroud) role R { }
role S does R { }
role T does S { }
my R $r0 = T; # compiles
my R $r1 = role U does R { }; # compiles
my R $r2 = role V does S { }; # compile error:
# Type check failed in assignment to $r2; expected R but got V (V)
Run Code Online (Sandbox Code Playgroud)
任务应该$r2可行吗?
目标是做类似的事情:
my R @r = (
role W does S { ... },
# etc. …Run Code Online (Sandbox Code Playgroud) say <a b c>[1];
Run Code Online (Sandbox Code Playgroud)
我认为相同的优先级适用于所有引用运算符.这有效:
my $string = '5+8i';
my $number = <<$string>>;
say $number;
Run Code Online (Sandbox Code Playgroud)
这会插入$string并创建allomorphes(在本例中为ComplexStr):
(5+8i)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其编入索引,就像文档中的示例一样,它不会编译:
my $string = '5+8i';
my $number = <<$string>>[0];
say $number;
Run Code Online (Sandbox Code Playgroud)
我不太确定Perl 6在这里发生了什么.也许它认为这是一个超级高手:
===SORRY!=== Error while compiling ...
Cannot use variable $number in declaration to initialize itself
at /Users/brian/Desktop/scratch.pl:6
------> say $?number;
expecting any of:
statement end
statement modifier
statement modifier loop
term
Run Code Online (Sandbox Code Playgroud)
我可以跳过变量:
my $string = '5+8i';
say <<$string>>[0];
Run Code Online (Sandbox Code Playgroud)
但这是一个不同的错误,无法找到收尾报价: …
为什么在 Raku 类上可以修改只读数组属性,而另一方面,不能修改标量?
我怎样才能使@.baz“只读”?
class Boo {
has $.bar;
has @.baz;
};
my $boo = Boo.new;
$boo.baz = (1, 2); # works ... ?
say $boo.baz;
$boo.bar = 1; #fails ... expected
Run Code Online (Sandbox Code Playgroud)
我的乐道版本:
This is Rakudo version 2020.05.1 built on MoarVM version 2020.05
implementing Raku 6.d.
Run Code Online (Sandbox Code Playgroud) 在启示录中,有一些关于布尔断言的词:
<( code )> # call code as boolean assertion
Run Code Online (Sandbox Code Playgroud)
但是,我无法让它发挥作用。
say "9471" ~~ m:g/ (\d) <($0 > 5)> /
Run Code Online (Sandbox Code Playgroud)
我希望只匹配大于 5 的数字,但出现编译错误。
哪个是正确的语法(如果存在),或者做一些布尔断言的任何替代方法?
在逗号中选择“将运算符转换为 Unicode”后,输入以下代码:
my @array = 1,3,5,7,9;
say @array >>+>> 3;
Run Code Online (Sandbox Code Playgroud)
>>+>>应该转换为»+»,但什么也没发生。
这是逗号的版本:
Comma 2021.01(社区版)Build #CT-202.6948.81,2021 年 1 月 29 日构建
运行时版本:1.8.0_261-bb12 x86_64
VM:Oracle Corporation 的 Java HotSpot(TM) 64 位服务器 VM。
raku ×10
perl6 ×4
rakudo ×3
regex ×2
class ×1
commaide ×1
constraints ×1
nativecall ×1
oop ×1
subroutine ×1
types ×1
unicode ×1