Re: DIP-1000 and return

2017-03-08 Thread Ilya Yaroshenko via Digitalmars-d-learn

On Monday, 2 January 2017 at 15:28:57 UTC, Nordlöw wrote:


Should I file a bug report?


Yes please



Re: DMD + Dynamic Library.

2017-03-08 Thread Damien Gibson via Digitalmars-d-learn
Well i guess ill try turning it to export everywhere then in a 
bit and report back if it worked out.


Re: DMD + Dynamic Library.

2017-03-08 Thread evilrat via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 18:21:35 UTC, Damien Gibson wrote:

On Wednesday, 8 March 2017 at 06:28:47 UTC, Jerry wrote:
You have to use "export" for any symbol to be visible from a 
dll. On Windows by default nothing is exported.


Would "export" and "export extern(D):" not be the same? Im 
confuseled..


They are independent.

"export" simply tells compiler/linker to make symbol public(i.e. 
available for dynamic loading from lib), note that on *nix 
systems all symbols are public by default.


"extern" thing controls symbol name mangling and calling 
conventions(yes, it has no meaning for data)




Re: Best way to manage non-memory resources in current D, ex: database handles.

2017-03-08 Thread Chad Joan via Digitalmars-d-learn

Awesome, thank you!

On Thursday, 9 March 2017 at 00:47:48 UTC, Adam D. Ruppe wrote:
Now, if you forget to scope(exit), it is OK, the garbage 
collector WILL get around to it eventually, and it is legal to 
work with C handles and functions from a destructor. It is only 
illegal to call D's garbage collector's functions or to 
reference memory managed by D's GC inside the destructor. C 
pointers are fine.


It's good to have this confirmed.  I'm always a bit trepidatious 
around destructors.


Oooh, and it looks like there is more information in the language 
spec about @disable on struct constructors and postblits now 
(compared to the previous time I tried to learn about that 
feature).  So between that and your example, I think I have a 
feel for how to use that.  Thanks.


Have a wonderful day!


Re: Best way to manage non-memory resources in current D, ex: database handles.

2017-03-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 23:54:56 UTC, Chad Joan wrote:

What's the best way to implement such a range in current D?


I'd go with a struct with disabled copying and default 
construction, then make the destructor free it and the function 
that returns it populate it.


So basically Unique.

The destroy function doesn't mention what methods specifically 
will be executed on the target object, other than "destructor 
or finalizer".


It calls the `~this()` function, aka the destructor. It is just 
sometimes called a finalizer in other contexts too. Same thing, 
different name.



So I have similar questions about this as I did Unique: How 
does it "free" the resource T?  What method does it call to 
tell T to deallocate itself?


So it doesn't deallocate itself, but it can deallocate its 
members.


It calls ~this(); first, your destructor, and you can free its 
members with that. Then it calls `free()` on the outer object 
pointer itself.


So clean up the members in the destructor and you should be good. 
Basically the same deal as with Unique.



I'm leaning towards this methodology;


This is good, but it is easy to forget the scope(exit) too.

