是否可以在 raku 中定义两个共享相同标识符的枚举?
例如,如果我有以下代码:
#!/usr/bin/raku
use v6;
enum Color <Red Blue>;
enum TrafficLight <Red Green>;
sub MAIN(
Color:D :c(:$color)!, #= the color
TrafficLight:D :t(:$traffic-light)!, #= the traffic-light
) {
say "Selected $color, Selected $traffic-light"
}
Run Code Online (Sandbox Code Playgroud)
您可以在这里看到,标识符 Red 是枚举颜色和枚举 TrafficLight 的一部分。
但是当我执行这个脚本时,我得到了重新声明异常:
$ ./test.p6
Potential difficulties:
Redeclaration of symbol 'Red'
at /home/martin/mnt/release-notes/./scripts/test.p6:5
------> enum TrafficLight <Red Green>?;
Usage:
./scripts/test.p6 -c|--color=<Color> (Blue Red) -t|--traffic-light=<TrafficLight> (Green Red)
-c|--color=<Color> (Blue Red) the color
-t|--traffic-light=<TrafficLight> (Green Red) the traffic-light
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我使用参数执行此脚本时-c=Blue and -t=Red,输出是我所期望的:
$ …Run Code Online (Sandbox Code Playgroud) 我想使用Perl6模块对一些函数进行分组,我经常使用.因为这些函数都是松散耦合的,所以我不喜欢在类中添加它们.
我喜欢use你可以选择哪些函数应该被导入但我不喜欢它的想法,导入的函数然后存储在全局命名空间中.
例如,如果我有一个文件my_util.pm6:
#content of my_util.pm6
unit module my_util;
our sub greet($who) is export(:greet) {
say $who;
}
sub greet2($who) is export(:greet2) {
say $who;
}
sub greet3($who) is export(:greet3) {
say $who;
}
Run Code Online (Sandbox Code Playgroud)
和一个文件test.p6:
#!/usr/bin/perl6
#content of test.p6
use v6.c;
use lib '.';
use my_util :greet2;
greet("Bob"); #should not work (because no namespace given) and also doesn't work
greet2("Bob"); #should not work (because no namespace given) but actually works
greet3("Bob"); #should not work …Run Code Online (Sandbox Code Playgroud) 我想限制Perl6中某些函数的返回类型.我知道,如何推断函数的正确返回类型,在Perl6中返回标量或数组,但我不知道,如果我使用特定类型的散列作为返回值,我该怎么做呢?
示例:可以看到Array方法,可以看到test_arr()Hash方法test_hash().所以我想指定返回值test_hash(),返回A类的哈希值.
#!/usr/bin/env perl6
use v6.c;
use fatal;
class A {
has Str $.member;
}
sub test_arr() returns Array[A] {
my A @ret;
@ret.push(A.new(member=>'aaa'));
return @ret;
}
sub test_hash() { #TODO: add `returns FANCY_TYPE`
my A %ret;
%ret.append("elem",A.new(member=>'aaa'));
%ret.append("b",A.new(member=>'d'));
return %ret;
}
sub MAIN() returns UInt:D {
say test_arr().perl;
say test_hash().perl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我的Perl 6脚本中,我想对标准输入进行(最好是非阻塞)检查以查看数据是否可用.如果是这种情况,那么我想处理它,否则我想做其他的事情.
示例(consumer.p6):
#!/usr/bin/perl6
use v6.b;
use fatal;
sub MAIN() returns UInt:D {
while !$*IN.eof {
if some_fancy_check_for_STDIN() { #TODO: this needs to be done.
for $*IN.lines -> $line {
say "Process '$line'";
}
}
say "Do something Else.";
}
say "I'm done.";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
作为STDIN-Generator我写了另一个Perl6脚本(producer.p6):
#!/usr/bin/perl6
use v6.b;
use fatal;
sub MAIN() returns UInt:D {
$*OUT.say("aaaa aaa");
sleep-until now+2;
$*OUT.say("nbbasdf");
sleep-until now+2;
$*OUT.say("xxxxx");
sleep-until now+2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果consumer.p6按预期工作,它应该产生以下输出,如果通过以下方式调用./producer.p6 | ./consumer.p6 …
我正在使用以下代码来解析 Markdown 文本的子集:
#!/usr/bin/perl6
use v6;
my @tests = '', #should print OK
"\n\n\n", #should print OK
'alpha', #should print OK
'1234', #should print OK
'alpha123', #should print OK
'a simple test without a heading and whitespaces and 123 numbers', #should print OK
'a simple test without a heading with punctuation, whitespaces and 123 numbers.', #should print OK
'a simple test without a heading with punctuation, whitespaces, 123 numbers, quotes\' ", braces (){}<>[], \/.', #should print OK
'a simple test …Run Code Online (Sandbox Code Playgroud) 我想创建一个 raku 语法,可用于解析简化的 Markdown 语法。这种简化的降价语法必须满足以下条件:
为了解析这个语法,我创建了以下脚本:
#!/usr/bin/perl6
use v6;
grammar gram {
token TOP {
<text>
}
token text {
[ <section> ]+
}
token section {
<headline> <textline>*
}
token headline {
^^ [<hashheadline> | <underlineheadline>] $$
}
token hashheadline {
<hashprefix> <headlinecontent>
}
token hashprefix {
[\#] <space>
}
token underlineheadline {
<headlinecontent> [\n] <underline>
}
token underline {
[\-]**2..*
}
token headlinecontent {
[\N]+
}
token textline {
^^ (<[\N]-[\#]> (<[\N]-[\ ]> …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include<chrono>
#include<iostream>
using namespace std::chrono_literals;
#define MSG "hello"
#define DUR 1000ms
class mwe{
public:
static constexpr auto msg = MSG;
static constexpr auto dur_1 = DUR;
static constexpr std::chrono::milliseconds dur_2 = DUR;
static const std::chrono::milliseconds dur_3;
static constexpr decltype(DUR) dur_4 = DUR;
};
constexpr std::chrono::milliseconds mwe::dur_2;
const std::chrono::milliseconds mwe::dur_3 = DUR;
constexpr decltype(DUR) mwe::dur_4;
int main(void) {
std::cout << "str: " << mwe::msg << std::endl;
std::cout << "dur_1: " << mwe::dur_1.count() << std::endl;
std::cout << "dur_2: " << mwe::dur_2.count() …Run Code Online (Sandbox Code Playgroud) 我知道,给定一个特定的函数参数,存在自动类型推导函数模板的可能性,但是对于非类型模板参数是否也存在这样的方法?
例:
#include <iostream>
template<typename T, T val>
void func_a(void) {
std::cout << val << std::endl;
}
template<typename T>
void func_b(T val) {
std::cout << val << std::endl;
}
int main(void) {
func_a<uint32_t, 42u>();
//func_a<42u>(); //This line doesn't work
func_b(42u);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此uint32_t,每当我打电话时,我都不想每次给出模板参数类型func_a().在C++ 17或更低版本中是否存在这样的方法?
我正在使用g ++ v.7.3和c ++ 17.