Re: SegFault with HibernateD

2018-01-12 Thread Ali Çehreli via Digitalmars-d-learn

On 01/12/2018 06:50 PM, Venkat wrote:
> Sorry about all these posts. Wish there were an edit button.

That's ok. :) These are actually newsgroups (see NNTP protocol). 
Newsgroups don't have any edit functionality. The "forum" is just a web 
interface to newsgroups.


Ali



Re: Using Postgres connection functions

2018-01-12 Thread Joe via Digitalmars-d-learn
Going beyond the connection, there are various other libpq 
functions that use a similar pattern of values passed using 
multiple parallel C arrays, e.g.,


   PGresult *PQexecParams(PGconn *conn,
   const char *command,
   int nParams,
   const Oid *paramTypes,
   const char * const *paramValues,
   const int *paramLengths,
   const int *paramFormats,
   int resultFormat);

Each of the `paramXxxxs' arguments are arrays (Oid is an alias 
for uint).


   PGresult *PQprepare(PGconn *conn,
const char *stmtName,
const char *query,
int nParams,
const Oid *paramTypes);

   PGresult *PQexecPrepared(PGconn *conn,
 const char *stmtName,
 int nParams,
 const char * const *paramValues,
 const int *paramLengths,
 const int *paramFormats,
 int resultFormat);

My point is that there seems to be a need to have a generic or 
generalized mechanism for passing these argument arrays from D to 
C.


Re: Using Postgres connection functions

2018-01-12 Thread Joe via Digitalmars-d-learn

On Saturday, 13 January 2018 at 04:26:06 UTC, Adam D. Ruppe wrote:

If and only if the values are known at compile time, you can do:

const char** keywords = ["hostaddr".ptr, "port".ptr, 
"dbname".ptr, null].ptr;


or even do it inline:


PQconnectdbParams(["hostaddr".ptr, "port".ptr, "dbname".ptr, 
null].ptr, ditto_for_values, 1);


The keywords are (or could be) known at compile time, but almost 
by definition, the associated values are only known at runtime.




Re: Using Postgres connection functions

2018-01-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 13 January 2018 at 04:17:02 UTC, Joe wrote:
It only compiled after I removed the second 'const' in the 
first and second arguments.


Yeah, D's const applies down the chain automatically, so you 
don't write it twice there.



string[] keywords = ["hostaddr", "port", "dbname"];
string[] values = ["127.0.0.1", "5432", "testdb"];



conn = PQconnectdbParams(cast(const char **)kws,
 cast(const char **)vals, 1);

So my question is: is there an easier or better way of passing 
two arrays of C null-terminated strings to an extern(C) 
function?


If and only if the values are known at compile time, you can do:

const char** keywords = ["hostaddr".ptr, "port".ptr, 
"dbname".ptr, null].ptr;


or even do it inline:


PQconnectdbParams(["hostaddr".ptr, "port".ptr, "dbname".ptr, 
null].ptr, ditto_for_values, 1);




Otherwise, what you did there is decent... being a C style of 
array of arrays, it will need to be coded in a C style with stuff 
like malloc and toStringz to convert D to C strings too.


Using Postgres connection functions

2018-01-12 Thread Joe via Digitalmars-d-learn
I'm trying to learn how to use D to connect (and send queries) to 
Postgres, i.e., libpq in C. Postgres has three families of 
connection functions: PQsetdbLogin which takes multiple 
individual arguments (all as const char *), PQconnectdb which 
takes a single connection string (which Postgres then has to 
parse into keywords/values) and PQconnectdbParams (introduced 
with PG 9.0 in 2010) which takes two arrays of char pointers, one 
for the keywords and another for the values, i.e., it's 
pre-parsed, and I believe, more convenient for callers since it's 
most likely that they get the parameter values from command line 
arguments, environment values or UI dialogs, so it saves the 
connection string formatting on the caller side and the 
parsing/decomposing on the library side.


In spite of the latter considerations, it appears most D 
libraries for Postgres only support connection string usage (only 
derelict-pq has a declaration for PQconnectdbParams--but I 
haven't tested it).


In any case, I tried to use PQconnectdbParams by first declaring 
it, as per the Postgres manual, with


extern(C)
{
struct PGconn;

PGconn *PQconnectdbParams(const char * const * keywords,
  const char * const * values,
  int expand_dbname);
}

This caused ldc2, on Linux, to complain as follows:

connection.d(30): Error: found 'const' when expecting ')'
connection.d(30): Error: semicolon expected following function 
declaration

connection.d(30): Error: declaration expected, not '*'

It only compiled after I removed the second 'const' in the first 
and second arguments.


The hard thing was figuring out how to pass the keyword/values 
arrays, defined as:


string[] keywords = ["hostaddr", "port", "dbname"];
string[] values = ["127.0.0.1", "5432", "testdb"];

to the D invocation of PQconnectdbParams.  I ended up with the 
following, which looks like covering a patient with lots of 
bandaids and looking the other way (and I had to modify the 
arrays above with a fixed length).


extern(C) char * [keywords.length + 1] kws;
extern(C) char * [keywords.length + 1] vals;
foreach (i, k; keywords) {
kws[i] = cast(char *)toStringz(k);
vals[i] = cast(char *)toStringz(values[i]);
}
kws[keywords.length] = null;  // Postgres wants the NULL 
termination

vals[keywords.length] = null;

conn = PQconnectdbParams(cast(const char **)kws,
 cast(const char **)vals, 1);

I assume that in a real program I'd have to malloc the C arrays, 
since I wouldn't know beforehand how many parameters there would 
be (or I could define them with a static size of about 30 since 
that's how many connection parameters are recognized currently).


So my question is: is there an easier or better way of passing 
two arrays of C null-terminated strings to an extern(C) function?




Re: continue in static foreach

2018-01-12 Thread Seb via Digitalmars-d-learn

On Saturday, 13 January 2018 at 01:07:24 UTC, Marc wrote:

On Friday, 12 January 2018 at 22:03:53 UTC, H. S. Teoh wrote:
On Fri, Jan 12, 2018 at 10:03:40PM +, Marc via 
Digitalmars-d-learn wrote:

How do I use?

> static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue;
>}

give error:

> must use labeled continue within static foreach

then I tried:

> outer:static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue outer;
>}

give error:

> Error: enclosing label outer for continue not found

How do I fix it?


Unfortunately, this is currently not supported. You'll have to 
use an else-clause to handle the case when the condition is 
false.



T


thanks


It was mentioned in DIP1010, but that bit hasn't been accepted 
yet:


https://github.com/dlang/DIPs/blob/master/DIPs/DIP1010.md#static-break-and-static-continue


Re: SegFault with HibernateD

2018-01-12 Thread Venkat via Digitalmars-d-learn
Sorry about all these posts. Wish there were an edit button. I 
meant PreparedStatement in mysqlddbc driver, not HibernateD.


Re: SegFault with HibernateD

2018-01-12 Thread Venkat via Digitalmars-d-learn
I think there is a bug with PreparedStatement class in 
HibernateD. ddbc fails when I use a PreparedStatement. The code 
below shows that. I will create an issue with HibernateD.



int main(string[] args) {

string url = 
"mysql://localhost:3306/webmarx?user=webmarx_dev,password=webm@rx";


// creating Connection
auto conn = createConnection(url);
scope(exit) conn.close();

// creating Statement
auto stmt = conn.createStatement();
scope(exit) stmt.close();

 PreparedStatement prepStatement = 
conn.prepareStatement("SELECT * FROM preferences_wm ORDER BY id");

 scope(exit) prepStatement.close();
 ResultSet rs = prepStatement.executeQuery();

writeln(rs.getFetchSize());
return 0;
}


Re: continue in static foreach

2018-01-12 Thread Marc via Digitalmars-d-learn

On Friday, 12 January 2018 at 22:03:53 UTC, H. S. Teoh wrote:
On Fri, Jan 12, 2018 at 10:03:40PM +, Marc via 
Digitalmars-d-learn wrote:

How do I use?

> static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue;
>}

give error:

> must use labeled continue within static foreach

then I tried:

> outer:static foreach(enum string member; members) {
>static if(isFunction!(__traits(getMember, C, member))) {
>continue outer;
>}

give error:

> Error: enclosing label outer for continue not found

How do I fix it?


Unfortunately, this is currently not supported. You'll have to 
use an else-clause to handle the case when the condition is 
false.



T


thanks


Re: SegFault with HibernateD

2018-01-12 Thread Venkat via Digitalmars-d-learn

On Friday, 12 January 2018 at 08:55:13 UTC, Rene Zwanenburg wrote:
Hard to guess what the issue is, I'd attach a debugger to see 
where it crashes.


It fails at the sql() method in Command struct in 
mysql-native-1.1.4/mysql-native/source/mysql/commands.d. This is 
what gdb says when I do a disp _sql.


1: _sql = 

I'm sorry I am new to systems programming, would that mean _sql 
is null ?






Re: SegFault with HibernateD

2018-01-12 Thread Venkat via Digitalmars-d-learn

On Friday, 12 January 2018 at 12:41:34 UTC, Mike Parker wrote:


I see now. I glossed right over that execution output. On 
Windows, I don't recall ever seeing a dub exception from dub 
from a segfault. Just checked by accessing a null pointer and 
there's nothing thrown from dub. Is that a Linux thing?


 Thankyou for the reply. Yes I am running the app on Linux. I 
will report the issue.





Re: continue in static foreach

2018-01-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 12, 2018 at 10:03:40PM +, Marc via Digitalmars-d-learn wrote:
> How do I use?
> 
> > static foreach(enum string member; members) {
> > static if(isFunction!(__traits(getMember, C, member))) {
> > continue;
> > }
> 
> give error:
> 
> > must use labeled continue within static foreach
> 
> then I tried:
> 
> > outer:static foreach(enum string member; members) {
> > static if(isFunction!(__traits(getMember, C, member))) {
> > continue outer;
> > }
> 
> give error:
> 
> > Error: enclosing label outer for continue not found
> 
> How do I fix it?

Unfortunately, this is currently not supported. You'll have to use an
else-clause to handle the case when the condition is false.


T

-- 
Why waste time reinventing the wheel, when you could be reinventing the engine? 
-- Damian Conway


continue in static foreach

2018-01-12 Thread Marc via Digitalmars-d-learn

How do I use?


static foreach(enum string member; members) {
static if(isFunction!(__traits(getMember, C, m ember))) 
{
continue;
}


give error:


must use labeled continue within static foreach


then I tried:


outer:static foreach(enum string member; members) {
static if(isFunction!(__traits(getMember, C, > 
member))) {
continue outer;
}


give error:


Error: enclosing label outer for continue not found


How do I fix it?


Re: How to move an associative array between modules?

2018-01-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 12, 2018 at 08:46:50PM +, WhatMeWorry via Digitalmars-d-learn 
wrote:
[...]
> I hate to keep being a bother, but my project with the below static
> this() now compiles fine, but aborts during runtime with a "a problem
> caused the program to stop working ..."

Is there a way to get at the actual error message? That would be
immensely more helpful in diagnosing the problem.

One potential pitfall of static this(), though unlikely given your
circumstances, is if there are multiple modules that use static this()
and there's a circular dependency somewhere.  If this is the problem,
splitting the static this() into a separate module may help.

If all else fails, you *could* just move the initialization code into
main() (or a function called by main()).  It's uglier, but should get
things going again until you have more time to investigate how to fix
the problem.


T

-- 
Beware of bugs in the above code; I have only proved it correct, not tried it. 
-- Donald Knuth


Re: How to move an associative array between modules?

2018-01-12 Thread WhatMeWorry via Digitalmars-d-learn

On Thursday, 11 January 2018 at 23:29:30 UTC, Adam D. Ruppe wrote:

On Thursday, 11 January 2018 at 23:20:44 UTC, WhatMeWorry wrote:
When I simply move the array out of main() but still in app.d, 
the compiler returns
Error: expression ["SCRATCH":Track("scratch.wav", 
cast(Sound)1, 0, null),...  is not a constant.


Can I use "static if" or "static this()", or "mixin" or some 
other technique?


Yeah, just declare the array outside, then initialize it inside 
a static this() constructor.


int[int] foo;
static this() {
  foo = [1:1];
}



I hate to keep being a bother, but my project with the below 
static this() now compiles fine, but aborts during runtime with a 
"a problem caused the program to stop working ..."


Does static if have some pitfalls I'm unaware of?

static this()
{
tracks["SCRATCH"] = Track("scratch.wav", Sound.SOUND_EFFECT, 
ONCE,null );


// or this form code segment

Track[string] tracks =
[
"SCRATCH" : Track("scratch.wav", Sound.SOUND_EFFECT, 
ONCE,null )

];

}

I even tried just foo = [1:1]; but that crashed during run time.

I have a writeln() statement just after main() so I know it is 
occurring before main().





Re: union/toString: crash/segfault: What's happening here?

2018-01-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 12, 2018 at 10:49:45AM -0800, H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Fri, Jan 12, 2018 at 11:09:47AM +, kdevel via Digitalmars-d-learn 
> wrote:
> [...]
[...]
> > https://issues.dlang.org/show_bug.cgi?id=18232
> 
> Yep, definitely a codegen bug.  Apparently, local variables in union
> member functions aren't initialized to .init as they should be.
[...]

Fix: https://github.com/dlang/dmd/pull/7687

Temporary workaround: explicitly initialize your local variables in
union member functions.


T

-- 
Life begins when you can spend your spare time programming instead of watching 
television. -- Cal Keegan


Re: Why is this valued zeroed?

2018-01-12 Thread Marc via Digitalmars-d-learn
On Friday, 12 January 2018 at 05:14:12 UTC, Jonathan M Davis 
wrote:
On Thursday, January 11, 2018 14:07:18 Ali Çehreli via 
Digitalmars-d-learn wrote:

[...]


And if all what you're doing is printing the value out, you 
might as well just print the Duration directly rather than 
calling total. Duration.toString returns the units in a 
human-readable format such as "2 secs, 300 msecs, and 24 μs" or 
"29 msecs".


https://dlang.org/phobos/core_time.html#.Duration.toString

And if you're doing something like adding up the time spent, 
then you'd be better off keeping it in a Duration than 
converting it to long holding seconds or milliseconds or 
whatever.


- Jonathan M Davis


I do use that value as following:

int t = fileSize / countOfByetsDownloaded * 
duration.total!"seconds";


I print that value there for testing only, if I was getting the 
values correctly.


Thank you all guys for the answers.



Re: union/toString: crash/segfault: What's happening here?

2018-01-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jan 12, 2018 at 11:09:47AM +, kdevel via Digitalmars-d-learn wrote:
[...]
> On Friday, 12 January 2018 at 02:16:39 UTC, Adam D. Ruppe wrote:
[...]
> > I'd file this as a compiler codegen bug.
> 
> https://issues.dlang.org/show_bug.cgi?id=18232

Yep, definitely a codegen bug.  Apparently, local variables in union
member functions aren't initialized to .init as they should be.  Digging
into the compiler code right now to see if I can find where the problem
is...


T

-- 
Only boring people get bored. -- JM


Re: Possible dmd 2.078 regression ?

2018-01-12 Thread Basile B. via Digitalmars-d-learn

On Friday, 12 January 2018 at 18:50:10 UTC, Basile B. wrote:

On Friday, 12 January 2018 at 17:58:30 UTC, Stefan Koch wrote:

On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote:
I have a simple program that only compiles if the dependency 
is not pre-compiled as a static library. It worked fine 
before.

I guess a mangle problem ?


Yes and quite old...apparently it's more a 2.074.x regression.
i'm digging right now.


digger: Commit 1e7b526b40852e9b85df3684430e371034cdf7ec (1/1) is 
untestable.

digger: There are only untestable commits left to bisect.
digger: The first bad commit could be any of:
digger: 1e7b526b40852e9b85df3684430e371034cdf7ec
digger: 6fecaa8232a427fb3ca29c5a5245e08fc43b71b1
digger: f0410bea1ad2b130884964d603b34e729b3e4f69
object.Exception@bisect.d(186): We cannot bisect more!


Re: Possible dmd 2.078 regression ?

2018-01-12 Thread Basile B. via Digitalmars-d-learn

On Friday, 12 January 2018 at 17:58:30 UTC, Stefan Koch wrote:

On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote:
I have a simple program that only compiles if the dependency 
is not pre-compiled as a static library. It worked fine before.

I guess a mangle problem ?


Yes and quite old...apparently it's more a 2.074.x regression.
i'm digging right now.


Re: Possible dmd 2.078 regression ?

2018-01-12 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 12 January 2018 at 14:13:22 UTC, Basile B. wrote:
I have a simple program that only compiles if the dependency is 
not pre-compiled as a static library. It worked fine before. 
Please test this


---
if [ ! -d "iz" ]; then
git clone https://www.github.com/BBasile/iz.git
fi

cd iz/scripts
sh compile.sh
cd ../

#fails to link
dmd samples/dictionnary_suffixarray.d lib/iz.a -Iimport > 
ddemangle



#on the other hand, this works...
dub samples/dictionnary_suffixarray.d <<< "q"

#or even this...
dmd samples/dictionnary_suffixarray.d import/iz/strings.d 
import/iz/memory.d -Iimport

---

and tell me what do you think: regression or not ?


I guess a mangle problem ?


Re: invalid or corrupt file: duplicate COMDAT / Previous Definition Different

2018-01-12 Thread Anonymouse via Digitalmars-d-learn

On Wednesday, 10 January 2018 at 20:53:24 UTC, Anonymouse wrote:
None of the (version specifiers in the) build configurations I 
have touch upon the part of the fairly innocent code mentioned 
in the error message, if I'm reading it right. 
(https://github.com/zorael/kameloso/blob/c00ca4489e39348bd4b1678c95c1b12636df307c/source/kameloso/plugins/common.d#L424)


struct Foo
{
this(Regex!char) { /* ... */ }
this(StaticRegex!char) { /* ... */ }
}

It seems like the problem there is that StaticRegex is only an 
alias to Regex, so I'm overloading it twice. It rhymes well with 
the linked bug.


/++
A $(D StaticRegex) is $(D Regex) object that contains D code 
specially

generated at compile-time to speed up matching.
No longer used, kept as alias to Regex for backwards 
compatibility.

+/
public alias StaticRegex = Regex;  // <--

Reducing it to just that doesn't reproduce the error message 
though. As long as Regex!char can house both ctRegex!"foo" and 
"foo".regex it works for me.


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread Seb via Digitalmars-d-learn

On Friday, 12 January 2018 at 13:04:30 UTC, aliak wrote:

On Friday, 12 January 2018 at 10:55:53 UTC, Seb wrote:

On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote:

Hi, so basically is there a way to:

void func(alias pred = null, Range)(Range range) {
  // 1) check if pred(ElementType!Range.init, 
ElementType!Range.init) is equality

  // 2) check if isUnary!pred
  // 3) check if isBinary!pred
}

[...]


It isn't possible yet, but will be very soon: 
https://github.com/dlang/dmd/pull/7484


Thats to compare whether lambdaA == lambdaB right? Not if 
lambda(a, b) == lambda(b, a) == true <- i guess this is not 
possible anyway without knowing the entire set of inputs)


Yes. The implementation is actually pretty straight-forward:
Serialize to AST and compare.


Possible dmd 2.078 regression ?

2018-01-12 Thread Basile B. via Digitalmars-d-learn
I have a simple program that only compiles if the dependency is 
not pre-compiled as a static library. It worked fine before. 
Please test this


---
if [ ! -d "iz" ]; then
git clone https://www.github.com/BBasile/iz.git
fi

cd iz/scripts
sh compile.sh
cd ../

#fails to link
dmd samples/dictionnary_suffixarray.d lib/iz.a -Iimport > 
ddemangle



#on the other hand, this works...
dub samples/dictionnary_suffixarray.d <<< "q"

#or even this...
dmd samples/dictionnary_suffixarray.d import/iz/strings.d 
import/iz/memory.d -Iimport

---

and tell me what do you think: regression or not ?


Re: Floating Point Literals: float (f) and real (L) suffix issue

2018-01-12 Thread Simen Kjærås via Digitalmars-d-learn

On Friday, 12 January 2018 at 12:57:37 UTC, kdevel wrote:

On Friday, 12 January 2018 at 12:45:59 UTC, kdevel wrote:

suffix.d
```
void main ()
{
   real r = 1.L;
   float f = 1.f;
}
```

$ dmd suffix.d
suffix.d(3): Error: no property 'L' for type 'int'
suffix.d(4): Error: no property 'f' for type 'int'

According to the grammar in dmd2/html/d/spec/lex.html both are 
valid FloatLiterals. Any comments?


Just found this on the same page

| If a floating literal has a . and a type suffix, at least one 
digit must be in-between:

|
| 1f; // OK
| 1.f; // forbidden
| 1.; // OK, double

Is there a rational for this restriction?


int foo(int n) { return n * 2; }

assert(2.foo == 4);

Now simply replace foo with f or L. I believe this ambiguity is 
the whole reason.


--
  Simen


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread aliak via Digitalmars-d-learn

On Friday, 12 January 2018 at 10:55:53 UTC, Seb wrote:

On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote:

Hi, so basically is there a way to:

void func(alias pred = null, Range)(Range range) {
  // 1) check if pred(ElementType!Range.init, 
ElementType!Range.init) is equality

  // 2) check if isUnary!pred
  // 3) check if isBinary!pred
}

[...]


It isn't possible yet, but will be very soon: 
https://github.com/dlang/dmd/pull/7484


Thats to compare whether lambdaA == lambdaB right? Not if 
lambda(a, b) == lambda(b, a) == true <- i guess this is not 
possible anyway without knowing the entire set of inputs)


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread aliak via Digitalmars-d-learn

On Friday, 12 January 2018 at 08:18:02 UTC, Simen Kjærås wrote:

On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote:

Hi, so basically is there a way to:

void func(alias pred = null, Range)(Range range) {
  // 1) check if pred(ElementType!Range.init, 
ElementType!Range.init) is equality

  // 2) check if isUnary!pred
  // 3) check if isBinary!pred
}


For 2 and 3, there's std.traits.arity. However, as you point 
out, it has some problems with templated functions. This 
template will handle those cases in conjunction with 
std.traits.arity:


import std.traits : arity, isCallable;

template arity(alias Fn, Args...)
if (is(typeof(Fn!Args)) && isCallable!(typeof(Fn!Args)))
{
enum arity = .arity!(Fn!Args);
}

unittest {
assert(arity!(() => 1) == 0);
assert(arity!(a => a, int) == 1);
assert(arity!((a,b) => a, int, float) == 2);

void test(T)(T,T,T) {}
assert(arity!(test, int) == 3);
}


Thank you! That leading period too ... noice!

Checking if a function is the equality function is much harder. 
Consider this function:


bool compare(int a, int b) {
if (a == 2 && b = 3) return true;
return a == b;
}

Clearly this is not an equality function, but for the vast 
majority of inputs, it behaves exactly the same.


There are other examples that make this hard. It's perfectly 
possible to overload opEquals and have it ignore some fields, 
or even access a database on the other side of the atlantic 
ocean. In these cases, evaluating the equality of two objects 
is not testable at compile time, and you simply cannot know.


In your examples you use string functions ("a == b", e.g.). 
These can of course be compared (you might want to do some 
processing, as "a==b" should give the same result as "b
==  a"). There's cases here where you can't be sure, of 
course.


All in all, I believe you're trying to solve the wrong problem, 
but I might be wrong. Care to give a bit more information so I 
get a better idea of why you want to do this?


Nah, your gut was spot on :D What I was trying to do didn't 
really make any sense. I'm learning D by playing with generic 
algorithms (trying to recreate a javascript functional library 
called lodash), and was trying to be "too smart". My thought 
process was I want a function that finds the intersection between 
two arrays, and I want to allow the user to pass in an equality 
predicate as well, in which case the intersection will return 
elements that return true for the equality predicate, and the 
algorithm would then not care about sortedness and do a naive 
O(n*m) approach (ie: for each element in a check if it exists in 
b and return it as one of the intersection elements).


If the predicate was not an equality predicate than it'd be 
assumed to be the predicate that was used to sort the array (in 
the case that they're sorted) and then a faster O(n) algorithm 
can be used.


The unary/binary stuff was to determine if the predicate was a 
unary predicate, in which case a transformation is applied to 
each elements and you have an algorithm that returns the 
transformed intersected elements.


Realized that the binary equality predicate is just a special 
case of the unary transformation predicate so I actually don't 
need to (and obviously as you point out can't reliable) test that 
a binary predicate is an equality one.


Cheers, and thanks for your input!




Re: Floating Point Literals: float (f) and real (L) suffix issue

2018-01-12 Thread kdevel via Digitalmars-d-learn

On Friday, 12 January 2018 at 12:45:59 UTC, kdevel wrote:

suffix.d
```
void main ()
{
   real r = 1.L;
   float f = 1.f;
}
```

$ dmd suffix.d
suffix.d(3): Error: no property 'L' for type 'int'
suffix.d(4): Error: no property 'f' for type 'int'

According to the grammar in dmd2/html/d/spec/lex.html both are 
valid FloatLiterals. Any comments?


Just found this on the same page

| If a floating literal has a . and a type suffix, at least one 
digit must be in-between:

|
| 1f; // OK
| 1.f; // forbidden
| 1.; // OK, double

Is there a rational for this restriction?


Floating Point Literals: float (f) and real (L) suffix issue

2018-01-12 Thread kdevel via Digitalmars-d-learn

suffix.d
```
void main ()
{
   real r = 1.L;
   float f = 1.f;
}
```

$ dmd suffix.d
suffix.d(3): Error: no property 'L' for type 'int'
suffix.d(4): Error: no property 'f' for type 'int'

According to the grammar in dmd2/html/d/spec/lex.html both are 
valid FloatLiterals. Any comments?


Re: SegFault with HibernateD

2018-01-12 Thread Mike Parker via Digitalmars-d-learn

On Friday, 12 January 2018 at 08:55:13 UTC, Rene Zwanenburg wrote:


It looks to me like the program is being run through dub, and 
dub is just reporting the program's exit code.




I see now. I glossed right over that execution output. On 
Windows, I don't recall ever seeing a dub exception from dub from 
a segfault. Just checked by accessing a null pointer and there's 
nothing thrown from dub. Is that a Linux thing?


Re: Is old style compile-time foreach redundant?

2018-01-12 Thread Seb via Digitalmars-d-learn

On Sunday, 7 January 2018 at 02:17:02 UTC, Stefan Koch wrote:

On Sunday, 7 January 2018 at 01:08:44 UTC, H. S. Teoh wrote:
On Sun, Jan 07, 2018 at 12:55:27AM +, Stefan Koch via 
Digitalmars-d-learn wrote:
On Saturday, 6 January 2018 at 23:25:58 UTC, Ali Çehreli 
wrote:
> Is 'static foreach' sufficient for all needs or is there 
> any value for regular foreach over compile-time sequences?

[...]

No it's not.
When you can use the old style do so. Since it puts less 
stress on the

compiler in the general case.


Really? Based on a recent post by Jonathan Davis, the new 
static foreach actually runs faster in certain use cases.



T


That might be true.
If you are hitting some constant factor, however the big-o for 
static foreach is worse then for tuple foreach.



FWIW there isn't any measurable performance penalty / difference.
We moved to use static foreach on Phobos recently: 
https://github.com/dlang/phobos/pull/5989


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread cuxu via Digitalmars-d-learn
мinterest Ask. our service 
https://reviews-up.com/android-app-reviews/ with the help of 
specialists will help you in promoting your application and 
solving this issue




Re: union/toString: crash/segfault: What's happening here?

2018-01-12 Thread kdevel via Digitalmars-d-learn

Thanks for the quick answer!

On Friday, 12 January 2018 at 02:16:39 UTC, Adam D. Ruppe wrote:

On Friday, 12 January 2018 at 00:54:03 UTC, kdevel wrote:

$ dmd crash.d
$ ./crash


Nicholas Wilson is right that you can use = "" to work around 
it, but with strings, null is supposed to behave the same way.


And this gives different (each wrong) behavior on -m32 vs -m64, 
which leads me to believe you actually found a compiler bug.


dmd -O crash even produces a binary which segfaults on my machine.

Calling u.toString directly also leads to random spam, which 
means it isn't even the library.


I'd file this as a compiler codegen bug.


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


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread Seb via Digitalmars-d-learn

On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote:

Hi, so basically is there a way to:

void func(alias pred = null, Range)(Range range) {
  // 1) check if pred(ElementType!Range.init, 
ElementType!Range.init) is equality

  // 2) check if isUnary!pred
  // 3) check if isBinary!pred
}

[...]


It isn't possible yet, but will be very soon: 
https://github.com/dlang/dmd/pull/7484





Re: calloc for std.experimental.allocator

2018-01-12 Thread Nordlöw via Digitalmars-d-learn

On Thursday, 11 January 2018 at 21:57:28 UTC, MrSmith wrote:

http://dpldocs.info/experimental-docs/std.experimental.allocator.makeArray.4.html


Thanks!


Re: SegFault with HibernateD

2018-01-12 Thread Rene Zwanenburg via Digitalmars-d-learn

On Friday, 12 January 2018 at 07:33:37 UTC, Mike Parker wrote:

On Friday, 12 January 2018 at 05:24:52 UTC, Venkat wrote:
I get a SegFault with the main method below which uses 
HibernateD . The second main method which uses ddbc just works 
fine. What is wrong with the first main method ? I have 
attached the error at the bottom although I don't think it 
says much.




This is the error.



object.Exception@source/dub/generators/build.d(530): Program


It says enough to know that the exception is being thrown from 
dub and not your program. You program is never executed because 
dub throws the exception before it gets that far. You should 
report this at the dub repository: 
https://github.com/dlang/dub/issues


It looks to me like the program is being run through dub, and dub 
is just reporting the program's exit code.


Hard to guess what the issue is, I'd attach a debugger to see 
where it crashes.


Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread Simen Kjærås via Digitalmars-d-learn

On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote:

Hi, so basically is there a way to:

void func(alias pred = null, Range)(Range range) {
  // 1) check if pred(ElementType!Range.init, 
ElementType!Range.init) is equality

  // 2) check if isUnary!pred
  // 3) check if isBinary!pred
}


For 2 and 3, there's std.traits.arity. However, as you point out, 
it has some problems with templated functions. This template will 
handle those cases in conjunction with std.traits.arity:


import std.traits : arity, isCallable;

template arity(alias Fn, Args...)
if (is(typeof(Fn!Args)) && isCallable!(typeof(Fn!Args)))
{
enum arity = .arity!(Fn!Args);
}

unittest {
assert(arity!(() => 1) == 0);
assert(arity!(a => a, int) == 1);
assert(arity!((a,b) => a, int, float) == 2);

void test(T)(T,T,T) {}
assert(arity!(test, int) == 3);
}

Checking if a function is the equality function is much harder. 
Consider this function:


bool compare(int a, int b) {
if (a == 2 && b = 3) return true;
return a == b;
}

Clearly this is not an equality function, but for the vast 
majority of inputs, it behaves exactly the same.


There are other examples that make this hard. It's perfectly 
possible to overload opEquals and have it ignore some fields, or 
even access a database on the other side of the atlantic ocean. 
In these cases, evaluating the equality of two objects is not 
testable at compile time, and you simply cannot know.


In your examples you use string functions ("a == b", e.g.). These 
can of course be compared (you might want to do some processing, 
as "a==b" should give the same result as "b==  a"). 
There's cases here where you can't be sure, of course.


All in all, I believe you're trying to solve the wrong problem, 
but I might be wrong. Care to give a bit more information so I 
get a better idea of why you want to do this?


--
  Simen