Re: Overloading funtion templates.

2017-06-29 Thread Balagopal Komarath via Digitalmars-d-learn

On Friday, 30 June 2017 at 04:51:23 UTC, vit wrote:

import std.traits : isCallable;

auto foo(alias F, T)(T x)
if(isCallable!F)//this line is optional
{
return F(x);
}



Thanks. That works.




Re: Overloading funtion templates.

2017-06-29 Thread vit via Digitalmars-d-learn
On Thursday, 29 June 2017 at 06:40:04 UTC, Balagopal Komarath 
wrote:

On Wednesday, 28 June 2017 at 12:19:31 UTC, vit wrote:

auto foo(alias F, T)(T x)
{
return x.foo(&F);
}


With this definition foo!((x) => x+1)(3); doesn't work. Is 
there a way to solve this?


You don´t need overload templates:



import std.traits : isCallable;

auto foo(alias F, T)(T x)
if(isCallable!F)//this line is optional
{
return F(x);
}


int g(int x) { return x; }

struct G{
int i;
this(int i){
this.i = i;
}
int opCall(int x){return x*i;}  //int operator()(int x)
}

void main(){
foo!g(3);
foo!((int x) => x*2)(3);
auto g2 = G(4);
foo!g2(3);
foo!(G(5))(3);
}


Bulk allocation and partial deallocation for tree data structures.

2017-06-29 Thread Filip Bystricky via Digitalmars-d-learn

Hello!

I'm implementing a persistent hash trie (like in clojure/scala). 
Every 'persisting' insertion involves allocating a fixed number 
(6) of nodes (each chunk is a fixed width ranging between 1 and 
~33 words).


Basically, this data structure always allocates a whole branch at 
a time, but then nodes are deallocated individually.


Is there a way to tell an allocator allocate n chunks at a time? 
Or, alternatively, is there a way to allocate all the memory 
needed at once, and then free just chunks of it at a time? It 
seems like this would provide at least some speed improvement. 
And it could also be useful for batch operations on other 
node-based data structures (such as adding ranges of nodes to a 
graph at a time).


Thanks!



mysql-native + vibe.d example

2017-06-29 Thread crimaniak via Digitalmars-d-learn

Hi!
Moving my project from mysql-lited to mysql-native I faced the 
problem with null pointer error inside of mysql-native:


Log:
SELECT id FROM versionupdate ORDER BY id
Task terminated with unhandled exception:
etc.linux.memoryerror.NullPointerError@src/etc/linux/memoryerror.d(325)

??:? void etc.linux.memoryerror.sigsegvUserspaceProcess(void*) 
[0x102ebad]

??:? void etc.linux.memoryerror.sigsegvDataHandler() [0x102eaee]
../../.dub/packages/mysql-native-1.1.0/mysql-native/source/mysql/result.d:466 
const(pure nothrow @property bool function()) mysql.result.ResultRange.isValid 
[0xdf1626]
../../.dub/packages/mysql-native-1.1.0/mysql-native/source/mysql/result.d:536 
void mysql.result.ResultRange.close() [0xdf1cb8]
../../.dub/packages/mysql-native-1.1.0/mysql-native/source/mysql/result.d:460 
void mysql.result.ResultRange.__dtor() [0xdf15cc]
../../.dub/packages/mysql-native-1.1.0/mysql-native/source/mysql/result.d:430 
ref return mysql.result.ResultRange 
mysql.result.ResultRange.opAssign(mysql.result.ResultRange) [0xdf1dba]
source/vcm/sqlWrapper.d:63 mysql.result.ResultRange 
vcm.sqlWrapper.SqlWrapper.query!().query(immutable(char)[]) 
[0xdd57e3]
source/updater/manager.d:92 void 
updater.manager.UpdateManager.update() [0xd74886]

source/app.d:81 void app.prepareDb().__lambda1() [0xcfe740]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/core.d:595 void 
vibe.core.core.makeTaskFuncInfo!(void delegate()).makeTaskFuncInfo(ref void 
delegate()).callDelegate(vibe.core.core.TaskFuncInfo*) [0xc5b703]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/core.d:1224 void 
vibe.core.core.CoreTask.run() [0xf6a916]
??:? void core.thread.Fiber.run() [0x107f17b]
??:? fiber_entryPoint [0x107eede]
??:? [0x]

Code fragment:
string s = Sql(sqlString, args).toString!MysqlDialect;
writeln(s);stdout.flush; // debugging...

if(conn.__conn() is null) // debugging...
throw new Exception("connection is null");

ResultRange result;
// synchronized (mutex)
result = conn.query(s);  // <-- sqlWrapper.d:63 is here

It seems I am doing something wrong so myself-native fails to 
detect it in isValid(). So I search for example how to use 
mysql-native in real multi-threaded vibe.d application with usage 
of MySQLPool. Please do not point me to basic example provided 
with package because it is single thread.

