不同D编译器的优缺点是什么?性能和标准合规性/ D2如何支持?调试器的支持程度如何?错误消息有多好,IDE集成是什么?64位支持有多好?到目前为止我的想法:
DMD
GDC
LDC
死了/不工作
我正在考虑针对ARM,我认为GDC是首选工具,但我不确定.
这是C#中的一个简单生成器.
IEnumerable<int> Foo()
{
int a = 1, b = 1;
while(true)
{
yield return b;
int temp = a + b;
a = b;
b = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在Digital Mars D中编写类似的发生器?
(问题是关于收益率报表)
谢谢!
更新.那很有意思.由于我只是生成一个数学序列,因此使用递归可能是一个不错的选择.
auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);
foreach (e; take(fib, 10)) // <- prints first ten numbers from the sequence
{
writeln(e);
}
Run Code Online (Sandbox Code Playgroud) 有一段时间我对D'运算符重载的方向感到困惑,但现在我意识到它是一个漂亮的系统...如果它只适用于核心类型(int,float等).考虑以下代码:
struct Vector {
float X, Y;
void opOpAssign(string op)(Vector vector) {
X.opOpAssign!op(vector.X); // ERROR: no property "opOpAssign" for float
Y.opOpAssign!op(vector.Y); // ERROR: ditto
}
}
Run Code Online (Sandbox Code Playgroud)
这将是漂亮的代码,如果它工作,看到它在一个方法中重载所有+ =, - =,*=等运算符.但是,正如您所看到的,它不是开箱即用的.我用模板创建了一个解决方案(上帝,我爱D):
template Op(string op, T) {
void Assign(ref T a, T b) {
static if (op == "+") a += b;
else if (op == "-") a -= b;
else if (op == "*") a *= b;
else if (op == "/") a /= b;
}
}
struct Vector {
float …
Run Code Online (Sandbox Code Playgroud) 我想尝试D,但我不太确定使用什么编译器.我在这个主题上找到了一些文章和SO问题,但我没有找到任何最新的文章.
每个编译器有什么好处,有什么缺点?现在DMD编译器对我来说似乎是最好的,但我可能会被过时的信息误导.
不久前我买了"D编程语言".好书,很有教育意义.但是,我在编写本书中列出的语言功能时遇到了麻烦:扩展功能.
在书中,Andrei写道任何函数(a,b)都可以调用,如:a.function(b); 所以我应该能够做到这一点:
struct Person {
string name;
}
void foo(Person person, string name) {
person.name = name;
}
void main() {
auto bob = Person();
bob.foo("Bob Dole"); // ERROR: Person does not have method 'foo'
}
Run Code Online (Sandbox Code Playgroud)
正确?这个功能还没有实现,或者我只是遗漏了什么?我注意到导入std.range会向数组添加方法,因此它似乎在某种程度上实现.
我使用Windows安装程序安装了dmd(2.0?),并尝试编译以下程序:
module tcpechoserver;
import std.stdio;
const int MAXPENDING = 5;
int main(char[][] argv)
{
if(argv.length != 2){
writef("Usage: %s <port>", argv[0]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我得到以下编译器错误:
Error: module stdio cannot read file 'std\stdio.d'
Run Code Online (Sandbox Code Playgroud)
是否有一些路径我必须指定才能使标准库工作?
我正在尝试在我的win32 D1-Tango设置上安装DMDScript-tango.我使用的版本是0.99.9 Kai包.
当我尝试构建它时,我得到以下错误(以及其他)
C:\DMD\sources\dmdscript>dsss build
Creating imports for dmdscript_tango
dmdscript_tango => dmdscript_tango
dmdscript_tango\script.d(24): module ctype cannot read file 'std\ctype.d'
Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting.
Error: Command failed, aborting.
C:\DMD\sources\dmdscript>
Run Code Online (Sandbox Code Playgroud)
它看起来仍然取决于一些phobos代码 - 但由于DMDScript源提到tangobos,我想我必须安装它才能使DMDScript工作.
从我所看到的情况来看,Tangobos这些日子似乎与探戈捆绑在一起,但我似乎无法让它发挥作用.我将不得不假设页面已经过时,而tangobos实际上并没有提供0.99.9的捆绑.
所以,我想我会从svn.dsource.org获取最新的tangobos,但是当我尝试构建它时,我收到了这个错误:
C:\DMD\sources\tangobos>dsss build
Creating imports for crc32
Creating imports for tangobos
Creating imports for tangobos-etc
crc32.d => crc32
Error: no object files to link
Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting.
Error: Command failed, aborting.
C:\DMD\sources\tangobos>
Run Code Online (Sandbox Code Playgroud)
我对这些链接器错误很无能为力.所以我无法弄清楚如何解决这个问题.你可以在这里看到dsss.conf …
D的模板文档包含一个名为"模板构造函数"的小部分.该部分没有任何示例或大量文档.
我正在尝试使用该功能(我知道我可以使用"静态构造函数",但我有理由更喜欢模板构造函数).
特别是,我试图在编译时生成一些哈希值.这是我的尝试:
struct MyHash
{
uint value;
this(uint value)
{
this.value = value;
}
this(string str)()
{
enum h = myHashFunc(str);
return MyHash(h);
}
}
uint myHashFunc(string s)
{
// Hashing implementation
return 0;
}
int main(string[] str)
{
MyHash x = MyHash!"helloworld";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不能用DMD 2.053编译:
x.d(10): Error: template x.MyHash.__ctor(string str) conflicts with constructor x.MyHash.this at x.d(5)
Run Code Online (Sandbox Code Playgroud)
它抱怨第一个构造函数.删除后:
x.d(20): Error: template instance MyHash is not a template declaration, it is a struct
Run Code Online (Sandbox Code Playgroud)
考虑到我使用的语法与MyHash是模板结构相同,这是非常合乎逻辑的.
那么,有谁知道如何声明并调用"模板构造函数"?
我疯了,因为我无法在屏幕上显示一组简单的三角形.
我正在使用OpenGL3(没有弃用的固定管道)使用D编程语言的废弃绑定.
你能发现以下程序中的错误吗?它编译得很好,不会抛出任何OpenGL/GLSL错误.它只显示我设置的清晰颜色的空白屏幕.
import std.string;
import std.conv;
import derelict.opengl3.gl3;
import derelict.sdl2.sdl2;
immutable string minimalVertexShader = `
#version 120
attribute vec2 position;
void main(void)
{
gl_Position = vec4(position, 0, 1);
}
`;
immutable string minimalFragmentShader = `
#version 120
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;
void main() {
DerelictSDL2.load();
DerelictGL3.load();
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
throw new Exception("Failed to initialize SDL: " ~ to!string(SDL_GetError()));
}
// Set OpenGL version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// Set OpenGL …
Run Code Online (Sandbox Code Playgroud) 我是D新编程的新手.选择DMD(2.061)或GDC(4.6,4.7或4.8,快照)的优缺点是什么.我应该选择哪种GDC版本?我已成功构建了GCC-4.8和GDC-4.8的最新快照,并编译了一个hello world程序.
以下是我对专业人士的看法:
如何通过GDB调试支持 - GDC和DMD之间有什么不同?
d ×10
dmd ×10
gdc ×5
compile-time ×1
debugging ×1
javascript ×1
opengl-3 ×1
tango ×1
templates ×1
version ×1