Templates: generic "return null;"

2014-02-03 Thread Chris
Is there a way I can make the return type in getAttribute generic? null does not work with numbers. MyStruct(T) { T[T] attributes; // public auto getAttribute(T attr) { if (!(attr in attributes)) { return null; // Doesn't work for numbers! } return attributes

Re: Templates: generic "return null;"

2014-02-03 Thread Stanislav Blinov
On Monday, 3 February 2014 at 10:25:19 UTC, Chris wrote: T[T] attributes; // public auto getAttribute(T attr) { if (!(attr in attributes)) { return null; // Doesn't work for numbers! } return attributes[attr]; } } One way would be to use std.typecons.Nu

Re: Templates: generic "return null;"

2014-02-03 Thread Chris
On Monday, 3 February 2014 at 10:32:58 UTC, Stanislav Blinov wrote: On Monday, 3 February 2014 at 10:25:19 UTC, Chris wrote: T[T] attributes; // public auto getAttribute(T attr) { if (!(attr in attributes)) { return null; // Doesn't work for numbers! } return attri

Re: Templates: generic "return null;"

2014-02-03 Thread Stanislav Blinov
On Monday, 3 February 2014 at 10:55:23 UTC, Chris wrote: I'm reluctant to (over)use throw, because I think that throw should be the last resort when you cannot easily predict all the things that can go wrong. Simple requests should give simple answers. If the key doesn't exist it returns nothi

Re: Templates: generic "return null;"

2014-02-03 Thread Dicebot
You have forgot to mention what behavior you are actually trying to achieve ;) Common not-so-meaningful value is simply T.init , but there can be no such thing as generic sentinel. If you need cheap and simple way to figure out that attribute was missing, change API to return value by out para

Re: Templates: generic "return null;"

2014-02-03 Thread Chris
On Monday, 3 February 2014 at 12:25:16 UTC, Dicebot wrote: You have forgot to mention what behavior you are actually trying to achieve ;) Common not-so-meaningful value is simply T.init , but there can be no such thing as generic sentinel. If you need cheap and simple way to figure out that at

Re: Templates: generic "return null;"

2014-02-03 Thread bearophile
Dicebot: If you need cheap and simple way to figure out that attribute was missing, change API to return value by out parameter and turn normal return value into boolean success flag. It's probably better to start using Nullable. Bye, bearophile

Re: Templates: generic "return null;"

2014-02-03 Thread Chris
On Monday, 3 February 2014 at 14:10:35 UTC, bearophile wrote: Dicebot: If you need cheap and simple way to figure out that attribute was missing, change API to return value by out parameter and turn normal return value into boolean success flag. It's probably better to start using Nullable.

Re: Templates: generic "return null;"