Thanks.



Re: dub seems to have forgotten my versions

2017-06-29 Thread jmh530 via Digitalmars-d-learn

On Wednesday, 28 June 2017 at 21:25:18 UTC, jmh530 wrote:

On Wednesday, 28 June 2017 at 20:18:20 UTC, jmh530 wrote:

On Wednesday, 28 June 2017 at 19:54:01 UTC, jmh530 wrote:

[snip]


Does not seem to be a problem for another project using dub 
with dmd and similar dependencies...


After spending some time looking through the dub issues, I 
found that you can use
the option --nodeps effectively as an offline mode. The code 
compiles with --nodeps, but still fails without it.


I discovered that this issue was that I imported one package in 
the main function and then another package had a dependency on a 
lower version of that package.


Re: Alias template parameter to a private function

2017-06-29 Thread Sebastien Alaiwan via Digitalmars-d-learn

On Thursday, 29 June 2017 at 20:21:13 UTC, Ali Çehreli wrote:

A workaround is to use a lambda:

  filter!(a => isValid(a))(array)


Thanks! Nice trick, this is definitely going into my company's 
codebase :-)


Such limitations are pretty annoying. There were a number of 
similar issues in recent dmd releases. Please file a bug if 
it's not already there:


Thanks, will do!


Re: Advice wanted on garbage collection of sockets for c++ programmer using D

2017-06-29 Thread Meta via Digitalmars-d-learn

On Wednesday, 28 June 2017 at 15:55:41 UTC, John Burton wrote:

On Tuesday, 27 June 2017 at 09:54:19 UTC, John Burton wrote:
I'm coming from a C++ background so I'm not too used to 
garbage collection and it's implications. I have a function 
that creates a std.socket.Socket using new and connects to a 
tcp server, and writes some stuff to it. I then explicitly 
close the socket, and the socket object goes out of scope.




Am I doing this right? Or is there a better way to do this in 
D?


Thanks.



For my use case here, I'm increasingly thinking that just 
calling the underlying 'C' socket and send calls is better. No 
need for anything complicated at all for my actual program :)


One final piece of advice as this thread seemed to have gone off 
the rails a bit. You can always put a `scope(exit) 
socket.close();` after you create the socket. This will ensure 
that the socket will be closed once the scope is exited no matter 
what... almost, anyway. If an Error is thrown no stack unwinding 
is done but at this point your program is in an unrecoverable 
state anyway and you have a lot more to worry about than a socket 
that hasn't been closed.


Re: Alias template parameter to a private function

2017-06-29 Thread Ali Çehreli via Digitalmars-d-learn

On 06/24/2017 02:04 AM, Sebastien Alaiwan wrote:

> private:
>
> void privateFunction1()
> {
>   auto array = [0, 1, 2, 3, 4, 5];
>   auto result = filter!isValid(array); // error: 'isValid' is private
> }

> bool isValid(int i)
> {
>   return i % 2 == 0;
> }

A workaround is to use a lambda:

  filter!(a => isValid(a))(array)

Such limitations are pretty annoying. There were a number of similar 
issues in recent dmd releases. Please file a bug if it's not already there:


  https://issues.dlang.org/

Ali



Re: Alias template parameter to a private function

2017-06-29 Thread Sebastien Alaiwan via Digitalmars-d-learn

up please!


Re: How to partially apply member functions?

2017-06-29 Thread Andrea Fontana via Digitalmars-d-learn

On Thursday, 29 June 2017 at 13:01:10 UTC, ct wrote:

I was only able to do it this way:

auto on_next_previous = &this.OnNextPreviousMatch;
entry_.addOnNextMatch(&partial!(on_next_previous, true));
entry_.addOnPreviousMatch(&partial!(on_next_previous, 
false));


I think you should provide a better example in order to get some 
help.

We neither know what argument .addOnNextMatch() expects.

Usually a full example on https://dpaste.dzfl.pl or other 
websites is more useful :)


Andrea


Re: How to partially apply member functions?

2017-06-29 Thread ct via Digitalmars-d-learn

I was only able to do it this way:

auto on_next_previous = &this.OnNextPreviousMatch;
entry_.addOnNextMatch(&partial!(on_next_previous, true));
entry_.addOnPreviousMatch(&partial!(on_next_previous, false));


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

2017-06-29 Thread Stefan Koch via Digitalmars-d-learn

On Thursday, 29 June 2017 at 12:02:48 UTC, Simon Bürger wrote:

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 platforms is trivial if you 
compile LDC yourself. You'll want to use this alias: 
https://github.com/ldc-developers/ldc/blob/master/ddmd/root/ctfloat.d#L19, https://github.com/ldc-developers/ldc/blob/master/ddmd/root/ctfloat.h#L19