That said, this is how my database.d handles its connection 
classes. If your connection is a struct, using the destructor is 
better (your option #3), since it just does this automatically - 
a struct destructor (unless it is in a dynamic array or some 
other kind of pointer) is automatically called on scope exit.


Classes, though, do not get their dtors called then - they wait 
until they are GC'd - so scope(exit) does a good job with getting 
them cleaned up faster.



===  (3)  Put a deallocate() method; call it in ~this()



This is what my database.d does with the query results. The 
database connection class is polymorphic and thus doesn't work 
well as a struct, but the query result worked beautifully as a 
struct and the destructor handles it.


I also threw in `@disable this(this);` to ensure it isn't copied 
somewhere so I don't have to refcount it or anything annoying 
like that. On the other hand, I must consume the query in-place 
(or pass it by pointer to other functions being careful not to 
keep it after the outer function returns)... but that's what I 
want to do anyway.




So my code looks something like this:


class Database {
   this(string conn) {
this.handle = establish_connection(conn);
if(this.handle is null) throw new Exception();
   }

   ~this() {
close_connection(this.handle);
   }

   Result query(string sql, string[] args) {
 // hugely simplified
 auto q = prepare_query(sql);
 bind_args(q, args);

 // the Result is constructed here and only here
 return Result(execute_query(q));
   }
}

struct Result {
 // no business default constructing it ever
 @disable this();

 // forbid copying it. Instead, pass it by pointer
 // or just loop it in-place and copy the results
 // elsewhere for storage.
 @disable this(this);

 // private constructor since only the query
 // method above should be making these
 private this(c_query_handle handle) {
this.handle = handle;
 // do whatever other allocation needs
 // to be done via C functions
// 

popFront(); // prime the first result
 }

 ~this() {
 // destroy whatever C resources this holds
 destroy_query_handle(this.handle);
 }

 Row front() {
return makeDFriendly(this.current_row);
 }

 void popFront() {
this.current_row = fetch_next_row(this.handle);
 }

 bool empty() {
return has_more_rows(this.handle);
 }
}



Then use it like this:


void main() {
auto db = new Database("db=test");
scope(exit) .destroy(db);

foreach(row; db.query("select * from foo", null)) {
// work with row
}
}



Now, if you forget to scope(exit), it is OK, the garbage 
collector WILL get around to it eventually, and it is legal to 
work with C handles and functions from a destructor. It is only 
illegal to call D's garbage collector's functions or to reference 
memory managed by D's GC inside the destructor. C pointers are 
fine.


It is just nicer to close the connection at a more specified time.


Re: DMD + Dynamic Library.

2017-03-08 Thread Damien Gibson via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 06:28:47 UTC, Jerry wrote:
You have to use "export" for any symbol to be visible from a 
dll. On Windows by default nothing is exported.


Would "export" and "export extern(D):" not be the same? Im 
confuseled..


Re: Date formatting in D

2017-03-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 16:00:26 UTC, aberba wrote:

Your docs page is really effective, not pretty though.


If you have specific complaints/suggestions, feel free to make a 
thread in the General forum, or email me 
destructiona...@gmail.com and I'll see what I can do (just don't 
want to get this thread too far off topic).


I'm open to changing design, but I'll resist if it sacrifices 
readability. I'm not a very good designer but have been doing 
some usability testing here and don't want to go backward on that.


Re: Date formatting in D

2017-03-08 Thread aberba via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 15:46:42 UTC, Adam D. Ruppe wrote:

On Wednesday, 8 March 2017 at 15:29:11 UTC, aberba wrote:

[...]


The  PHP function is basically just (translated to D):

