d

enum constructors (creating members of members)

人走茶凉 提交于 2020-01-14 14:40:48
问题 In D, I'm trying to create an enum whose members have members. I can better explain what I'm trying to do with an example, where s and i stand in for the sub-members I'm trying to create: In Python, I can do this: class Foo(enum.Enum): A = "A string", 0 B = "B string", 1 C = "C string", 2 def __init__(self, s, i): self.s = s self.i = i print(Foo.A.s) Java can do something like this: public enum Foo { A("A string", 0), B("B string", 1), C("C string", 2); private final String s; private final

Most efficient portable overflow detection? [duplicate]

泄露秘密 提交于 2020-01-13 08:50:06
问题 This question already has answers here : Catch and compute overflow during multiplication of two large integers (12 answers) Closed 5 months ago . In close to the metal languages like C, C++ and D, what's the most efficient reasonably portable way (i.e. w/o using assembler, though you may assume two's complement arithmetic and wrap-around behavior) to detect overflow of an unsigned 64-bit integer on multiplication? 回答1: You can detect overflow in advance by dividing the maximum value

Simple D program Output order is wrong

非 Y 不嫁゛ 提交于 2020-01-05 22:21:35
问题 I am learning a new language called "D" but i have a problem when trying to write a simple program import std.stdio; void main() { double gradeOne; writeln("Please enter the First Test Grade: "); readf(" s", &gradeOne); } Why does my program ask me for the input first before the output message? I think its just the DDT problem, when i run the program in command prompt its working fine 回答1: Output to Eclipse buffers output by larger data blocks rather than lines. To force output to appear,

Simple D program Output order is wrong

ε祈祈猫儿з 提交于 2020-01-05 22:20:50
问题 I am learning a new language called "D" but i have a problem when trying to write a simple program import std.stdio; void main() { double gradeOne; writeln("Please enter the First Test Grade: "); readf(" s", &gradeOne); } Why does my program ask me for the input first before the output message? I think its just the DDT problem, when i run the program in command prompt its working fine 回答1: Output to Eclipse buffers output by larger data blocks rather than lines. To force output to appear,

“Module x is in x.d which cannot be read” error when compiling a d program

纵饮孤独 提交于 2020-01-05 10:28:23
问题 Here is the project structure: . ├── dub.json ├── dub.selections.json ├── dub.userprefs ├── source │ └── app.d └── testd2 Content of app.d import std.stdio; import scid.matrix; import colorize: fg, color, cwriteln, cwritefln; void main() { cwriteln("This is blue.".color(fg.blue)); auto c = "red"; cwritefln("This is %s".color(c), c); } Content of dub.josn: { "name": "testd2", "description": "A minimal D application.", "copyright": "Copyright © 2014, kaiyin", "authors": ["kaiyin"],

Initializing the D runtime on OS X

a 夏天 提交于 2020-01-04 04:20:12
问题 Edit: this seems to be a longrunning issue with no imminent solution: http://d.puremagic.com/issues/show_bug.cgi?id=8133 http://www.digitalmars.com/d/archives/digitalmars/D/Ideas_for_runtime_loading_of_shared_libraries._154126.html http://lists.puremagic.com/pipermail/dmd-internals/2011-December/002853.html There seems to be a problem with starting the d runtime in a d dylib loaded in a c program. Whenever I call Runtime.initialize() i get a segfault. C code: #include <stdio.h> #include

How does D allow delegates as template parameters?

。_饼干妹妹 提交于 2020-01-03 17:17:13
问题 In "The D Programming Language" by Andrei Alexandrescu, there's an example where a delegate is taken as a template parameter: T[] find(alias pred, T)(T[] input) if(is(typeof(pred(input[0])) == bool)) { for(; input.length > 0; input = input[1 .. $]) { if (pred(input[0])) break; } return input; } unittest { int[] a = [1,2,3,4,-5,3,-4]; int z = -2; auto b = find!(delegate(x) { return x < z; })(a); asssert(b == a[4..$]); } Alexandrescu explains that this works because a delegate is actually a fat

Non-blocking concurrent message receive

自作多情 提交于 2020-01-03 15:39:20
问题 Is there a standard function to receive messages concurrent, but non-blocking? It seems like all the functions available in std.concurrency are blocking and the closest I found to something that is non-blocking is receiveTimeout however it still waits until the timeout. I would like it to return immediately if there are no messages passed to the thread. Here is what I came up with using receiveTimeout . module main; import std.concurrency; import std.stdio : writefln, readln; import core.time

Converting a temporary character array to a string in D

只谈情不闲聊 提交于 2020-01-03 08:52:43
问题 I'm learning D language (I know C++ well)... I want to do some Windows specific stuff so I wrote this just to try out the API: import core.sys.windows.windows; import std.stdio; string name() { char buffer[100]; uint size = 100; GetComputerNameA(&buffer[0], &size); return buffer; } void main() { writeln(name()); } I get in my return statement: test.d(11): Error: cannot implicitly convert expression (buffer) of type char[100] to string Ok, in C++ it would call the constructor to make a string.

Converting a temporary character array to a string in D

吃可爱长大的小学妹 提交于 2020-01-03 08:52:36
问题 I'm learning D language (I know C++ well)... I want to do some Windows specific stuff so I wrote this just to try out the API: import core.sys.windows.windows; import std.stdio; string name() { char buffer[100]; uint size = 100; GetComputerNameA(&buffer[0], &size); return buffer; } void main() { writeln(name()); } I get in my return statement: test.d(11): Error: cannot implicitly convert expression (buffer) of type char[100] to string Ok, in C++ it would call the constructor to make a string.