Huh, I will definitely look into this. This might be the most 
elegant solution. Thanks for the suggestion.


Required a custom build of the compiler for the library to work 
is rather inelegant imo.

However It might allow you to make progress the quickest.
Note that this only changes the type all ct-float-math is done at 
to double.

which means that know float and real will be double.
(Which is probably better then having float and double transform 
into 80bit reals)





Re: How to partially apply member functions?

2017-06-29 Thread ct via Digitalmars-d-learn

On Thursday, 29 June 2017 at 12:31:58 UTC, ct wrote:

I have something similar to the following:

class Editor {
  void OnNextPreviousMatch(bool is_forward, SearchEntry entry) {
}
}


I pressed send by mistake. Again, I have something similar to the 
following:


class Editor {
  void OnNextPreviousMatch(bool is_forward, SearchEntry entry) {
// ...
  }
}

In another member function, I want to partially apply a delegate:

class Editor {
  void OnNextPreviousMatch(bool is_forward, SearchEntry entry) {
// ...
  }

  void InstallHandlers() {
entry_.addOnNextMatch(partial!(&this.OnNextPreviousMatch, 
true));
entry_.addOnPreviousMatch(partial!(&this.OnNextPreviousMatch, 
false));

  }
}

I tried several things, but I keep getting compiler errors:

Error: value of 'this' is not known at compile time
/usr/include/dmd/phobos/std/functional.d(664,23): Error: need 
'this' for 'OnNextPreviousMatch' of type 'void(bool is_forward, 
SearchEntry entry)'


How to partially apply member functions?

2017-06-29 Thread ct via Digitalmars-d-learn

I have something similar to the following:

class Editor {
  void OnNextPreviousMatch(bool is_forward, SearchEntry entry) {
}
}


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 platforms is trivial if you 
compile LDC yourself. You'll want to use this alias: 
https://github.com/ldc-developers/ldc/blob/master/ddmd/root/ctfloat.d#L19, https://github.com/ldc-developers/ldc/blob/master/ddmd/root/ctfloat.h#L19


Huh, I will definitely look into this. This might be the most 
elegant solution. Thanks for the suggestion.


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/~dhbailey/mpdist/ which is under a 
(modified) BSD license. I just posted the link for context, sorry 
for the confusion. Doing a port to D does not allow me to change 
the license even though I not a single line from the original 
would remain (I think?).


I might do a completely new D implementation (still based on the 
original authors research paper, not on the details of their 
code). But
1. I probably would only do a subset of functions I need for my 
work (i.e. double-double only, no quad-double, and only a limited 
set of trancendental functions).
2. Given that I have seen the original code, this might still be 
considered a "derivative work". I'm not sure, copyright-law is 
kinda confusing to me in these cases.


Indeed you'll have no way to get rid of the excess precision 
except for creating a function per sub-expression.


No, doesn't seem to work. Here is a minimal breaking example:

double sum(double x, double y) { return x + y; }
bool equals(double x, double y) { return x == y; }

enum pi = ddouble(3.141592653589793116e+00, 
1.224646799147353207e-16);


struct ddouble
{
double hi, lo;

invariant
{
if(!isNaN(hi) && !isNaN(lo))
assert(equals(sum(hi, lo),  hi));
}

this(double hi, double lo)
{
this.hi = hi;
this.lo = lo;
}
}

But there are workarounds that seem to work:
1. remove the constructor (I think this means the invariant is 
not checked anymore?)

2. disable the invariant in ctfe (using "if(__ctfe) return;")
3. Don't use any ctfe (by replacing enum with immutable globals, 
initialized in "static this").



I was using the newCTFE fork which fixes this.


Does this mean your new CTFE code (which is quite impressive work 
as far as I can tell), floating point no longer gets promoted to 
higher precision? That would be really good news for hackish 
floating-point code.


Honestly, this whole "compiler gets to decide which type to 
actually use" thing really bugs me. Kinda reminiscent of C/C++ 
integer types which could in principle be anything at all. I 
thought D had fixed this by specifying "int = 32-bit, long = 
64-bit, float = IEEE-single-precision, double = 
IEEE-double-precision". Apparently not.


If I write "double", I would like to get IEEE-conform 
double-precision operations. If I wanted something depending on 
target-platform and compiler-optimization-level I would have used 
"real". Also this 80-bit-extended type is just a bad idea in 
general and should never be used (IMHO). Even on x86 processors, 
it only exists for backward-compatibility. No current instruction 
set (like SEE/AVX) supports it. Sorry for the long rant. But I am 
puzzled that the spec (https://dlang.org/spec/float.html) 
actually encourages double<->real convertions while at the same 
time it (rightfully) disallows "unsafe math optimizations" such 
as "x-x=0".