string date(string format, time_t timestamp) {
char[256] buffer;
auto ret = strftime(buffer.ptr, buffer.ptr, 
toStringz(format), gmtime(×tamp);

return buffer[0 .. ret].idup;
}


in other words, it is a thin wrapper around the C function.

Let's see, how do we get a time_t out of D's std.datetime?

http://dpldocs.info/locate?q=time_t

The SysTime "toUnixTime" looks good:

http://dpldocs.info/experimental-docs/std.datetime.SysTime.toUnixTime.html



So an overload might be


string date(string format, SysTime time) {
date(format, time.toUnixTime());
}


Your docs page is really effective, not pretty though.


Re: Date formatting in D

2017-03-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 15:29:11 UTC, aberba wrote:

Having to do these stuff with C is a punch in the face.

Or PHP was:

date(format, timestamp);


The  PHP function is basically just (translated to D):

string date(string format, time_t timestamp) {
char[256] buffer;
auto ret = strftime(buffer.ptr, buffer.ptr, 
toStringz(format), gmtime(×tamp);

return buffer[0 .. ret].idup;
}


in other words, it is a thin wrapper around the C function.

Let's see, how do we get a time_t out of D's std.datetime?

http://dpldocs.info/locate?q=time_t

The SysTime "toUnixTime" looks good:

http://dpldocs.info/experimental-docs/std.datetime.SysTime.toUnixTime.html



So an overload might be


string date(string format, SysTime time) {
date(format, time.toUnixTime());
}


Re: Date formatting in D

2017-03-08 Thread aberba via Digitalmars-d-learn

On Tuesday, 7 March 2017 at 20:52:39 UTC, ikod wrote:

On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
I've been trying to figure out an inbuilt functionality in 
phobos for formatting date. In my use case, I've been trying 
to format current Unix timestamp to something like "Thu, 08 
Mar 2017 12:00:00 GMT".


How do I go by this easily (Currently, long concatenation of 
strings is what I'm thinking)?


I saw this thread[1] from 2011 on similar issue. Has it been 
resolved? What is the state of toCustomString?


[1] 
http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-le...@puremagic.com


Straightforward solution:

https://dlang.org/library/core/stdc/time/strftime.html
http://man7.org/linux/man-pages/man3/strftime.3.html

Maybe there is something better.


Having to do these stuff with C is a punch in the face.

Or PHP was:

date(format, timestamp);

Almost every other language (ive used) has this equivalent.


Re: Passing macros from commandline or enumerating versions

2017-03-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 12:00:40 UTC, Martin Drašar wrote:
Yeah, that's definitely an option, but I expect to have 
troubles with DUB if I use this approach.


ugh dub really show offer some way to pass individual modules 
too. Maybe we can request a `--passthrough 
something_to_pass_to_dmd_unmodified` feature.


Also, I need these files to be json, not a D code, because the 
same configuration mechanism can be used during runtime and I 
need to parse these files easily.


You could use the string import feature which is basically the 
same deal on dmd, just do 
`-Jpath/to/specific/config/directory`... but again, not sure if 
dub's command line will let you do that. I know its config file 
will though, "stringImportPaths".


But it would have to be a directory regardless, can't do 
individual files from the command line for string imports.



So far, I incline to have a build script run separate builds 
and copying different configuration files to one predefined and 
hardcode its name into a string import, thus sidestepping the 
problem.


Yeah, that works too.


Re: Is it possible to use std.experimental.allocator without the runtime or with the runtime disabled?

2017-03-08 Thread Guillaume Piolat via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 12:28:13 UTC, Jacob Carlborg wrote:

On 2017-03-08 12:59, Guillaume Piolat wrote:
Is it possible to use std.experimental.allocator without the 
runtime or

with the runtime disabled?


I had a quick look through the imports, I could not find 
anything that I know uses the runtime. Although it does use 
exceptions and asserts in some places. Exceptions seem to only 
be used in the free_list module. It also uses compile time 
features from Phobos.


Thanks Jacob. This is something I should have tried earlier.


Re: Is it possible to use std.experimental.allocator without the runtime or with the runtime disabled?

2017-03-08 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-03-08 12:59, Guillaume Piolat wrote:

Is it possible to use std.experimental.allocator without the runtime or
with the runtime disabled?


I had a quick look through the imports, I could not find anything that I 
know uses the runtime. Although it does use exceptions and asserts in 
some places. Exceptions seem to only be used in the free_list module. It 
also uses compile time features from Phobos.


--
/Jacob Carlborg


Re: Best memory management D idioms

2017-03-08 Thread Moritz Maxeiner via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 06:42:40 UTC, ag0aep6g wrote:

[...]

Yes and yes. GCAllocator.allocate calls core.memory.GC.malloc 
with does pretty much the same thing as the builtin `new`.


Nitpicking: `new` is typed (i.e. allocation+construction), 
`malloc` and `allocate` are not (only allocation). If you want 
allocation *and* construction with the new Allocator interface, 
you'll want to use the make[1] (and dispose[2] for the reverse 
path) template function; and they are a superset of `new`: You 
cannot, e.g., construct a delegate with new, but you can with 
`make`.


[1] https://dlang.org/phobos/std_experimental_allocator.html#.make
[2] 
https://dlang.org/phobos/std_experimental_allocator.html#.dispose


Re: Passing macros from commandline or enumerating versions

2017-03-08 Thread Martin Drašar via Digitalmars-d-learn
Dne 7.3.2017 v 22:36 Adam D. Ruppe via Digitalmars-d-learn napsal(a):
> The way I like to do it is to pass a module on the command line that
> contains the custom config. So in the app:
> 
> ---
> import myapp.config;
> 
> // use the variables defined in there like normal
> ---
> 
> 
> Now, to define a config file, you do something like:
> 
> 
> myconfiguration.d # note that the file name can be anything!
> ---
> module myapp.config; // but each must use this same module config
> 
> enum some_key = "some_value";
> // and so on
> ---
> 
> Now, when you compile, you build it with a particular config by passing
> one of those files to the compile:
> 
> dmd myapp.d myconfiguration.d # or whatever single config you want
> 
> 
> 
> Then you can define as much as you want in the config module, and have
> as many of them as you want too.

Yeah, that's definitely an option, but I expect to have troubles with
DUB if I use this approach. Also, I need these files to be json, not a D
code, because the same configuration mechanism can be used during
runtime and I need to parse these files easily.

So far, I incline to have a build script run separate builds and copying
different configuration files to one predefined and hardcode its name
into a string import, thus sidestepping the problem.

Anyway, thanks for an idea.

Martin




Is it possible to use std.experimental.allocator without the runtime or with the runtime disabled?

2017-03-08 Thread Guillaume Piolat via Digitalmars-d-learn
Is it possible to use std.experimental.allocator without the 
runtime or with the runtime disabled?


It would be ideal for allocating audio buffers in the audio 
thread. malloc is tolerated but using a pre-allocated area with a 
fallback on malloc would be way better and faster too.


Re: Can i using D & LLVM & SDL2 for Android?

2017-03-08 Thread Joakim via Digitalmars-d-learn

On Tuesday, 7 March 2017 at 12:06:48 UTC, dummy wrote:

Just thought. I do want to know. :-)

As far as I know is,
  * LDC2 woring on NDK(yah!)
  * Native OpenGLES: 
http://wiki.dlang.org/Build_LDC_for_Android#Build_a_sample_OpenGL_Android_app_ported_to_D
  * Dlangui working on Android that based on SDL2: 
https://github.com/buggins/dlangui / 
https://dlang.org/blog/2016/10/07/project-highlight-dlangui/


regards,


Yes, though I have not tried SDL2 myself.


Re: DUB specify version identifier on command line?

2017-03-08 Thread XavierAP via Digitalmars-d-learn

On Wednesday, 8 March 2017 at 02:15:00 UTC, Nicholas Wilson wrote:
Setting version identifiers is done by the `-version=ident` 
command line flag (this is equivalent to `version = ident` at 
source level) .
This should therefore be settable by the "dflags" dub 
configuration setting.


The way I would do it would be to have a custom configuration 
that sets "dflags" : [ "other normal flags", "-version= 
MemoryDebug"]


and then build the MemoryDebug dub configuration.

Hope that makes sense.


Yes... Although I was looking for a command line parameter for 
dub, not dmd, but apparently it's impossible. So thanks for 
pointing to the DFLAGS possibility, it has worked. :) I still 
prefer this for building different versions rather than changing 
the dub.json file every time. Thanks!


Re: Cconditional expression in return statement. Bug?

2017-03-08 Thread Jack Applegame via Digitalmars-d-learn

On Tuesday, 7 March 2017 at 16:00:54 UTC, kinke wrote:
Definitely a very bad bug. It works too if you mark `fun()` as 
nothrow. Please file a DMD issue.

https://issues.dlang.org/show_bug.cgi?id=17246