Re: Avoiding GC in D and code consistancy

2017-12-30 Thread Ali Çehreli via Digitalmars-d-learn
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 that I know is to insert a function like the following into

Avoiding GC in D and code consistancy

2017-12-30 Thread Tim Hsu via Digitalmars-d-learn
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. I found that struct in D is allocate on stack by default. And we can use

Re: Slices and Dynamic Arrays

2017-12-30 Thread Tony via Digitalmars-d-learn
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 = [3.14159]; Can you guess what D is? :D It took me a while but I finally came up with "a

Re: Slices and Dynamic Arrays

2017-12-30 Thread codephantom via Digitalmars-d-learn
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 = [3.14159]; Can you guess what D is? :D It took me a while but I finally came up with "a slice of pi" a slice of pi is irrational.

Re: Slices and Dynamic Arrays

2017-12-30 Thread Tony via Digitalmars-d-learn
On Sunday, 31 December 2017 at 03:08:05 UTC, Ivan Trombley wrote: double[] D = [3.14159]; Can you guess what D is? :D It took me a while but I finally came up with "a slice of pi"

Re: Slices and Dynamic Arrays

2017-12-30 Thread Ivan Trombley via Digitalmars-d-learn
double[] D = [3.14159]; Can you guess what D is? :D

Re: Slices and Dynamic Arrays

2017-12-30 Thread Tony via Digitalmars-d-learn
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. In general, a slice is a contiguous group of elements. A slice of memory would be a

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

2017-12-30 Thread Tony via Digitalmars-d-learn
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 could be turned off somehow ("-boundschecks=off") by the

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

2017-12-30 Thread Dukc via Digitalmars-d-learn
On Saturday, 30 December 2017 at 19:00:07 UTC, aliak wrote: Instead of this: auto result = range.op!f; if (!result.empty) { result.front.method(); } This: range.op!f.ifFront.method(); Ah, so you don't have a problem with checking emptiness but you want to do it inside the

Re: take symbol as parameter

2017-12-30 Thread rjframe via Digitalmars-d-learn
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 say a enum member such nameof!(myEnum.X) but this:

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

2017-12-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, December 30, 2017 19:11:20 aliak via Digitalmars-d-learn wrote: > On Friday, 29 December 2017 at 20:33:22 UTC, Jonathan M Davis > > wrote: > > Well, I don't know what you're really doing in code here, but > > in general, you'd write your code in a way that it checks empty > > and

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

2017-12-30 Thread Mengu via Digitalmars-d-learn
On Saturday, 30 December 2017 at 19:00:07 UTC, aliak wrote: On Friday, 29 December 2017 at 20:47:44 UTC, Dukc wrote: [...] Hmm, interesting. Not sure that's what I'm looking for but I like it anyway :) I'm more looking to deal with situations like this: Instead of this: auto result =

Re: Is Nullable supposed to provide Optional semantics?

2017-12-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/30/17 3:59 AM, Chris Paulson-Ellis wrote: On Friday, 29 December 2017 at 22:08:59 UTC, vit wrote:     n = Nullable!Object.init;     assert(n.isNull == true); [...] more: https://forum.dlang.org/thread/jrdedmxnycbqzcpre...@forum.dlang.org?page=1 Thanks. No-one in the linked thread

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

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:33:22 UTC, Jonathan M Davis wrote: Well, I don't know what you're really doing in code here, but in general, you'd write your code in a way that it checks empty and simply doesn't try to do anything with front if the range is empty. Yes, ideally, if

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

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:47:44 UTC, Dukc wrote: On Friday, 29 December 2017 at 19:38:44 UTC, aliak wrote: So when I'm dealing with ranges, there're a number of times where I get the front of the returned result of a set of operations, but of course there is no front so you get an

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

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:11:03 UTC, Seb wrote: On Friday, 29 December 2017 at 19:38:44 UTC, aliak wrote: Hi, So when I'm dealing with ranges, there're a number of times where I get the front of the returned result of a set of operations, but of course there is no front so you get an

Re: structs inheriting from and implementing interfaces

2017-12-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/29/17 7:03 AM, Mike Franklin wrote: Is that simply because it hasn't been implemented or suggested yet for D, or was there a deliberate design decision? It was deliberate, but nothing says it can't actually be done. All an interface call is, is a thunk to grab the actual object, and

Re: Avoiding default generic types, and allocator awareness

2017-12-30 Thread helxi via Digitalmars-d-learn
On Saturday, 30 December 2017 at 15:00:32 UTC, helxi wrote: As an exercise in http://ddili.org/ders/d.en/pointers.html, I was implementing a very stripped down version of singly linked list like below: struct Node(T) { T item; Node!T* next_item; } [...] Correction, I meant: If I

Avoiding default generic types, and allocator awareness

2017-12-30 Thread helxi via Digitalmars-d-learn
As an exercise in http://ddili.org/ders/d.en/pointers.html, I was implementing a very stripped down version of singly linked list like below: struct Node(T) { T item; Node!T* next_item; } string to_string(T)(in Node!T node) { import std.format; return node.nextItem ? "%s ->

take symbol as parameter

2017-12-30 Thread Marc via Digitalmars-d-learn
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 say a enum member such nameof!(myEnum.X) but this: struct S { int v; } S s;

Re: Calling a d-style-variadic-function from another

2017-12-30 Thread John Chapman via Digitalmars-d-learn
On Saturday, 30 December 2017 at 10:14:35 UTC, tipdbmp wrote: // how can I adjust _argptr? _argptr = ??? Try this: _argptr = *cast(va_list*)_argptr;

Calling a d-style-variadic-function from another

2017-12-30 Thread tipdbmp via Digitalmars-d-learn
import core.vararg; string foo(fmt, ...) { int args_len = _arguments.length; if (args_len == 1) { if (typeid(_arguments[0] == typeid(TypeInfo[]))) { _arguments = va_arg!(TypeInfo[])(_argptr); args_len = _arguments.length; // how can I adjust

Re: Is Nullable supposed to provide Optional semantics?

2017-12-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, December 30, 2017 08:59:40 Chris Paulson-Ellis via Digitalmars- d-learn wrote: > On Friday, 29 December 2017 at 22:08:59 UTC, vit wrote: > > n = Nullable!Object.init; > > assert(n.isNull == true); > > > > [...] > > more: > >

Re: Is Nullable supposed to provide Optional semantics?

2017-12-30 Thread Chris Paulson-Ellis via Digitalmars-d-learn
On Friday, 29 December 2017 at 22:08:59 UTC, vit wrote: n = Nullable!Object.init; assert(n.isNull == true); [...] more: https://forum.dlang.org/thread/jrdedmxnycbqzcpre...@forum.dlang.org?page=1 Thanks. No-one in the linked thread seemed to know why .destroy is used in nullify.