Re: How would you create this construct?

2018-03-29 Thread Mike Parker via Digitalmars-d-learn
On Friday, 30 March 2018 at 02:30:01 UTC, Chris Katko wrote: What I'm trying to do is through this experimental API, is both eliminate the user needing to call a clean-up function explicitly, and, make the "right way" to use the API basically... the only way... to use it. The way I have

Re: How would you create this construct?

2018-03-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, March 30, 2018 02:30:01 Chris Katko via Digitalmars-d-learn wrote: > void start_draw_calls(BITMAP target_bitmap); //locks onto a > resource > void end_draw_calls(); //frees previous resource lock > > void my_function() > { > //... > > start_draw_calls(target_bitmap)

How would you create this construct?

2018-03-29 Thread Chris Katko via Digitalmars-d-learn
void start_draw_calls(BITMAP target_bitmap); //locks onto a resource void end_draw_calls(); //frees previous resource lock void my_function() { //... start_draw_calls(target_bitmap) //whether this is a function, or class, lambda, or a "using"? { draw_call1();

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:26:59 UTC, kdevel wrote: What is the lifetime of the first loop's variable i? It lives as long as the delegate. What about this example: ``` bug2.d import std.stdio; void main () { int delegate () [] dg; foreach (i; 0..2) { int *j; if (i

Re: string to hex convert

2018-03-29 Thread Ali Çehreli via Digitalmars-d-learn
On 03/29/2018 02:23 PM, Cym13 wrote: On Thursday, 29 March 2018 at 20:29:39 UTC, aerto wrote: how i can convert Hello world! to hex 48656c6c6f20776f726c6421 ?? Maybe something like: void main() {     // Those look like lots of imports but most of those are very common anyway     import

Re: string to hex convert

2018-03-29 Thread Seb via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:29:39 UTC, aerto wrote: how i can convert Hello world! to hex 48656c6c6f20776f726c6421 ?? --- import std.format, std.stdio; void main() { writeln("Hello World!".format!("%(%02X%)")); } --- https://run.dlang.io/is/acz7kV

Re: Cleaner way of working with a shared resource?

2018-03-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, March 29, 2018 18:50:39 Chris M. via Digitalmars-d-learn wrote: > On Thursday, 29 March 2018 at 18:07:50 UTC, Jonathan M Davis > > wrote: > > On Thursday, March 29, 2018 17:41:15 Chris M. via > > > > Digitalmars-d-learn wrote: > >> [...] > > > > In general, the correct way to deal

Re: string to hex convert

2018-03-29 Thread Cym13 via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:29:39 UTC, aerto wrote: how i can convert Hello world! to hex 48656c6c6f20776f726c6421 ?? Maybe something like: void main() { // Those look like lots of imports but most of those are very common anyway import std.digest: toHexString; import

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread kdevel via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:05:35 UTC, ag0aep6g wrote: On Thursday, 29 March 2018 at 19:02:51 UTC, kdevel wrote: On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: [...] int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2)

string to hex convert

2018-03-29 Thread aerto via Digitalmars-d-learn
how i can convert Hello world! to hex 48656c6c6f20776f726c6421 ??

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 29 March 2018 at 19:02:51 UTC, kdevel wrote: On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: [...] int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2) writeln (guns[i] ()); // 1 and 1, why? Isn't this undefined

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread kdevel via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: import std.stdio; void main () { int delegate () [] funs; funs ~= () => 0; funs ~= () => 1; foreach (i; 0..2) writeln (funs[i] ()); // 0 and 1 as expected int delegate () [] guns;

Re: Cleaner way of working with a shared resource?

2018-03-29 Thread Chris M. via Digitalmars-d-learn
On Thursday, 29 March 2018 at 18:07:50 UTC, Jonathan M Davis wrote: On Thursday, March 29, 2018 17:41:15 Chris M. via Digitalmars-d-learn wrote: [...] In general, the correct way to deal with a shared object is to protect access to it with a mutex and then within that protected section, you

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread Ivan Kazmenko via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:38:14 UTC, ag0aep6g wrote: <...> With immutable, this is certainly a problem. https://issues.dlang.org/show_bug.cgi?id=2043 Wow, such history for the bug! Two possible workarounds: int delegate () [] iuns; foreach (i; 0..2) iuns ~= (j) { return () =>

Re: Cleaner way of working with a shared resource?

2018-03-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, March 29, 2018 17:41:15 Chris M. via Digitalmars-d-learn wrote: > I'm working with mysql-native for a project, and have been using > a single, shared Connection > (http://semitwist.com/mysql-native-docs/v2.2.0/mysql/connection/Connection > .html) among multiple threads. The issue here

Cleaner way of working with a shared resource?

2018-03-29 Thread Chris M. via Digitalmars-d-learn
I'm working with mysql-native for a project, and have been using a single, shared Connection (http://semitwist.com/mysql-native-docs/v2.2.0/mysql/connection/Connection.html) among multiple threads. The issue here is that since it's shared, I can't use certain functions such as exec() or close()

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread Dennis via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: So, why do delegates of guns[] and huns[] all return 1, and how to correctly reproduce the behavior of funs[] while populating it in a loop? A delegate is a function with a pointer to the stack frame where it was created. It

Re: .ptr and .value

2018-03-29 Thread Timoses via Digitalmars-d-learn
On Wednesday, 5 May 2010 at 22:42:19 UTC, Robert Clipsham wrote: .ptr is only available for arrays. Internally, (dynamic) arrays in D look like this: struct { size_t length; T* ptr; } Thanks for this!! (I know this topic is old). But it made me understand why I found that this

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On 03/29/2018 05:16 PM, Ivan Kazmenko wrote: int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2) writeln (guns[i] ());  // 1 and 1, why? Because there's only variable `i`. All delegates refer to that same one. With `i` being mutable, this could maybe

