Documents about ddoc? and markdown in ddoc?
hi, Is there any other documents related about ddoc usage? the only thing I can find is: https://dlang.org/spec/ddoc.html#using_ddoc_to_generate_examples But I found it never mentioned something like $(LI a list item), is there a full ddoc document available? And, is there any info about using markdown in ddoc? thanks! --- dangbinghoo
Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote: On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote: simple example: you can not use functionAttributes from betterC
Re: Access program args outside of main
On Wednesday, 17 October 2018 at 22:37:53 UTC, Stanislav Blinov wrote: On Wednesday, 17 October 2018 at 22:30:31 UTC, Jordan Wilson wrote: Ideally, I'd check args before I take the time to load up data. https://dlang.org/phobos/core_runtime.html#.Runtime Here I was looking through std...thanks mate. Jordan
Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
On Wednesday, 17 October 2018 at 16:50:36 UTC, Paul Backus wrote: You can't append to an array in betterC code, because making space for the new elements requires allocating memory, and that uses the GC. In theory, since you're only using the GC during CTFE, it shouldn't be a problem, but the D compiler doesn't *know* that's what you're doing, so it has to assume the function could be called at runtime--which would be an error. The test2 is not build as source code, like you can use std.* in butterC ctfe function even it use GC. for example you can use this function in betterC if it not build as source code(include from include path): string add_prefix(string exp) { string reg = "^" ~ exp; return reg ; } the dynamic array append working for basic type but not for struct. so I guess this is a dmd front bug.
Re: lazy variables
On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote: lazy S x = () { // do some heavy stuff }(); if (condition) { func(x.y); // heavy stuff evaluated here } auto x = () { // do some heavy stuff }; if (condition) { func(x().y); // heavy stuff evaluated here } If you want to make it a little prettier, you could define a couple helper functions: T delegate() delay(lazy T expr) { return () => expr; } T force(T delegate() thunk) { return thunk(); }
How do you get a .dub project to build / run / debug in Visual D?
I'm referring mainly to the `dagon` game engine. Doing: dub build :tutorial1 dub run :tutorial1 works on windows 10. I'm not sure how to replicate this build process with Visual Studio 2017 project settings. Dagon is building, but I'm getting errors with a basic Hellow World app in Dagon. I don't think it's currently using dub to build.
Re: Which Docker to use?
On Wednesday, 17 October 2018 at 08:08:44 UTC, Gary Willoughby wrote: On Wednesday, 17 October 2018 at 03:37:21 UTC, Ky-Anh Huynh wrote: Hi, I need to build some static binaries with LDC. I also need to execute builds on both platform 32-bit and 64-bit. From Docker Hub there are two image groups: * language/ldc (last update 5 months ago) * dlang2/ldc-ubuntu (updated recently) Which one do you suggest? Thanks a lot. To be honest, you don't need docker for this. You can just download LDC in a self-contained folder and use it as is. https://github.com/ldc-developers/ldc/releases That's what I do on Linux. I need to use docker to build static linked Linux executables. My reason is specific, may be different than the OP's. I'm using Travis-CI to build executables. Travis-CI uses Ubuntu 14.04, but static linking fails on 14.04. The standard C library from Ubuntu 16.04 or later is needed. There may be other/better ways to do this, I don't know.
Re: Do D's std.signals check for already-connected slot and simply ignore the call?
Answer: they don't connect uniquely, you have to manage that yourself.
Re: Access program args outside of main
On Wednesday, 17 October 2018 at 22:30:31 UTC, Jordan Wilson wrote: Ideally, I'd check args before I take the time to load up data. https://dlang.org/phobos/core_runtime.html#.Runtime
Access program args outside of main
Hello, Is there a way to access command line arguments outside of main? // main.d module main; import data; void main(string args[]) { } // data.d module data immutable programData; static this() { // read in data } Ideally, I'd check args before I take the time to load up data. Thanks, Jordan
Re: Can Scope Be Used for Anonymous Objects?
On Wednesday, 17 October 2018 at 20:53:02 UTC, Vijay Nayar wrote: This particular use of "scope" I overheard at the last DConf, and I believe it has been added to the official documentation here: https://dlang.org/spec/expression.html#new_expressions If a NewExpression is used as an initializer for a function local variable with scope storage class, and the ArgumentList to new is empty, then the instance is allocated on the stack rather than the heap or using the class specific allocator. Class-specific allocators (aka overriding 'new') were deprecated a long time ago, and this particular use of 'scope' should follow. Unfortunately, as with many other things in D, allocators, 'scope', DIP1000 etc are somewhere between here and there... I didn't know about the std.typecons scoped, it looks really useful, especially when you want to only create the object when short-circuiting fails, like in the middle of an "||" expression. One drawback of "scoped", however, is that it doesn't appear to warn you if you accidentally let the reference escape out of the function, unlike a scope variable. That's an artifact of it's implementation predating DIP25 and DIP1000, and those DIPs themselves not being realized fully. Why, exactly, is a trivial thing like this even a class? Porting C++ code which unfortunately makes heavy use of inheritance. I originally had this as a struct until I much later stumbled into the other classes that were inheriting from it. Ouch. If there aren't any virtual functions, you could "inherit" via struct inclusion and alias this. Or just take artistic... err... porting license and deviate from the original implementation :)
Re: Can Scope Be Used for Anonymous Objects?
On 10/17/2018 01:53 PM, Vijay Nayar wrote: > https://dlang.org/spec/expression.html#new_expressions > > If a NewExpression is used as an initializer for a function local > variable with > scope storage class, and the ArgumentList to new is empty, then the > instance is > allocated on the stack rather than the heap or using the class > specific allocator. I did not know that. It looks like it's a feature for classes only. Ali
Re: Can Scope Be Used for Anonymous Objects?
On Wednesday, 17 October 2018 at 20:41:24 UTC, Ali Çehreli wrote: On 10/17/2018 01:24 PM, Vijay Nayar wrote: I have a snippet of code like this: scope chordAngle = new S1ChordAngle(_center, other._center); return _radius + other._radius >= chordAngle; The reason the "scope" temporary variable exists is to avoid a heap allocation and instead prefer a value be created on the stack. Is there a way to do this inline? Something like: return _radius + other._radius >= scope new S1ChordAngle(_center, other._center); I think it's possible but what you're looking for is std.typecons.scoped. 'scope' does not do what you think it does. import std.typecons : scoped; class C { int i; this(int i) { this.i = i; } int foo() { return i; } } bool bar() { auto c = scoped!C(42); return 42 == c.foo(); } bool bar_2() { return 42 == scoped!C(42).foo(); } void main() { bar(); bar_2(); } Ali This particular use of "scope" I overheard at the last DConf, and I believe it has been added to the official documentation here: https://dlang.org/spec/expression.html#new_expressions If a NewExpression is used as an initializer for a function local variable with scope storage class, and the ArgumentList to new is empty, then the instance is allocated on the stack rather than the heap or using the class specific allocator. I didn't know about the std.typecons scoped, it looks really useful, especially when you want to only create the object when short-circuiting fails, like in the middle of an "||" expression. One drawback of "scoped", however, is that it doesn't appear to warn you if you accidentally let the reference escape out of the function, unlike a scope variable.
Re: Can Scope Be Used for Anonymous Objects?
On Wednesday, 17 October 2018 at 20:51:29 UTC, Stanislav Blinov wrote: On Wednesday, 17 October 2018 at 20:24:56 UTC, Vijay Nayar wrote: I have a snippet of code like this: scope chordAngle = new S1ChordAngle(_center, other._center); return _radius + other._radius >= chordAngle; The reason the "scope" temporary variable exists is to avoid a heap allocation and instead prefer a value be created on the stack. Is there a way to do this inline? Why, exactly, is a trivial thing like this even a class? Porting C++ code which unfortunately makes heavy use of inheritance. I originally had this as a struct until I much later stumbled into the other classes that were inheriting from it.
Re: Can Scope Be Used for Anonymous Objects?
On Wednesday, 17 October 2018 at 20:24:56 UTC, Vijay Nayar wrote: I have a snippet of code like this: scope chordAngle = new S1ChordAngle(_center, other._center); return _radius + other._radius >= chordAngle; The reason the "scope" temporary variable exists is to avoid a heap allocation and instead prefer a value be created on the stack. Is there a way to do this inline? Why, exactly, is a trivial thing like this even a class?
Re: lazy variables
On 10/17/2018 12:32 AM, aliak wrote: Hi, Is there any notion of lazy vars in D (i see that there're parameters)? i.e: struct S { //... int y; //... } lazy S x = () { // do some heavy stuff }(); if (condition) { func(x.y); // heavy stuff evaluated here } Cheers, - Ali Not very clean but something like this: import std.stdio; struct LazyVar(alias exp) { alias T = typeof(exp()); T value() { static bool initialized = false; static T val; if (!initialized) { val = exp(); initialized = true; } return val; } alias value this; } LazyVar!(() { writeln("Doing heavy stuff"); return 42; }) a; void main() { auto b = a.value;// Must specify .value or int c = a; // must specify type of value (int). // Otherwise, b and c have type LazyVar!(...) // Some more usage b = c = a; assert(b == 42); assert(c == 42); } Ali
Can Scope Be Used for Anonymous Objects?
I have a snippet of code like this: scope chordAngle = new S1ChordAngle(_center, other._center); return _radius + other._radius >= chordAngle; The reason the "scope" temporary variable exists is to avoid a heap allocation and instead prefer a value be created on the stack. Is there a way to do this inline? Something like: return _radius + other._radius >= scope new S1ChordAngle(_center, other._center);
Re: Can Scope Be Used for Anonymous Objects?
On 10/17/2018 01:24 PM, Vijay Nayar wrote: I have a snippet of code like this: scope chordAngle = new S1ChordAngle(_center, other._center); return _radius + other._radius >= chordAngle; The reason the "scope" temporary variable exists is to avoid a heap allocation and instead prefer a value be created on the stack. Is there a way to do this inline? Something like: return _radius + other._radius >= scope new S1ChordAngle(_center, other._center); I think it's possible but what you're looking for is std.typecons.scoped. 'scope' does not do what you think it does. import std.typecons : scoped; class C { int i; this(int i) { this.i = i; } int foo() { return i; } } bool bar() { auto c = scoped!C(42); return 42 == c.foo(); } bool bar_2() { return 42 == scoped!C(42).foo(); } void main() { bar(); bar_2(); } Ali
Re: custom sorting of lists ?
On 10/17/18 2:03 PM, Carl Sturtivant wrote: On Monday, 15 October 2018 at 13:39:59 UTC, Steven Schveighoffer wrote: But that's just the thing -- merge sort *does* depend on the container type. It requires the ability to rearrange the elements structurally, since you merge the sets of items together. This requires making another list from the original list, and ranges don't lend themselves to that. One thing you *can* do is allocate an array beside the original container, and move things back and forth. But this is not required if you have a linked-list type which can simply be restructured without moving. Doesn't this just mean a new special kind of range is needed to be defined? I don't think it fits into range primitives. Basically, I need to rearrange one element from one place to another in O(1) time (and without actually moving/copying the data). This really just is a linked-list special feature. One thing to note is that in a range of T, this move has nothing to do with the T. -Steve
Re: custom sorting of lists ?
On Monday, 15 October 2018 at 13:39:59 UTC, Steven Schveighoffer wrote: But that's just the thing -- merge sort *does* depend on the container type. It requires the ability to rearrange the elements structurally, since you merge the sets of items together. This requires making another list from the original list, and ranges don't lend themselves to that. One thing you *can* do is allocate an array beside the original container, and move things back and forth. But this is not required if you have a linked-list type which can simply be restructured without moving. Doesn't this just mean a new special kind of range is needed to be defined?
Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote: test1: module test1; import test2; enum X = getR(1,3); void main(string[] args){} test2: module test2; struct R { int i; } R[] getR(int a, int b){ R[] r; r ~= R(a); r ~= R(b); return r; } You can't append to an array in betterC code, because making space for the new elements requires allocating memory, and that uses the GC. In theory, since you're only using the GC during CTFE, it shouldn't be a problem, but the D compiler doesn't *know* that's what you're doing, so it has to assume the function could be called at runtime--which would be an error. If you're willing to give up on computing `X` at compile time, you could rewrite getR to allocate manually, with `malloc`: R[] getR(int a, int b) { import core.stdc.stdlib: malloc; R* rp = cast(R*) malloc(2 * R.sizeof); R[] r = rp[0 .. 2]; r[0] = R(a); r[1] = R(b); return r; }
Re: Dealing with ranges where front and popFront do the same logic / eager ranges
On Wednesday, 17 October 2018 at 00:12:13 UTC, H. S. Teoh wrote: I'm not sure what's the reasoning behind the saying that throwing exceptions in ctors is bad, but exceptions are exactly the kind of thing designed for handling this sort of situation. If the parser detects a problem early (i.e., at construction time, rather than the first call to .front), why not report the problem early instead of waiting? I think I must have confused constructors with destructors there. Walter made this comment: "Throwing in a destructor is a nightmare, it makes my brain hurt just trying to figure out what 'should' happen. I've proposed before that destructors should be nothrow." https://issues.dlang.org/show_bug.cgi?id=14903 I couldn't find anything about nothrow constructors, so it might be just fine. Throwing exceptions to early is a problem when you do something like this: ``` auto tokens = Lexer(source).handle!(ParseException, RangePrimitive.front, (e, r) => Token.error); ``` https://dlang.org/phobos/std_exception.html#.handle It's unexpected that the exception will be thrown one token in advance. Interesting solution for your problem with byLine by the way.
Re: why is the default floating point value NAN ?
On Wednesday, 17 October 2018 at 15:51:21 UTC, Codifies wrote: okay I should have carried on reading the blog, its so uninitialized values stick out when debugging... Indeed, the initial value is not supposed to be useful, it's there because dealing with garbage memory when forgetting to initialize a variable is hard to debug. That's also why characters are initialized to 0xFF. Unfortunately, (associative) arrays and integers initialize to an empty array and 0 respectively by a lack of an 'invalid' value. These are often useful initial values, so people who didn't know about / didn't agree with that philosphy started using default initialization. Even the 'Count fequencies of all 2-tuples' example on the dlang homepage uses an uninitialized associative array.
Re: why is the default floating point value NAN ?
On Wednesday, 17 October 2018 at 15:48:16 UTC, Codifies wrote: I'd have thought it ought to be 0.0 ? So far I seen carefully considered and sensible reasons for doing things in D, so why NAN ? You are supposed to initialize your own variables explicitly. NaN is a somewhat easy way to indicate that you forgot to do that.
Re: why is the default floating point value NAN ?
On Wednesday, 17 October 2018 at 15:48:16 UTC, Codifies wrote: I'd have thought it ought to be 0.0 ? So far I seen carefully considered and sensible reasons for doing things in D, so why NAN ? okay I should have carried on reading the blog, its so uninitialized values stick out when debugging...
why is the default floating point value NAN ?
I'd have thought it ought to be 0.0 ? So far I seen carefully considered and sensible reasons for doing things in D, so why NAN ?
Re: Who can stop it ? Help me,thank you.
On Wednesday, 17 October 2018 at 14:06:49 UTC, FrankLike wrote: Where can get the new dmd or ldc2 that's no 'Trojan horse virus' ? https://dlang.org/download.html
Re: Who can stop it ? Help me,thank you.
On Wednesday, 17 October 2018 at 13:50:03 UTC, Stanislav Blinov wrote: On Wednesday, 17 October 2018 at 13:48:04 UTC, FrankLike wrote: What can I do? Delete the bloatware that you downloaded. Where can get the new dmd or ldc2 that's no 'Trojan horse virus' ?
Re: Who can stop it ? Help me,thank you.
On Wednesday, 17 October 2018 at 13:48:04 UTC, FrankLike wrote: What can I do? Delete the bloatware that you downloaded.
Who can stop it ? Help me,thank you.
Hi,teacher: I like D lang,when I download the soft from http://www.360totalsecurity.com/en/, but I find, the link.exe of dmd or ldc2,all have the ‘Trojan horse virus’. dmd.2.082.1.windows.7z:HEUR/QVM19.1.92C9.Malware.Gen file MD5:91ce2a59f06151902a1f3fc49e0a4752 ldc2-7e9db717-windows-x64.7z: HEUR/QVM202.0.92C9.Malware.Gen file MD5:9535728d583e950ea446599b2018cbbd It let me not to use them. What can I do? Help me. Thank you.
Re: Dealing with ranges where front and popFront do the same logic / eager ranges
On Wednesday, 17 October 2018 at 09:53:22 UTC, Dukc wrote: Whatever iterates through the range could then throw an appopriate exception when it encounters an error token. Exceptions in a parser? Monsieur knows his perversion.
Re: Dealing with ranges where front and popFront do the same logic / eager ranges
On Tuesday, 16 October 2018 at 22:59:50 UTC, Dennis wrote: [snip] The first thing to consider for invalid tokens, at least for me, would be to either have popFront set empty to true, or set front to some value representing a parsing error. A programming language parser almost certainly needs to output helpful error messages, so the latter is likely a better approach. Whatever iterates through the range could then throw an appopriate exception when it encounters an error token. I'm not saying your approach is wrong though. It's probably just a matter of taste.
Re: lazy variables
On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote: Hi, Is there any notion of lazy vars in D (i see that there're parameters)? i.e: struct S { //... int y; //... } lazy S x = () { // do some heavy stuff }(); if (condition) { func(x.y); // heavy stuff evaluated here } Cheers, - Ali This might be helpful: https://dlang.org/articles/lazy-evaluation.html
Re: Which Docker to use?
On Wednesday, 17 October 2018 at 03:37:21 UTC, Ky-Anh Huynh wrote: Hi, I need to build some static binaries with LDC. I also need to execute builds on both platform 32-bit and 64-bit. From Docker Hub there are two image groups: * language/ldc (last update 5 months ago) * dlang2/ldc-ubuntu (updated recently) Which one do you suggest? Thanks a lot. To be honest, you don't need docker for this. You can just download LDC in a self-contained folder and use it as is. https://github.com/ldc-developers/ldc/releases That's what I do on Linux.
lazy variables
Hi, Is there any notion of lazy vars in D (i see that there're parameters)? i.e: struct S { //... int y; //... } lazy S x = () { // do some heavy stuff }(); if (condition) { func(x.y); // heavy stuff evaluated here } Cheers, - Ali
Re: how to get UDA only for methods
On Wednesday, 17 October 2018 at 02:54:26 UTC, test wrote: I need get the Route UDA only for method without (static methods, property, constructor, destructor), dont know how to do it. 1) Note that __traits(allMembers, T) gets you a tuple of names, not actual member aliases. 2) Remember there may be overloads 3) Filter the unwanted out: import std.traits; import std.meta : Alias, AliasSeq; import std.stdio; import std.string : startsWith; import std.algorithm : among; alias operatorNames = AliasSeq!("opUnary", "opIndexUnary", "opCast", "opBinary", "opBinaryRight", "opIn", "opIn_r", "opEquals", "opCmp", "opCall", "opAssign", "opIndexAssign", "opOpAssign", "opIndexOpAssign", "opSliceOpAssign", "opIndex", "opSlice", "opDollar", "opDispatch"); void main() { writeln(getRoutes!App); } struct Route { string r; } Route[] getRoutes(T)(){ Route[] routes; foreach (name; __traits(allMembers, T)) { alias member = Alias!(__traits(getMember, T, name)); static if (__traits(isStaticFunction, member) || name.startsWith("__") || name.among(operatorNames)) {} else static if (is(typeof(member) == function)) { foreach (overload; __traits(getOverloads, T, name)) { pragma(msg, name); static foreach(route; getUDAs!(overload, Route)) { routes ~= route; } } } } return routes; } struct App { @Route("xxx") int field; @Route("/blah") this(this) {} @Route("ggg") ~this() {} @Route("/index") void home() {} @Route("/index2") void home(bool overloaded) {} @Route("/blog") void blog(){} }
betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
test1: module test1; import test2; enum X = getR(1,3); void main(string[] args){} test2: module test2; struct R { int i; } R[] getR(int a, int b){ R[] r; r ~= R(a); r ~= R(b); return r; } to build without betterC: ldmd2 ./test.d -I. to build with betterC: ldmd2 ./test.d -I. -betterC If build without betterC, all work fine. if with betterC: test2.d(3): Error: TypeInfo cannot be used with -betterC
Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC
On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote: test2.d(3): Error: TypeInfo cannot be used with -betterC the first problem is the error message is not clear and can be improved. And my question is how to workaround this to make it work with betterC.