Re: initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn
On Tuesday, 10 October 2017 at 13:54:16 UTC, Daniel Kozak wrote: struct Double { double v = 0; alias v this; } struct Foo(size_t n) { Double[n] bar; } Interesting approach. But this might introduce problems later. For example `Double` is implicitly convertible to `double`, but `D

Re: initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn
On Tuesday, 10 October 2017 at 13:48:16 UTC, Andrea Fontana wrote: On Tuesday, 10 October 2017 at 13:36:56 UTC, Simon Bürger wrote: Is there a good way to set them all to zero? The only way I can think of is using string-mixins to generate a string such as "[0,0,0,0]" with exactl

initializing a static array

2017-10-10 Thread Simon Bürger via Digitalmars-d-learn
I have a static array inside a struct which I would like to be initialized to all-zero like so struct Foo(size_t n) { double[n] bar = ... all zeroes ... } (note that the default-initializer of double is nan, and not zero) I tried double[n] bar = 0; // does not compile double[n]

Re: lambda function with "capture by value"

2017-08-06 Thread Simon Bürger via Digitalmars-d-learn
On Sunday, 6 August 2017 at 12:50:22 UTC, Adam D. Ruppe wrote: On Saturday, 5 August 2017 at 19:58:08 UTC, Temtaime wrote: (k){ dgs[k] = {writefln("%s", k); }; }(i); Yeah, that's how I'd do it - make a function taking arguments by value that return the delegate you actually wa

Re: lambda function with "capture by value"

2017-08-05 Thread Simon Bürger via Digitalmars-d-learn
On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: Maybe std.functional.partial can help you. Nope. int i = 1; alias dg = partial!(writeln, i); i = 2; dg(); still prints '2' as it should because 'partial' takes 'i' as a symbol, which is - for this purpose

Re: lambda function with "capture by value"

2017-08-05 Thread Simon Bürger via Digitalmars-d-learn
On Saturday, 5 August 2017 at 18:54:22 UTC, ikod wrote: On Saturday, 5 August 2017 at 18:45:34 UTC, Simon Bürger wrote: On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote: [...] No, sometimes I want i to be the value it has at the time the delegate was defined. My actual usecase

Re: lambda function with "capture by value"

2017-08-05 Thread Simon Bürger via Digitalmars-d-learn
On Saturday, 5 August 2017 at 18:22:38 UTC, Stefan Koch wrote: On Saturday, 5 August 2017 at 18:19:05 UTC, Stefan Koch wrote: On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote: If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But

lambda function with "capture by value"

2017-08-05 Thread Simon Bürger via Digitalmars-d-learn
If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example: int i = 1; auto dg = (){ writefln("%s", i); }; i = 2; dg(); // prints '2' Is there a way to make the delegate "capture by value

Re: Force usage of double (instead of higher precision)

2017-06-29 Thread Simon Bürger via Digitalmars-d-learn
On Thursday, 29 June 2017 at 00:07:35 UTC, kinke wrote: On Wednesday, 28 June 2017 at 22:16:48 UTC, Simon Bürger wrote: I am currently using LDC on 64-bit-Linux if that is relevant. It is, as LDC on Windows/MSVC would use 64-bit compile-time reals. ;) Changing it to double on other

Re: Force usage of double (instead of higher precision)

2017-06-29 Thread Simon Bürger via Digitalmars-d-learn
Thanks a lot for your comments. On Wednesday, 28 June 2017 at 23:56:42 UTC, Stefan Koch wrote: [...] Nice work can you re or dual license under the boost license ? I'd like to incorporate the qd type into newCTFE. The original work is not mine but traces back to http://crd-legacy.lbl.gov/~dh

Force usage of double (instead of higher precision)

2017-06-28 Thread Simon Bürger via Digitalmars-d-learn
According to the standard (http://dlang.org/spec/float.html), the compiler is allowed to compute any floating-point statement in a higher precision than specified. Is there a way to deactivate this behaviour? Context (reason why I need this): I am building a "double double" type, which essent

Re: nested inout return type

2016-06-14 Thread Simon Bürger via Digitalmars-d-learn
On Tuesday, 14 June 2016 at 14:47:11 UTC, Steven Schveighoffer wrote: * only do one mutable version of opSlice * add implicit cast (using "alias this") for const(Slice!T) -> Slice!(const(T)). Interesting, but unfortunately, the compiler isn't eager about this conversion. auto x = s[5 .. 7] isn

Re: nested inout return type

2016-06-14 Thread Simon Bürger via Digitalmars-d-learn
On Tuesday, 14 June 2016 at 01:50:17 UTC, Era Scarecrow wrote: On Monday, 13 June 2016 at 23:51:40 UTC, Era Scarecrow wrote: inout(Slice) opSlice(size_t a, size_t b) inout { return cast(inout(Slice)) Slice(ptr+a, b-a); } Seems the pointer has to be force-cast back to a

nested inout return type

2016-06-13 Thread Simon Bürger via Digitalmars-d-learn
I'm writing a custom (originally multi-dimensional) Slice-type, analogous to the builtin T[], and stumbled upon the problem that the following code won't compile. The workaround is simple: just write the function three times for mutable/const/immutable. But as "inout" was invented to make that

Re: custom memory management

2014-02-28 Thread Simon Bürger
On Friday, 28 February 2014 at 10:40:17 UTC, Dicebot wrote: On Thursday, 27 February 2014 at 21:46:17 UTC, Simon Bürger wrote: Sadly, this is incorrect as well. Because if such an object is collected by the gc, but the gc decides not to run the destructor, the buffer will never be free'd

Re: Switching from Java to D: Beginner questions, multiplatform issues, etc.

2014-02-27 Thread Simon Bürger
What exactly is the difference between C and D headers? D itself does not use headers at all. But you will need "D headers", if you want to call a C library from D. The translation is mostly syntatic and straight forward like: * replace #define-constants with enums * replace macros with (templa

Re: custom memory management

2014-02-27 Thread Simon Bürger
On Thursday, 27 February 2014 at 22:15:41 UTC, Steven Schveighoffer wrote: On Thu, 27 Feb 2014 16:46:15 -0500, Simon Bürger [...] More and more, I think a thread-local flag of "I'm in the GC collection cycle" would be hugely advantageous -- if it doesn't already exist..

Re: custom memory management

2014-02-27 Thread Simon Bürger
On Thursday, 27 February 2014 at 22:04:50 UTC, Namespace wrote: A struct is a value type. So it is passed by value and is placed on the stack. { S s; } S DTor is called at the end of the scope. So you can rely on RAII as long as you use structs. On the stack yes. But not on the heap:

Re: What is format of "dmd -deps ..." output?

2014-02-27 Thread Simon Bürger
On Wednesday, 26 February 2014 at 13:38:55 UTC, Cooler wrote: Is there any official/unofficial documentation about -deps command line option output? This does not answer your question directly, but if you want to create dependency files for use with make, you can use "rdmd --makedepend".

custom memory management

2014-02-27 Thread Simon Bürger
I am trying to implement a structure with value semantics which uses an internal buffer. The first approach looks like this: struct S { byte[] buf; this(int size) { buf = new byte[size]; } this(this) { buf = buf.dup; } ~this(this) { delete buf; } } T