2014-02-03 Thread Dicebot
On Monday, 3 February 2014 at 14:21:29 UTC, Dicebot wrote: This is intended. The very point of Nullable is to force you to handle `null` state before accessing actual payload. P.S. for this very reason in my own implementation of Optional I provide only delegate access method: value.get(

Re: Templates: generic "return null;"

2014-02-03 Thread Chris
On Monday, 3 February 2014 at 14:21:29 UTC, Dicebot wrote: On Monday, 3 February 2014 at 14:17:11 UTC, Chris wrote: Probably. I tried using Nullable, but it caused some problems when the attribute wasn't defined: core.exception.AssertError@/usr/include/dmd/phobos/std/typecons.d(1233): Called

Re: Templates: generic "return null;"

2014-02-03 Thread Dicebot
On Monday, 3 February 2014 at 14:10:35 UTC, bearophile wrote: Dicebot: If you need cheap and simple way to figure out that attribute was missing, change API to return value by out parameter and turn normal return value into boolean success flag. It's probably better to start using Nullable.

Re: Templates: generic "return null;"

2014-02-03 Thread Dicebot
On Monday, 3 February 2014 at 14:17:11 UTC, Chris wrote: Probably. I tried using Nullable, but it caused some problems when the attribute wasn't defined: core.exception.AssertError@/usr/include/dmd/phobos/std/typecons.d(1233): Called `get' on null Nullable!int. This is intended. The very poi

How to "scope"?

2014-02-03 Thread Martin
Seeing as the scope keyword is (being?) deprecated, how would you handle something like this: class Test { private: string str; this(string str) { this.str = str; } public: static Test createFromString(string str) { return new Test(); } } void main() { // at the end

Re: How to "scope"?

2014-02-03 Thread Martin
Oops, I of course meant: static Test createFromString(string str) { return new Test(str); }

Re: How to "scope"?

2014-02-03 Thread Namespace
On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: Oops, I of course meant: static Test createFromString(string str) { return new Test(str); } You _can_ use scoped but it may allocate way to much and it's ugly to use: http://dlang.org/phobos/std_typecons.html#.scoped AFAIK scope'd

Re: How to "scope"?

2014-02-03 Thread Martin
On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote: On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: Oops, I of course meant: static Test createFromString(string str) { return new Test(str); } You _can_ use scoped but it may allocate way to much and it's ugly to use: http

Re: How to "scope"?

2014-02-03 Thread Namespace
On Monday, 3 February 2014 at 17:50:33 UTC, Martin wrote: On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote: On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: Oops, I of course meant: static Test createFromString(string str) { return new Test(str); } You _can_ use scoped bu

InstanceOf Template during runtime in a variant

2014-02-03 Thread Andre
Hi, I want to check whether the value stored in variant v is a type of Decimal during runtime. Is there a nice way? Kind regards André import std.variant; struct Decimal(int scale, int precision){ int _precision = precision; int _scale = scale; this(string value){/*...

Re: How to "scope"?

2014-02-03 Thread Martin
On Monday, 3 February 2014 at 17:58:43 UTC, Namespace wrote: On Monday, 3 February 2014 at 17:50:33 UTC, Martin wrote: On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote: On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: Oops, I of course meant: static Test createFromString(s

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
No. Variant only stores TypeInfo for its current data and templated struct will have a totally different type for each set of template arguments. Their similarity exists only during compile-time.

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
(you can check for specific type via `v.type() == typeid(Decimal!(10,2))` though)

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Ali Çehreli
On 02/03/2014 10:15 AM, Andre wrote: > > I want to check whether the value stored in > variant v is a type of Decimal during runtime. > Is there a nice way? > > Kind regards > André > > import std.variant; > > struct Decimal(int scale, int precision){ > int _precision = precision; > int

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Andre
Am 03.02.2014 20:09, schrieb Ali Çehreli: On 02/03/2014 10:15 AM, Andre wrote: > > I want to check whether the value stored in > variant v is a type of Decimal during runtime. > Is there a nice way? > > Kind regards > André > > import std.variant; > > struct Decimal(int scale, int prec

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
On Monday, 3 February 2014 at 19:35:47 UTC, Andre wrote: Btw. having std.decimal in the library would be really nice;) Kind regards André There is a proposal in Phobos review queue (http://wiki.dlang.org/Review_Queue) but its author does not seem to be active anymore so it moves nowhere.

3d vector struct

2014-02-03 Thread Brenton
Hi, I'm just getting to know D and so am hoping that someone more experienced with the language could review this 3d vector struct and my comments below. I'm planning on building a little ray tracer in the next week or so :) struct Vector3d { double x = 0, y = 0, z = 0; void

Re: 3d vector struct

2014-02-03 Thread bearophile
Brenton: 1) I initialize the vector to a null vector, not nans Why? 2) The dot and cross are "inout" methods, i.e. available for mutable, const, and immutable objects. There is no reason to declare "inout" methods as being "const". But I suggest to add pure/nothrow. 3) The dot and cro

Re: 3d vector struct

2014-02-03 Thread Craig Dillabaugh
5) I notice that a lot of other people online prefer using fixed arrays not structs for Vectors in D, why? It does make some calculations more straightforward. For example I have code that calculates distance between points as follows: double euclideanDistance( double[] pt1, double[] pt2 ) in

Re: 3d vector struct

2014-02-03 Thread Martijn Pot
On Monday, 3 February 2014 at 20:10:59 UTC, Brenton wrote: double dot(in Vector3d other) inout { return x * other.x + y * other.y + z * other.z; } Vector3d cross(in Vector3d other) inout { const Vector3d result = { y

Re: 3d vector struct

2014-02-03 Thread Stanislav Blinov
On Monday, 3 February 2014 at 20:10:59 UTC, Brenton wrote: 4) Is it advisable for the cross method to return by value? In C++, I would declare this method as inline and in a header file. Can I trust D to inline away this inefficiency? Perhaps I should pass in the result as a "ref" or "out"

Performant method for reading huge text files

2014-02-03 Thread Rene Zwanenburg
I'm running into a problem I've come across before but never found a satisfactory solution for. There's a pretty large ascii file I need to process, currently about 3GB but size will increase in the future. D's ranges in combination with std.algorithm are simply perfect for what I'm doing, an

Re: Performant method for reading huge text files

2014-02-03 Thread bearophile
Rene Zwanenburg: The problem is speed. I'm using LockingTextReader in std.stdio, but it't not nearly fast enough. On my system it only reads about 3 MB/s with one core spending all it's time in IO calls. Are you reading the text by lines? In Bugzilla there is a byLineFast: https://d.puremag

Re: Performant method for reading huge text files

2014-02-03 Thread Rene Zwanenburg
On Monday, 3 February 2014 at 23:50:54 UTC, bearophile wrote: Rene Zwanenburg: The problem is speed. I'm using LockingTextReader in std.stdio, but it't not nearly fast enough. On my system it only reads about 3 MB/s with one core spending all it's time in IO calls. Are you reading the text

Re: Templates: generic "return null;"

2014-02-03 Thread TheFlyingFiddle
On Monday, 3 February 2014 at 10:25:19 UTC, Chris wrote: Is there a way I can make the return type in getAttribute generic? null does not work with numbers. MyStruct(T) { T[T] attributes; // public auto getAttribute(T attr) { if (!(attr in attributes)) { return null; //

Re: std.parallelism: How to wait all tasks finished?

2014-02-03 Thread Dan Killebrew
It seems to me that worker threads will continue as long as the queue isn't empty. So if a task adds another task to the pool, some worker will process the newly enqueued task. No. After taskPool.finish() no way to add new tasks to the queue. taskPool.put will not add new tasks. Then perhaps

Re: Python calling D

2014-02-03 Thread Artem Tarasov
On Sunday, 2 February 2014 at 15:31:30 UTC, Russel Winder wrote: result is: |> LD_LIBRARY_PATH=. python execute.py Segmentation fault You should call Runtime.initialize() prior to calling any other D functions.