how to correctly populate an array of dynamic closures?

2018-03-29 Thread Ivan Kazmenko via Digitalmars-d-learn
Here's a simplified example of what I want to achieve. I first create funs, an array of two delegates. I want funs[0] to always return 0 and funs[1] to always return 1. By assigning the constants directly (see the code below), I achieve exactly that. Now, I want to use a loop to assign the

Re: help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread dangbinghoo via Digitalmars-d-learn
On Thursday, 29 March 2018 at 09:16:11 UTC, dangbinghoo wrote: On Thursday, 29 March 2018 at 09:13:27 UTC, dangbinghoo wrote: On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote: On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote: [...] try [...] thanks for

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-29 Thread aliak via Digitalmars-d-learn
On Tuesday, 27 March 2018 at 15:37:11 UTC, SimonN wrote: On Tuesday, 27 March 2018 at 15:28:40 UTC, jmh530 wrote: static if (isMutable!T) bag[0] = rhs; else bag = [rhs]; I like this idea. I'd even take it a step futher: When T is a pointer or class

Re: how to make private class member private

2018-03-29 Thread Nick Treleaven via Digitalmars-d-learn
On Sunday, 18 March 2018 at 18:45:23 UTC, Steven Schveighoffer wrote: unittest { auto foo = new Foo; assert(foo.internalbuffer.empty); // note, this is a private symbol. } I don't understand why you would want a private symbol in a *documented* unittest, the reader of the

Re: help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread dangbinghoo via Digitalmars-d-learn
On Thursday, 29 March 2018 at 09:13:27 UTC, dangbinghoo wrote: On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote: On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote: [...] try [...] thanks for your reply, but it will also fail, the compiler just gives me :

Re: help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread dangbinghoo via Digitalmars-d-learn
On Thursday, 29 March 2018 at 09:02:16 UTC, Nicholas Wilson wrote: On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote: * #define SPI_MSGSIZE(N) \ N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0)

Re: help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote: * #define SPI_MSGSIZE(N) \ N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) */ extern (D) size_t SPI_MSGSIZE(size_t N) { return ((N *

Re: help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread dangbinghoo via Digitalmars-d-learn
On Thursday, 29 March 2018 at 08:47:50 UTC, dangbinghoo wrote: hi, I'm doing a D binding myself to , and has some problems to wrap the C macros. [...] PS: if I just _IOW!(char[])... it compiles, but does it mean it's unsafe? thanks!

help on how to wrap a C macros when binding linux/spi/spidev.h

2018-03-29 Thread dangbinghoo via Digitalmars-d-learn
hi, I'm doing a D binding myself to , and has some problems to wrap the C macros. ``` import core.sys.posix.sys.ioctl; ... /* IOCTL commands */ enum SPI_IOC_MAGIC = 'k'; /** * Orginal C macros. * * #define SPI_MSGSIZE(N) \ N)*(sizeof (struct spi_ioc_transfer))) < (1 <<

Re: Building application with LDC and -flto=thin fails in link stage

2018-03-29 Thread Jacob Carlborg via Digitalmars-d-learn
On Wednesday, 28 March 2018 at 20:09:24 UTC, Johan Engelen wrote: On Wednesday, 28 March 2018 at 17:03:07 UTC, Seb wrote: dub supports dflags and lflags in the config file. lflags are the linker commands. Please read the thread. `lflags` is for passing flags to the _linker_ (i.e. those

Re: D RAII with postblit disabled

2018-03-29 Thread Norm via Digitalmars-d-learn
On Thursday, 29 March 2018 at 04:16:55 UTC, Adam D. Ruppe wrote: On Thursday, 29 March 2018 at 04:12:38 UTC, Norm wrote: Is there a way to do this in D, or does it require special "create" functions for every struct that has a RAII-like struct as a member? You'll have to do it all the way up