当前位置:网站首页>D experimental new anomaly
D experimental new anomaly
2022-08-03 01:33:00 【fqbqrr】
原文
Strings are not good,应用变量.很容易创建新类型.如std.algorithmSame as the middle pipe,Implicitly instantiated templates返回值.
我构建了arsd.exception模块,新异常The focus is on improvementenforce.
用法
import exception2;
enum MyError;
enum OtherError;
void main() {
int a;
try {
throw Exception2!MyError(a, "更多信息");
} catch(Exception2!(OtherError) e) {
// won't catch it,Because it's another bug
import std.stdio;writeln("wrong");
} catch(Exception2!(MyError, int) e) {
// 会抓!
import std.stdio;writeln("CAUGHT!");
writeln(e);
}
}
我抛了整/串,But only caught整.Idea details just进一步特化,According to whether感兴趣来处理它们.如果不关心附加数据,Available onlycatch(Exception2!MyError).
or static enumerationMyError构中数据,而不是用MyError来枚举.另一方面,All can be discarded命名空间类型的区分,而只使用Exception2!("somestring",data).
import exception2;
void main() {
int a;
try {
//String error type instead of namespaceDTypes can still have additional information
throw Exception2!"foo bar"(a);
} catch(Exception2!"foo bar") {
//Catch by string
import std.stdio;
writeln("caught");
}
}
我不大喜欢,串不能区分不同库.而使用enum,可通过模块导入和命名空间Rules distinguish.
优点是可不提前声明,可在toss声明,并在抓点确认.I don't really like this style,但与throw new Exception("一些串")一样方便,and data can be attached.
You might counter that,Declare the new class,并用mixin template.声明结构,枚举,并在那里声明相关数据.Not much benefit.
我要说,枚声明,得到了A static list of exception families,然后根据需要添加数据.
考虑InvalidValue族,附加信息,给了int valueGiven, int maxValue,或int valueGiven, int minValue, int maxValue,或string valueGiven, string expectedPattern.一个族,Reasonably附加不同值.
这,也是为何需要结构化数据.
If you get twoint值,哪个是valueGiven,哪个是maxValue?还是给出了两个值(如矩阵坐标)?
use this mechanism,你可
throw Exception2!InvalidValue(MyStruct(structured, information, here);
别人可
catch(Exception2!InvalidValue)
//或
catch(Exception2!(InvalidValue, MyStruct))
to catch clans or details.
仍然工作,只是参数,
struct MyStruct {
}
//从上面,变成下面.
class InvalidValueExceptionWithMyStruct : InvalidValueException {
MyStruct data; mixin ExceptionCtor;s }
当前,异常的信息不足.
我想用IFTI:Implicit function template instantiation,来附加数据,即要创建新子类.它不能用普通的构造函数.但可用opCall.
此外,I also want to make sure派生类可在声明点name outside,This way you can relax抓异常.这排除Anonymous class,but still availableopCall.
深入代码
module exception2;
/+
It is written using long form templates,Because I want to define the parent separately
这样更容易.
+/
template Exception2(alias Type, T...) {
//Each piece of added data is a specialization of the more general case
//This is unchanged through the parent class,But cut out a piece to make it happen
//or as all`Exception2`Generic parent of ,并回退到`Exception`
static if(T.length)
alias Parent = Exception2!(Type, T[0 .. $-1]);
else
alias Parent = Exception;
class Exception2 : Parent {
//Should be named differently,或至少是`const`之类的
//But it saves the data passed at the throw point
T t;
//This is the main entry point for toss,Notice how it looks like`标库`The same as in the factory mode,Take the parameter and forward to the new class
//在`Phobos`used for construction,如,来自`map()`的`MapResult.`
//如果你直接使用`newException2`,All types to be passed must be specified,但通过`opCall`,Implicitly derivable`R`.
//Note that the deduced return value is the full static type passed in.
static opCall(R...)(R r, string file = __FILE__, size_t line = __LINE__) {
return new Exception2!(Type, T, R)(r, "", file, line); //Strings can contain anything
}
//You can't call it directly!I even made it for`保护`
this(T t, string msg, string file = __FILE__, size_t line = __LINE__) {
this.t = t;
static if(is(Parent == Exception))
super(msg, file, line);
else
super(t[0 .. $-1], msg, file, line);
}
//这与旧的arsd.exception`基本相同
override void toString(scope void delegate(in char[]) sink) const {
import std.conv;
sink(typeid(this).name); //待办,Long string of the same name.
sink("@");
sink(file);
sink(":");
sink(to!string(line));
sink("\n");
sink("玩笑");
//这部分是真实的:打印时,Loop through the additional data and display it
foreach(idx, item; t) {
sink("\n");
sink(typeof((cast() this).t[idx]).stringof);
sink(" = ");
sink(to!string(item));
}
if(info) {
try {
sink("\n----------------");
foreach (t; info) {
sink("\n"); sink(t);
}
}
catch (Throwable) {
// Ignore more errors.
}
}
}
}
}
This is the flexible class,It allows you to definecatch(Exception2!X)和throw Exception2!X(data),Return is declaredConstructed for you的More subclasses的对象的opCall.Not the usual staticopCall,It's my old one旧模式,and it works just fine.
我很满意.Tool can be easily used更多结构的throw new Exception("stuff").
边栏推荐
猜你喜欢
随机推荐
Week 7 - Distributional Representations
科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯
# DWD层及DIM层构建## ,220801 ,
Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
js基础知识整理之 —— 获取元素和命名规范
APT level comprehensive free kill with Shell
KubeSphere监控失效为NAN的问题
微信小程序实现lot开发09 接入微信登录
程序员常说的“左手锟斤拷,右手烫烫烫”是怎么回事?
智能电视竞争白热化,利用小程序共建生态突围
ROS2初级知识(9):bag记录过程数据和重放
Let's talk about the charm of code language
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
centos7安装mysql8
程序员的七夕浪漫时刻
测试人生 | 阿里实习 90 天:从实习生的视角谈谈个人成长
APT级全面免杀拿Shell
CTF命令执行题目解题思路
Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
Numpy数组中d[True]=1的含义









