Re: Avoiding GC in D and code consistancy

2017-12-31 Thread Tim Hsu via Digitalmars-d-learn
On Sunday, 31 December 2017 at 07:32:50 UTC, Ali Çehreli wrote: On 12/30/2017 11:16 PM, Tim Hsu wrote: > Struct version of Vector3f can't derive toString > method. writeln() prints unformated struct members. I know I can use > helper function here. But is there any other way? The normal way

Re: Avoiding GC in D and code consistancy

2017-12-31 Thread Seb via Digitalmars-d-learn
On Sunday, 31 December 2017 at 07:16:46 UTC, Tim Hsu wrote: I came from C++ looking forward to D. Some languages require programmers to use GC all the time. However, A lot of time we don't really need GC especially when the time of destruction is deterministic in compile time. [...] You

Re: Slices and Dynamic Arrays

2017-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/30/17 8:57 PM, Tony wrote: For me, it is confusing to use "slice" and "dynamic array" as synonyms. My initial impression was that they must have different code underlying them, and different behavior. As stated in the slices article, I think of them as separate -- the slice is the

Re: Slices and Dynamic Arrays

2017-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/30/17 10:08 PM, Ivan Trombley wrote: double[] D = [3.14159]; Can you guess what D is?  :D An approximation of a slice of pi. -Steve

Re: Avoiding GC in D and code consistancy

2017-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/31/17 6:16 AM, Tim Hsu wrote: On Sunday, 31 December 2017 at 07:32:50 UTC, Ali Çehreli wrote: On 12/30/2017 11:16 PM, Tim Hsu wrote: > Struct version of Vector3f can't derive toString > method. writeln() prints unformated struct members. I know I can use > helper function here. But is

Re: Avoiding GC in D and code consistancy

