当前位置:网站首页>D the author: d new features
D the author: d new features
2022-08-03 13:15:00 【fqbqrr】
概述
Better readable function
●八进制字面
●0A string that does not need to be allocated at the end
●隐藏类型
●Avoid chaining of memory allocations
●Avoid returning errors
●替换goto的嵌套函数
已有技术
● Half floating point数
● 用CTFE
初化数组
● Generated with an enumeration域名
列表
● 从C对接
D
八进制字面
●0755
●18位字的PDP-10
Only used today文件权限
,But they still fit well.十进制的0755
是什么.
Template literal
// RX to write.
enum RX = octal!755;
pragma(msg, RX);//十进制:493
template octal(int i) {
enum octal = convert(i);
}
int convert(int i) {
return i ? convert(i / 10) * 8 + i % 10 : 0;
}
No built-in literals
● 像binary!1100_1111
的模板字面
.
● 无需
扩展编译器
● 用户可根据需要添加
它们
●不分配0Terminate the string
● 切片
is the description length,不是0终止
● 如何不分配内存
的调用需要0终止
的C函数
?
● Allocate on the stackstringz!
,但是如何
?
auto toCStringThen(alias dg)(const(char)[ ] src) nothrow
{
import dmd.common.string : SmallBuffer;
const len = src.length + 1;
char[512] small = void;
auto sb = SmallBuffer!char(len, small[]);
scope ptr = sb[];
ptr[0 .. src.length] = src[];
ptr[src.length] = '\0';
return dg(ptr);
}
char[ ] name = ... ;
int fd = name.toCStringThen!(
(fname) => open(fname.ptr, O_RDONLY)
);
隐藏类型
auto range(int i, int j) {
struct Result {
int i, j;
bool empty() {
return i == j; }
int front() {
return i; }
void popFront() {
++i; }
}
return Result(i, j);
}
void main() {
foreach (x; range(3, 6))
printf("%d\n", x);
}
//打印:3,4,5
Avoid memory allocation chains
一般:
char[ ] path = "include/";
char[ ] name = "file";
char[ ] ext = ".ext";
char[ ] filename = path ~ name ~ ext;
No distribution method:
import std.stdio;
import std.range : chain;
import std.algorithm.iteration : joiner;
import std.array : array;
import std.utf : byChar;
void main() {
string path = "include/";
string name = "file";
string ext = ".ext";
auto filename = chain(path, name, ext);
writeln(filename); // "include/file.ext"
string f = filename.byChar.array();
writeln(f.length); // 17
writeln(f); // "include/file.ext"
//注意注释.
}
Avoid returning errors
● 异常
昂贵且复杂
● 错误代码
Confused and easy被忽略
● 可选
The return type remains很难看
● 因而...
:Definition does not exist错误
!
搜索子串,但未发现
返回空集
,如零
length string.
NaN
方法
● Floating point representation containsNaN
(非数
)模式
● IEEE754
规范的一部分
● NaN
is generated by any operationNaN
结果
● This way you don't have to check代码错误
NaN
变量
● 有NaN
时发出
错误消息
● NaN
will result in any operationNaN
结果
1,D
编译器从错误
中"恢复
"时使用此方法.
2,Prevent meaningless concatenation错误消息
.
替换char
变量
发现无效Unicode
时,D
倾向于抛异常
.
1,这通常是因为Unicode
数据混乱,2不希望退出
处理,如渲染
Text to be displayed.
● 我的方法是定义
All code points,因此没有错误
● 相反,用U+FFFD
替换Invalid code point
.
Nested function replacementGoto
void plan(int i) {
switch (i) {
case 1:
a();goto L3;
case 2: goto L4;
case 3:e();
L3:b();
L4:
c();
return;
}
}
这样:
void plan(int I){
void doc(){
c();}
void dobc(){
b();doc();}
switch (i) {
case 1:
a();return dobc();
case 2: return doc();
case 3:e();return dobc();
}
}
Half floating point数
● 16位浮点类型
● 16bits are used for economical storage
● 请求按内置类型
,麻烦的是有很多16位
浮点格式
合理的库解决方案
可能吗?
Half-float usage
HalfFloat h = hf!27.2f;
HalfFloat j = cast(HalfFloat)( hf!3.5f + hf!5 );
HalfFloat f = HalfFloat(0.0f);
Half floating point,指数表示为1(符号),5(指数),10(分数)
.
Half floating point implementation
● 存储为short
● 隐式转换HalfFloat
为float
.
● 显式转换float
为HalfFloat
.
struct HalfFloat {
@property float toFloat() {
return shortToFloat(s); }
alias toFloat this;//隐式转换`HalfFloat`为`float`
//Templates avoid implicit conversions`参数`到`浮点`
this(T : float)(T f) {
static assert(is(T == float));
s = floatToShort(f);
}
ushort s = EXPMASK | 1;//.init是HalfFloat.nan
static @property {
HalfFloat min_normal() {
HalfFloat hf = void;hf.s=0x04;return hf;}
HalfFloat max() {
HalfFloat hf = void; hf.s = 0x7BFF; return hf; }
HalfFloat nan() {
HalfFloat hf = void; hf.s = EXPMASK | 1; return hf; }
HalfFloat infinity() {
HalfFloat hf = void; hf.s = EXPMASK; return hf; }
HalfFloat epsilon() {
HalfFloat hf = void; hf.s = 0x14 ; return hf; }
}
enum dig = 3;
enum mant_dig = 11;
enum max_10_exp = 5;
enum max_exp = 16;
enum min_10_exp = -5;
enum min_exp = -14;
ushort s = EXPMASK | 1;//.init是HalfFloat.nan
}
//Half-float literal
template hf(float v)
{
enum hf = HalfFloat(v);
}
HalfFloat h = hf!27.2f;
ShortToFloat()
和floatToShort()
实现
●floating point advantage:圆整-保护位-粘性位-隐藏位
用CTFE初始化数组
int[20] squares=[0,1,4,9,16,25,36,49,64,81,1,121,144,169,196,225,256,289,324,361,];
旧方法:
import core.stdc.stdio;
void main()
{
enum N = 20;
printf("module table;\n");
printf("int[%d] squares = [", N);
foreach (i; 0 .. N) {
printf("%d,", i * i);
}
printf("];\n");
}
//使用:
import table;
新方法
组合λ
和CTFE
:
enum N = 20;
int[N] squares = () {
int[N] squares;
foreach (i; 0 .. N)
squares[i] = i * i;
return squares;
}();//Should be very useful.
Generated with an enumerationA list of domain names
感谢Dennis Korpel
.
struct S
{
bool square : 1,
circle : 1,
triangle : 1;
}
得到想要的:
struct S
{
enum Flags {
Square = 1, Circle = 2, Triangle = 4 }
bool square() {
return !!(flags & Flags.Square); }
bool circle() {
return !!(flags & Flags.Circle); }
bool triangle() {
return !!(flags & Flags.Triangle); }
bool square(bool b) {
b ? (flags |= Flags.Square)
: (flags &= ~Flags.Square); return b; }
bool circle(bool b) {
b ? (flags |= Flags.Circle)
: (flags &= ~Flags.Circle); return b; }
bool triangle(bool b) {
b ? (flags |= Flags.Triangle)
: (flags &= ~Flags.Triangle); return b; }
private ubyte flags;
}
会这样写:
void main()
{
enum F {
square, circle, triangle }
static struct S
{
mixin(generateFlags!(F, ubyte));
}//生成标志.
S s;
s.square = true;
s.circle = false;
s.triangle = true;
assert(s.square == true);
assert(s.circle == false);
assert(s.triangle == true);
}
Generate bit flags:
string generateBitFlags(E, T)() {
string result = "pure nothrow @nogc @safe final {";
enum enumName = __traits(identifier, E);
foreach (size_t i, mem; __traits(allMembers, E)) {
static assert(i < T.sizeof * 8, "字段太多");
enum mask = "(1 << "~i.stringof~")";
result ~= "
bool "~mem~"() const scope {
return !!(flags & "~mask~"); }
bool "~mem~"(bool v) {
v ? (flags |= "~mask~") : (flags &= ~"~mask~");
return v;
}";
}
return result ~ "}\n private "~T.stringof~" flags;\n";
}
导入C
__import stdio;
int main()
{
printf("你好,世界\n");
return 0;
}
如果导入的是D模块?
__import stdio;
__import daction;
int main() {
printf("D函数返回%d\n", action(value)); // 1
return 0;
}
//
module daction;
enum value = 7;
int action(int i) {
return 3 + i; }
重载函数:
__import stdio;
__import daction;
int main()
{
printf("D函数返回%d\n",action(1.0f));//5
return 0;
}
//
module daction;
int action(int i) {
return 3; }
int action(float f) {
return 5; }
模板?
__import stdio;
__import daction;
int main()
{
printf("D函数返回%d\n",action(1));//4
return 0;
}
//
module daction;
int action(T)(T t) {
return cast(int)t.sizeof; }
导入D
模板,补全了循环圈
,即C
也可对接D函数
!
D
的细节使编程
更圆滑!
边栏推荐
猜你喜欢
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
An introduction to basic tools for selecting line tools (package church)
[R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
setTimeout, setInterval requestAnimationFrame
An工具介绍之骨骼工具
Basic principle of the bulk of the animation and shape the An animation tip point
基于php网上零食商店管理系统获取(php毕业设计)
类和对象(中上)
VLAN 实验
[Deep Learning] Overview of Efficient and Lightweight Semantic Segmentation
随机推荐
JS获得浏览器类型
论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
Oracle is installed (system disk) and transferred from the system disk to the data disk
An动画基础之按钮动画与基础代码相结合
Golang GMP 原理
Yahoo!Answers - data set
云计算服务主要安全风险及应对措施初探
Notepad++ 安装jsonview插件
基于php校园医院门诊管理系统获取(php毕业设计)
Using the Work Queue Manager (4)
Golang 通道 channel
An工具介绍之宽度工具、变形工具与套索工具
An动画优化之补间形状与传统补间的优化
shell编程条件语句
技术分享 | 接口自动化测试如何搞定 json 响应断言?
Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
An动画优化之传统引导层动画
【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理
An introduction to the camera
五、函数的调用过程