2017-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/31/17 7:50 AM, Steven Schveighoffer wrote: Note, you can use a "sink" version of toString as well, and avoid the gc: void toString(void delegate(const(char)[]) sink) @nogc {     // use formatValue to push into the sink } I guess I'm missing some parameters here, go with what Seb

Re: How do you safely deal with range.front?

2017-12-31 Thread aliak via Digitalmars-d-learn
On Sunday, 31 December 2017 at 01:03:17 UTC, Tony wrote: For me, front() should throw a pre-defined exception when called on an empty range in order to eliminate undefined behavior. It does take some time to make a check, but D does array bounds checking by default. Ideally the front() check

Re: How do you safely deal with range.front?

2017-12-31 Thread ag0aep6g via Digitalmars-d-learn
On 12/31/2017 02:14 PM, aliak wrote: Also, is going out of array bounds well defined behavior in D even with boundscheck off? No. Without the checks you get undefined behavior. I.e., `-boundscheck=off` defeats the `@safe` attribute. For that reason, I'd advise against ever using it.

std.file and non-English filename in Windows

2017-12-31 Thread Domain via Digitalmars-d-learn
In Windows, exists, rename, copy will report file not exists when you input non-English filename, such as Chinese 中文.txt

Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn
On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote: On Sat, 30 Dec 2017 13:07:49 +, Marc wrote: how do I take a symbol as parameter? for example: template nameof(alias S) { import std.array : split; enum nameof = S.stringof.split(".")[$-1]; } Works fine for

Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn
On Sunday, 31 December 2017 at 22:50:12 UTC, Marc wrote: On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote: On Sat, 30 Dec 2017 13:07:49 +, Marc wrote: how do I take a symbol as parameter? for example: [...] Works fine for say a enum member such nameof!(myEnum.X) but

static if and early exit from function doesn't seem to work?

2017-12-31 Thread aliak via Digitalmars-d-learn
Alo! I'm making a recursive concat function that is similar to chain. The following code works: import std.range: isInputRange; auto concat(R, V...)(R range, V values) if (isInputRange!R) { import std.range: chain, ElementType; static if (V.length) { static if

Re: static if and early exit from function doesn't seem to work?

2017-12-31 Thread Colin via Digitalmars-d-learn
On Sunday, 31 December 2017 at 13:32:03 UTC, aliak wrote: Alo! I'm making a recursive concat function that is similar to chain. The following code works: [...] I suspect it's because you've no 'else static if'.

Determine at compile time whether or not an alias is defined in a struct/class

2017-12-31 Thread ktoast via Digitalmars-d-learn
Hello everyone, I started to learn D a few weeks ago and have been stuck trying to solve the following problem : - determine at compile time whether an alias is defined or not in a struct/class. You'll find my attempt thereafter, something similar to how it's done in C++ (using the type

Re: How do you safely deal with range.front?

2017-12-31 Thread Tony via Digitalmars-d-learn
On Sunday, 31 December 2017 at 13:14:10 UTC, aliak wrote: On Sunday, 31 December 2017 at 01:03:17 UTC, Tony wrote: For me, front() should throw a pre-defined exception when called on an empty range in order to eliminate undefined behavior. It does take some time to make a check, but D does

Re: static if and early exit from function doesn't seem to work?

2017-12-31 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 31 December 2017 at 13:32:03 UTC, aliak wrote: So it seems it tries to compile the statements below the check on V.length even though it's guaranteed to be true and there's a return statement inside the if. Yeah, static if includes or excludes code independently at compile time.

Re: Slices and Dynamic Arrays

2017-12-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, December 31, 2017 04:42:01 Tony via Digitalmars-d-learn wrote: > On Sunday, 31 December 2017 at 04:20:28 UTC, codephantom wrote: > > On Sunday, 31 December 2017 at 03:57:17 UTC, Tony wrote: > >> On Sunday, 31 December 2017 at 03:08:05 UTC, Ivan Trombley > >> > >> wrote: > >>> double[] D

Re: Slices and Dynamic Arrays

2017-12-31 Thread Tony via Digitalmars-d-learn
On Sunday, 31 December 2017 at 14:24:40 UTC, Jonathan M Davis wrote: The D Slices article does an excellent job of explaining all of this. It's just that it calls the GC-allocated memory buffer the dynamic array instead of calling T[] the dynamic array like the language and spec do.

Re: Slices and Dynamic Arrays

2017-12-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, December 31, 2017 01:57:58 Tony via Digitalmars-d-learn wrote: > On Friday, 29 December 2017 at 23:13:20 UTC, Jonathan M Davis > > wrote: > > The term "slice" is a bit overused in D, meaning a variety of > > things. It doesn't help that some folks dislike the official > > terminology.

Re: Determine at compile time whether or not an alias is defined in a struct/class

2017-12-31 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/31/17 10:01 AM, ktoast wrote: Hello everyone, I started to learn D a few weeks ago and have been stuck trying to solve the following problem : - determine at compile time whether an alias is defined or not in a struct/class. You'll find my attempt thereafter, something similar to

Re: take symbol as parameter

2017-12-31 Thread Marc via Digitalmars-d-learn
On Saturday, 30 December 2017 at 23:30:02 UTC, rjframe wrote: On Sat, 30 Dec 2017 13:07:49 +, Marc wrote: how do I take a symbol as parameter? for example: template nameof(alias S) { import std.array : split; enum nameof = S.stringof.split(".")[$-1]; } Works fine for

Re: Slices and Dynamic Arrays

2017-12-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, December 31, 2017 14:49:40 Tony via Digitalmars-d-learn wrote: > On Sunday, 31 December 2017 at 14:24:40 UTC, Jonathan M Davis > > wrote: > > The D Slices article does an excellent job of explaining all of > > this. It's just that it calls the GC-allocated memory buffer > > the dynamic

Re: How do you safely deal with range.front?

2017-12-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, December 31, 2017 01:03:17 Tony via Digitalmars-d-learn wrote: > For me, front() should throw a pre-defined exception when called > on an empty range in order to eliminate undefined behavior. It > does take some time to make a check, but D does array bounds > checking by default.

Re: How do you safely deal with range.front?

2017-12-31 Thread Ali Çehreli via Digitalmars-d-learn
On 12/30/2017 11:00 AM, aliak wrote: Instead of this:   auto result = range.op!f;   if (!result.empty) {     result.front.method();   } This:   range.op!f.ifFront.method(); In the above scenario I only want method to be called if the pipeline resulted in any element left in the range.

Re: Slices and Dynamic Arrays

2017-12-31 Thread Tony via Digitalmars-d-learn
On Monday, 1 January 2018 at 02:10:14 UTC, Jonathan M Davis wrote: The DLang Tour should probably be fixed to use the term dynamic array though. Or embrace both terms but take care that it is clear that they are synonyms and one may be preferred depending on context. As a beginner, I had

Re: Slices and Dynamic Arrays

2017-12-31 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, January 01, 2018 05:06:46 Tony via Digitalmars-d-learn wrote: > On Monday, 1 January 2018 at 02:10:14 UTC, Jonathan M Davis wrote: > > The DLang Tour should probably be fixed to use the term dynamic > > array though. > > Or embrace both terms but take care that it is clear that they >