Re: Array!T and find are slow

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 06:52:44 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:

On Thu, 15 May 2014 05:53:45 +
monarch_dodra via Digitalmars-d-learn
 wrote:


As a workaround, I'm sure we could specialize enforce without
lazy for built-in types?


No. I don't think that that would work. The problem is that 
you'd have to be

able to overload between stuff like "error message" and
format("error message: %s", foo), because you don't want the 
first one to be

lazy, whereas you do want the second one to be lazy.


Oh... right. It's the *second* parameter that's lazy.

Arguably, the compiler should be able too "see" if the argument 
passed is a value or an expression though, and optimize 
accordingly.


[Rosettacode] Growable slices

2014-05-15 Thread bearophile via Digitalmars-d-learn
This task asks for an basic implementation of the the 
Lempel-Ziv-Welch (LZW) compression/decompression algorithm. I am 
keeping two D versions, the first one is minimal, and the second 
is a little more optimized:

http://rosettacode.org/wiki/LZW_compression#More_Refined_Version

There are of course ways to implement a really efficient ZLW 
compressor in D, and such implementation could be added as third 
D entry if it manages to be not too much long, but for the first 
two versions keeping the code very short is more important.


Despite the compressor in the second D entry is still not 
efficient at all, I have tried to make it not terrible regarding 
its efficiency, so I have defined a new kind of slice that can be 
extended, unlike the D slices, to avoid the string appends 
visible in the first D entry:



struct Slice {
size_t start, end;
@property opSlice() const pure nothrow @safe @nogc {
return original[start .. end];
}
alias opSlice this;
}

Slice w;
Tcomp[] result;
foreach (immutable i; 0 .. original.length) {
auto wc = Slice(w.start, w.end + 1); // Extend slice.
if (wc in dict) {
w = wc;
} else {
result ~= dict[w];
assert(dict.length < Tcomp.max); // Overflow guard.
dict[wc] = cast(Tcomp)dict.length;
w = Slice(i, i + 1);
}
}


Is this showing a limit (problem) of D slices, or is this design 
better written in other ways?


Bye,
bearophile


Re: Array!T and find are slow

2014-05-15 Thread Ary Borenszweig via Digitalmars-d-learn

On 5/15/14, 1:31 AM, Jonathan M Davis via Digitalmars-d-learn wrote:

On Thu, 15 May 2014 01:29:23 +
Kapps via Digitalmars-d-learn  wrote:


On Wednesday, 14 May 2014 at 23:50:34 UTC, Meta wrote:

On the topic of lazy, why *is* it so slow, exactly? I thought
it was just shorthand for taking a function that evaluates the
expression, and wrapping said expression in that function at
the call site. That is, I thought that:

int doSomething(lazy int n)
{
 return n();
}

Was more or less equivalent to:

int doSomething(int function(int) n)
{
 return n();
}


It's more equivalent to:

int doSomething(int delegate(int) n)
{
  return n();
}

And (I could be very likely wrong here), I believe that it's
expensive because it's not scope and possibly requires a closure.
Again, very likely may be wrong.


Yeah. It generates a delegate. You even use the value internally as a
delegate. So, that's definitely part of the problem, though IIRC, there were
other issues with it. However, I don't remember at the moment. The big one
IIRC (which may be due to its nature as a delegate) is simply that it can't be
inlined, and in many cases, you very much what the code to be inlined (enforce
would be a prime example of that).

enforce(cond, "failure");

really should just translate to something close to

if(!cond) throw new Exception("failure");

but it doesn't do anything close to that.


Isn't there a way in D to just expand:

enforce(cond, "failure");

(or something with a similar syntax) to this, at compile-time:

if(!cond) throw new Exception("failure");

I thought D could do this, so enforce should do this instead of using 
lazy arguments.


Re: Any chance to avoid monitor field in my class?

2014-05-15 Thread Daniel Murphy via Digitalmars-d-learn

"Yuriy"  wrote in message news:rfirqtgbparjbqxwt...@forum.dlang.org...


On Wednesday, 14 May 2014 at 08:47:38 UTC, Daniel Murphy wrote:
> I'm not getting any errors with the development head.  What os/compiler 
> version?
Hm, now that's strange. Building with latest public version seems to work. 
However, development head is doing the following:


Never mind I can reproduce the bug with master, I probably ran 'dmd test.d' 
instead of './dmd test.d' after building dmd.


This version seems to compile - the new manger can't handle extern(C++) 
functions with D arrays as arguments or return types.


extern(C++) class A(T)
{
extern(D):
   string hi()
   {
   return "asdf";
   }
}

void main()
{
   A!string a;
}

Only the subset of extern(C++) required to interface with actual C++ code 
has been tested at all, so using it with D-only types is going to be fairly 
unpleasant. 



Re: Array!T and find are slow

2014-05-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On Wed, 14 May 2014 19:50:33 -0400, Meta  wrote:

On Wednesday, 14 May 2014 at 22:32:01 UTC, Jonathan M Davis via  
Digitalmars-d-learn wrote:
Yeah, much as Andrei would hate to hear it (enforce was his idea, and  
he quite
likes the idiom), the fact that lazy is so inefficient makes it so that  
it's
arguably bad practice to use it in high performance code. We really  
need to
find a way to make it so that lazy is optimized properly so that we  
_can_
safely use enforce, but for now, it's not a good idea unless the code  
that

you're working on can afford the performance hit.

Honestly, in general, I'd avoid most anything which uses lazy (e.g.  
that's why

I'd use explict try-catch blocks rather than use
std.exception.assumeWontThrow - like enforce, it's a nice idea, but  
it's too

expensive at this point).

- Jonathan M Davis


On the topic of lazy, why *is* it so slow, exactly?


Last time I remember, the issue is that functions with lazy parameters  
will never be inlined.


-Steve


Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On Wed, 14 May 2014 05:13:42 -0400, Jonathan M Davis via  
Digitalmars-d-learn  wrote:



On Wed, 14 May 2014 08:27:45 +
monarch_dodra via Digitalmars-d-learn
 wrote:


On Monday, 12 May 2014 at 18:44:22 UTC, Jonathan M Davis via
Digitalmars-d-learn wrote:
> Sure, you can cast char[] to ubyte[] and sort that if you know
> that the array
> only holds pure ASCII. In fact, you can use
> std.string.representation to do it
> - e.g.
>
> auto ascii = str.representation;
>
> and if str were mutable, then you could sort it. But that will
> only work if
> the string only contains ASCII characters. Regardless, he
> wanted to know why
> he couldn't sort char[], and I explained why - all strings are
> treated as
> ranges of dchar, making it so that if their element type is
> char or wchar, so
> they're not random access and thus can't be sorted.

Arguably, a smart enough implementation should know how to sort a
"char[]", while still preserving codepoint integrity.


I don't think that that can be done at the same algorithmic complexity  
though.
So, I don't know if that would be acceptable or not from the standpoint  
of

std.algorithm.sort. But even if it's a good idea, someone would have to
special case sort for char[], and no one has done that.


I think it can be done at O(nlgn) complexity, but you must allocate a  
block of scratch space to do the sorting. You can't do it in-place because  
swapping isn't available.


Might as well convert to dchar and back explicitly.

Merge sort may allow more promising speedups, but I still think you will  
need to allocate extra space.





As a matter of fact, the built in "sort" property does it.

void main()
{
 char[] s = "éöeèûà".dup;
 s.sort;
 writeln(s);
}
//prints:
eàèéöû


I'm surprised. I thought that one of Bearophile's favorite complaints  
was that
it didn't sort unicode properly (and hence one of the reasons that it  
should

be removed). Regardless, I do think that it should be removed.


I can't believe this worked. I want to say that it's a freak accident for  
that set of characters. Looking in druntime, I don't see where the special  
case is.


-Steve


Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn

On Wednesday, 14 May 2014 at 09:01:23 UTC, John Colvin wrote:

Why would anyone ever want to sort code-points?


Why not? To remove duplicate characters?

They might want to sort graphemes, but that's difficult to do 
in-place (needs O(n) memory, I think...). If out-of-place is 
good enough


someStr.byGrapheme.array.sort();


The current "status quo" in D is that a dchar basically
represents a character.


Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn

On Thursday, 15 May 2014 at 13:26:45 UTC, Steven Schveighoffer
wrote:
On Wed, 14 May 2014 05:13:42 -0400, Jonathan M Davis via 
Digitalmars-d-learn  wrote:



On Wed, 14 May 2014 08:27:45 +
monarch_dodra via Digitalmars-d-learn
 wrote:

As a matter of fact, the built in "sort" property does it.

void main()
{
char[] s = "éöeèûà".dup;
s.sort;
writeln(s);
}
//prints:
eàèéöû


I'm surprised. I thought that one of Bearophile's favorite 
complaints was that
it didn't sort unicode properly (and hence one of the reasons 
that it should

be removed). Regardless, I do think that it should be removed.


I can't believe this worked. I want to say that it's a freak 
accident for that set of characters. Looking in druntime, I 
don't see where the special case is.


-Steve


Must be a hell of a freak accident ;)

 auto s = "é東öe京ûタèワà".dup;
 writeln(s.sort);

=> eàèéöûタワ京東

It's in rt/adi.d

extern (C) char[] _adSortChar(char[] a)

It's basically: string=>dstring=>sort=>dstring=>string.



BTW, the built in reverse also works with char[], and so does 
std.algorithm.reverse (and it does it pretty cleverly too, might 
I say).


As far as I'm concerned, if we *can* do it in n.log(n), and 
somebody provides the implementation, then there is no reason to 
not offer dchar sorting for char[]/wchar.


Re: Any chance to avoid monitor field in my class?

2014-05-15 Thread Yuriy via Digitalmars-d-learn

On Thursday, 15 May 2014 at 11:51:38 UTC, Daniel Murphy wrote:
This version seems to compile - the new manger can't handle 
extern(C++) functions with D arrays as arguments or return 
types.

Ok, i can understand that, but what about this one:
http://dpaste.dzfl.pl/6a9961e32e6d
It doesn't use d arrays in function interfaces. Should it work?


Re: [Rosettacode] Growable slices

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn

On Thursday, 15 May 2014 at 09:00:13 UTC, bearophile wrote:
This task asks for an basic implementation of the the 
Lempel-Ziv-Welch (LZW) compression/decompression algorithm. I 
am keeping two D versions, the first one is minimal, and the 
second is a little more optimized:

http://rosettacode.org/wiki/LZW_compression#More_Refined_Version

There are of course ways to implement a really efficient ZLW 
compressor in D, and such implementation could be added as 
third D entry if it manages to be not too much long, but for 
the first two versions keeping the code very short is more 
important.


Despite the compressor in the second D entry is still not 
efficient at all, I have tried to make it not terrible 
regarding its efficiency, so I have defined a new kind of slice 
that can be extended, unlike the D slices, to avoid the string 
appends visible in the first D entry:



struct Slice {
size_t start, end;
@property opSlice() const pure nothrow @safe @nogc {
return original[start .. end];
}
alias opSlice this;
}

Slice w;
Tcomp[] result;
foreach (immutable i; 0 .. original.length) {
auto wc = Slice(w.start, w.end + 1); // Extend slice.
if (wc in dict) {
w = wc;
} else {
result ~= dict[w];
assert(dict.length < Tcomp.max); // Overflow guard.
dict[wc] = cast(Tcomp)dict.length;
w = Slice(i, i + 1);
}
}


Is this showing a limit (problem) of D slices, or is this 
design better written in other ways?


Bye,
bearophile


I don't think it shows a limit of "slices" in and out of itself, 
but rather the whole "range" concept, that conveniently 
encapsulates a start/end iteration scheme.


The problem though is that, unlike iterators, these can only 
shrink, and never grow. Furthermore, it is usally hard to "split" 
a range into two parts, given a center iterator (especially for 
bidirectional-only ranges: EG: linked list).


I think ranges/slices still provide more functionality and are 
worth it, but they do sacrifice *some* power: Growth and 
splitting.


To be honest, what you are doing is essentially working with 
iterator pairs, disguised as indexes inside a "Slice" struct. Not 
that there's anything wrong with that, but that's my impression 
when looking at the code.


Related:
http://forum.dlang.org/thread/bhssgxfcscfbionhq...@forum.dlang.org#post-umxlhsfqmfjwydemfdeb:40forum.dlang.org
http://forum.dlang.org/thread/gpsiwnslxtsyfolym...@forum.dlang.org#post-mailman.2149.1353648522.5162.digitalmars-d:40puremagic.com


Re: TKD AddProtocolCommand example

2014-05-15 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 14 May 2014 at 21:23:02 UTC, DaveG wrote:
tkd\window\window.d(426): Error: undefined identifier 
CommandCallback


Added the missing import and now all works fine. Fixed in 
v1.0.5-beta. Any more issues open them up in github and i'll deal 
with them there. :)


https://github.com/nomad-software/tkd


Re: TKD AddProtocolCommand example

2014-05-15 Thread DaveG via Digitalmars-d-learn

On Thursday, 15 May 2014 at 17:30:22 UTC, Gary Willoughby wrote:

On Wednesday, 14 May 2014 at 21:23:02 UTC, DaveG wrote:
tkd\window\window.d(426): Error: undefined identifier 
CommandCallback


Added the missing import and now all works fine. Fixed in 
v1.0.5-beta. Any more issues open them up in github and i'll 
deal with them there. :)


https://github.com/nomad-software/tkd


Thanks Gary!
I was expecting it to catch when someone hits the close X on the 
title bar, but I changed it to this


this.mainWindow.addProtocolCommand(WindowProtocol.deleteWindow, 
&this.saveMeCommand);


And that seems to do the trick.

I will post bugs on github in future, I thought I was doing 
something wrong.

I have never messed with TCL/TK until now.

BTW, Really nice job on this!  Thanks for the quick reply and fix



Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thu, 15 May 2014 11:37:07 -0400, monarch_dodra   
wrote:



On Thursday, 15 May 2014 at 13:26:45 UTC, Steven Schveighoffer
wrote:
On Wed, 14 May 2014 05:13:42 -0400, Jonathan M Davis via  
Digitalmars-d-learn  wrote:



On Wed, 14 May 2014 08:27:45 +
monarch_dodra via Digitalmars-d-learn
 wrote:

As a matter of fact, the built in "sort" property does it.

void main()
{
char[] s = "éöeèûà".dup;
s.sort;
writeln(s);
}
//prints:
eàèéöû


I'm surprised. I thought that one of Bearophile's favorite complaints  
was that
it didn't sort unicode properly (and hence one of the reasons that it  
should

be removed). Regardless, I do think that it should be removed.


I can't believe this worked. I want to say that it's a freak accident  
for that set of characters. Looking in druntime, I don't see where the  
special case is.


-Steve


Must be a hell of a freak accident ;)

  auto s = "é東öe京ûタèワà".dup;
  writeln(s.sort);

=> eàèéöûタワ京東

It's in rt/adi.d


Seriously, I fucking hate the file names in druntime.

I looked in qsort.d.


extern (C) char[] _adSortChar(char[] a)

It's basically: string=>dstring=>sort=>dstring=>string.


OK, that's what I would have expected. I don't think we should support  
this.


BTW, the built in reverse also works with char[], and so does  
std.algorithm.reverse (and it does it pretty cleverly too, might I say).


This is a different algorithm. Sort requires random-access swapping.  
Reverse does not.


As far as I'm concerned, if we *can* do it in n.log(n), and somebody  
provides the implementation, then there is no reason to not offer dchar  
sorting for char[]/wchar.


I think there is nothing wrong with requiring the steps to be explicit. We  
should not hide such bloat behind sort.


-Steve


receiveTimeout minimum reasonable timeout value?

2014-05-15 Thread Charles Hixson via Digitalmars-d-learn
Duration can be specified in nanoseconds, but does it make any sense 
to have a value of 1 nanosecond?  0?

My desire is to check whether a message is in the queue, and then 
either move it local to the thread, or continue, depending.

Secondarily, is there much overhead in calling receiveTimeout 
frequently?  It would minimize the storage in the Tid mailbox, but at the 
cost of requiring more local storage.  As I can only receive one 
message (if there is one) at a time anyway, it's not clear that I would 
save much by calling it only once/iteration.

The current design is that I have an array of classes to tell to process 
themselves and I have a local AA of messages, with id#s that say which 
class instance they are intended for.  I could either loop through the 
array, and then loop receiving messages, or I could receive messages 
in between processing each instance.  The messages received may be 
intended for any instance in the thread, so they get stacked in an AA of 
the form:
Msg[uint64_t][] msg;
Msg is immutable and contains both from and to identifiers, among 
other things not all of which have been decided.  (Well, it's a struct of 
which all fields are immutable, so I presume the struct counts as 
immutable, though I haven't yet tested it.  I may need to redefine it, but 
it's intended as immutable.)  And the only thing passed to receive is a 
Msg (or a couple of control values in an enum).

As an aside, is it better to have more threads than processors?  TDPL 
seems equivocal about this.


Re: *** GMX Spamverdacht *** RegEx for a simple Lexer

2014-05-15 Thread Tim Holzschuh via Digitalmars-d-learn

Am 13.05.2014 21:53, schrieb Tim Holzschuh via Digitalmars-d-learn:

[...]


Thank you for all your interesing links and tips, I'll check these out!

Tim



Re: TKD AddProtocolCommand example

2014-05-15 Thread Mengu via Digitalmars-d-learn

On Thursday, 15 May 2014 at 17:30:22 UTC, Gary Willoughby wrote:

On Wednesday, 14 May 2014 at 21:23:02 UTC, DaveG wrote:
tkd\window\window.d(426): Error: undefined identifier 
CommandCallback


Added the missing import and now all works fine. Fixed in 
v1.0.5-beta. Any more issues open them up in github and i'll 
deal with them there. :)


https://github.com/nomad-software/tkd


i just was about to send a PR :(


Re: receiveTimeout minimum reasonable timeout value?

2014-05-15 Thread JR via Digitalmars-d-learn
On Thursday, 15 May 2014 at 18:15:46 UTC, Charles Hixson via 
Digitalmars-d-learn wrote:
Duration can be specified in nanoseconds, but does it make any 
sense

to have a value of 1 nanosecond?  0?

My desire is to check whether a message is in the queue, and 
then

either move it local to the thread, or continue, depending.


I use 0.seconds for such oneshot receive attempts. Snippet:

while (!halt) {
if (++readCounter > messageCheckFrequency) {
receiveTimeout(0.seconds,
&_verbose.fun,
&_quiet.fun,
&_unknown.fun
);

readCounter = 0;
}

slice = conn.stream.readLine(buf);
// ...
}


Re: TKD AddProtocolCommand example

2014-05-15 Thread Gary Willoughby via Digitalmars-d-learn

On Thursday, 15 May 2014 at 19:02:58 UTC, Mengu wrote:

On Thursday, 15 May 2014 at 17:30:22 UTC, Gary Willoughby wrote:

On Wednesday, 14 May 2014 at 21:23:02 UTC, DaveG wrote:
tkd\window\window.d(426): Error: undefined identifier 
CommandCallback


Added the missing import and now all works fine. Fixed in 
v1.0.5-beta. Any more issues open them up in github and i'll 
deal with them there. :)


https://github.com/nomad-software/tkd


i just was about to send a PR :(


he he, too slow. ;)

If you see anything else feel free to make another.


Re: Why std.algorithm.sort can't be applied to char[]?

2014-05-15 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 15 May 2014 at 17:46:52 UTC, Steven Schveighoffer 
wrote:
As far as I'm concerned, if we *can* do it in n.log(n), and 
somebody provides the implementation, then there is no reason 
to not offer dchar sorting for char[]/wchar.


I think there is nothing wrong with requiring the steps to be 
explicit. We should not hide such bloat behind sort.


-Steve


Of course, but I meant if we could find an algoirthm O(1) (or 
O(log(N)) space overhead.


If the only algorithm we are capable of providing just 
temporarily dstrings, then no.


Seg fault when calling C code

2014-05-15 Thread Andrew Brown via Digitalmars-d-learn
I'm trying to calculate residuals after fitting linear 
regression, and I've got some code in C using the gsl which 
should do it. Everything works fine if I use static arrays 
(below, defining X[15], y[5] etc.). Trouble is, I won't know the 
number of individuals or covariates until runtime, so I'm stuck 
using dynamic arrays. But passing them to C causes a seg fault. 
I'm very sure I'm doing something (lots of things) stupid. If 
someone could point out what, I'd be very grateful. Thanks very 
much.


Andrew

Here's a toy D example:

import std.stdio;

extern(C) {
  void regress(int nInd, int nCov, ref double[] x, ref double[] 
y, ref double[] rOut);

}

void main(){
  int nInd = 5;
  int nCov = 3;
  double[] x = new double[nCov * nInd];
  x = [1, 4, 3, 1, 4, 3, 1, 4, 2, 1, 6, 7, 1, 3, 2];
  double[] y = new double[nInd];
  y = [5, 3, 4, 1, 5];
  double[] residuals = new double[nInd];
  regress(5, 3, x, y, residuals);

  writeln(residuals);
}

and the C code it calls:

#include 

void regress(int nInd, int nCov, double *x, double *y, double 
*rOut){

  int i, j;
  gsl_matrix *xMat, *cov;
  gsl_vector *yVec, *c, *r;
  double chisq;

  xMat = gsl_matrix_alloc(nInd, nCov);
  yVec = gsl_vector_alloc(nInd);
  r = gsl_vector_alloc(nInd);
  c = gsl_vector_alloc(nCov);
  cov = gsl_matrix_alloc(nCov, nCov);
  for(i = 0; i < nInd; i++)
{
  gsl_vector_set(yVec, i, *(y+i));
  for(j = 0; j < nCov; j++)
gsl_matrix_set(xMat, i, j, *(x + i * nCov + j));
}

  gsl_multifit_linear_workspace *work =  
gsl_multifit_linear_alloc(nInd, nCov);

  gsl_multifit_linear(xMat, yVec, c, cov, &chisq, work);
  gsl_multifit_linear_residuals(xMat, yVec, c, r);
  gsl_multifit_linear_free(work);


  for(i = 0; i < nInd; i++)
rOut[i] = gsl_vector_get(r, i);
}


Re: Seg fault when calling C code

2014-05-15 Thread Ali Çehreli via Digitalmars-d-learn

On 05/15/2014 01:55 PM, Andrew Brown wrote:
> extern(C) {
>void regress(int nInd, int nCov, ref double[] x, ref double[] y, ref
> double[] rOut);
> }

I don't think that should even be allowed. C functions should not know 
or be compatible with 'ref' D parameters. Define the arguments as simple 
'double *' or 'const double *'.


That makes sense because your C function is defined that way anyway.

> void main(){
>int nInd = 5;
>int nCov = 3;
>double[] x = new double[nCov * nInd];
// ...
>regress(5, 3, x, y, residuals);

You want to pass the address of the first array member: x.ptr (&(x[0]) 
would work as well). (Same for y.)


Otherwise, what ends up happening is that the address of the x and y 
slices are passed and C has no idea of what that is.


Ali



Re: Seg fault when calling C code

2014-05-15 Thread Andrew Brown via Digitalmars-d-learn

That worked a treat! Thank you very much!

On Thursday, 15 May 2014 at 21:11:54 UTC, Ali Çehreli wrote:

On 05/15/2014 01:55 PM, Andrew Brown wrote:
> extern(C) {
>void regress(int nInd, int nCov, ref double[] x, ref
double[] y, ref
> double[] rOut);
> }

I don't think that should even be allowed. C functions should 
not know or be compatible with 'ref' D parameters. Define the 
arguments as simple 'double *' or 'const double *'.


That makes sense because your C function is defined that way 
anyway.


> void main(){
>int nInd = 5;
>int nCov = 3;
>double[] x = new double[nCov * nInd];
// ...
>regress(5, 3, x, y, residuals);

You want to pass the address of the first array member: x.ptr 
(&(x[0]) would work as well). (Same for y.)


Otherwise, what ends up happening is that the address of the x 
and y slices are passed and C has no idea of what that is.


Ali




Re: Seg fault when calling C code

2014-05-15 Thread bearophile via Digitalmars-d-learn

Ali Çehreli:

I don't think that should even be allowed. C functions should 
not know or be compatible with 'ref' D parameters. Define the 
arguments as simple 'double *' or 'const double *'.


Time ago I suggested something like that, that is not meaningful 
to accept extern(C) function signatures with ref, [], fixed-sized 
arrays, lazy arguments, associative arrays, pure, and so on, 
because the C side doesn't know about those things or can't 
enforce them (like purity). Walter didn't agree with me, but I 
didn't understand his answer (or I don't remember it now).


Bye,
bearophile


D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread Tom Browder via Digitalmars-d-learn
I am a volunteer developer with the well-known 3D CAD FOSS project BRL-CAD:

  http://brlcad.org

I have wanted to use D for a long time but I hadn't taken the plunge.
Yesterday I advertised to the BRL-CAD community my new project to
attempt to create D bindings for BRL-CAD's C libraries, and I created
a branch for the project.

I have been looking for specific information on creating D bindings
from C headers for which there seems to be sufficient information
available, but I would appreciate recommendations as to the best
method.  I have successfully built my first pure D program but now
need to test the feasibility of my project.

What I have not seen yet is the exact way to build a D program which
uses D bindings and its matching C library.  I have just created a
Cookbook page on the D Wiki where I show my first attempt for a real
GNU Makefile as an example for the project.  The page link is here:

  http://wiki.dlang.org/Using_C_libraries_for_a_D_program

I would appreciate it if an experienced D user would correct that
recipe so it should compile the desired binary source correctly
(assuming no errors in the  input files).

Thanks for any help.

Best regards,

-Tom


Re: receiveTimeout minimum reasonable timeout value?

2014-05-15 Thread Charles Hixson via Digitalmars-d-learn
On Thursday, May 15, 2014 08:21:41 PM JR via Digitalmars-d-learn wrote:
> On Thursday, 15 May 2014 at 18:15:46 UTC, Charles Hixson via
> 
> Digitalmars-d-learn wrote:
> > Duration can be specified in nanoseconds, but does it make any
> > sense
> > to have a value of 1 nanosecond?  0?
> > 
> > My desire is to check whether a message is in the queue, and
> > then
> > either move it local to the thread, or continue, depending.
> 
> I use 0.seconds for such oneshot receive attempts. Snippet:
> 
>  while (!halt) {
>  if (++readCounter > messageCheckFrequency) {
>  receiveTimeout(0.seconds,
>  &_verbose.fun,
>  &_quiet.fun,
>  &_unknown.fun
>  );
> 
>  readCounter = 0;
>  }
> 
>  slice = conn.stream.readLine(buf);
>  // ...
>  }
Thanks.  That's what I was hoping to hear.


XamarinStudio dependency issues(Mono-D)

2014-05-15 Thread Jack via Digitalmars-d-learn
I recently downloaded Xamarin Studio from the Mono-Develop 
site(as this was the only available installer on that site) and 
was looking to try out Mono-D.


Then this showed up: http://puu.sh/8NV4V.png

Was wondering where I can get those dependencies, or how to solve 
this particular problem... Thank you


Re: XamarinStudio dependency issues(Mono-D)

2014-05-15 Thread Kapps via Digitalmars-d-learn

On Friday, 16 May 2014 at 00:23:00 UTC, Jack wrote:
I recently downloaded Xamarin Studio from the Mono-Develop 
site(as this was the only available installer on that site) and 
was looking to try out Mono-D.


Then this showed up: http://puu.sh/8NV4V.png

Was wondering where I can get those dependencies, or how to 
solve this particular problem... Thank you


The problem occurs because MonoDevelop 5 is coming out, and the 
4.2.2 version I believe isn't supported anymore. I think that if 
you go into Help -> Check for Updates and set yourself to the 
Alpha channel it might work on Windows.


Re: XamarinStudio dependency issues(Mono-D)

2014-05-15 Thread Kapps via Digitalmars-d-learn

On Friday, 16 May 2014 at 01:07:28 UTC, Kapps wrote:

On Friday, 16 May 2014 at 00:23:00 UTC, Jack wrote:
I recently downloaded Xamarin Studio from the Mono-Develop 
site(as this was the only available installer on that site) 
and was looking to try out Mono-D.


Then this showed up: http://puu.sh/8NV4V.png

Was wondering where I can get those dependencies, or how to 
solve this particular problem... Thank you


The problem occurs because MonoDevelop 5 is coming out, and the 
4.2.2 version I believe isn't supported anymore. I think that 
if you go into Help -> Check for Updates and set yourself to 
the Alpha channel it might work on Windows.


I should clarify, I think that the 4.2.2 version isn't supported
by Mono-D anymore (might be wrong). The installation instructions
say to make sure you're on the alpha channel and thus 4.3. But if
this doesn't work, you might get more useful help on
http://mono-d.alexanderbothe.com, Alex is quite active and
helpful with questions.


Re: D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread Craig Dillabaugh via Digitalmars-d-learn
On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
I am a volunteer developer with the well-known 3D CAD FOSS 
project BRL-CAD:


  http://brlcad.org

I have wanted to use D for a long time but I hadn't taken the 
plunge.
Yesterday I advertised to the BRL-CAD community my new project 
to
attempt to create D bindings for BRL-CAD's C libraries, and I 
created

a branch for the project.

I have been looking for specific information on creating D 
bindings
from C headers for which there seems to be sufficient 
information

available, but I would appreciate recommendations as to the best
method.  I have successfully built my first pure D program but 
now

need to test the feasibility of my project.

What I have not seen yet is the exact way to build a D program 
which
uses D bindings and its matching C library.  I have just 
created a
Cookbook page on the D Wiki where I show my first attempt for a 
real
GNU Makefile as an example for the project.  The page link is 
here:


  http://wiki.dlang.org/Using_C_libraries_for_a_D_program

I would appreciate it if an experienced D user would correct 
that

recipe so it should compile the desired binary source correctly
(assuming no errors in the  input files).

Thanks for any help.

Best regards,

-Tom



Hi Tom,
Sadly, I lack the expertise to give you much advice.  I did read 
through your Wiki posting though.  One thing that came to mind 
was you used GMake.  Perhaps you should consider using DUB.  For 
example here is the DUB config file for one of my library 
bindings (in my case I used a static library though):


{
"name": "shplib",
"description": "D bindings for Shapelib. Shapefile 
reader.",
"homepage": 
"https://github.com/craig-dillabaugh/shplib.d";,

"homepage": "http://shapelib.maptools.org/";,
"importPaths":["."],
"targetType": "sourceLibrary",
"authors": [
"Craig Dillabaugh"
],
"sourcePaths": ["./source"],
"libs-posix" : ["libshp"]
}

A little nicer that GMake, more the "D way", and cross platform 
... I think.


Not sure exactly how you change that for linking to a .so lib.

Cheers,

Craig


Re: D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread Craig Dillabaugh via Digitalmars-d-learn

On Friday, 16 May 2014 at 01:16:46 UTC, Craig Dillabaugh wrote:
On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via 
Digitalmars-d-learn wrote:
I am a volunteer developer with the well-known 3D CAD FOSS 
project BRL-CAD:


 http://brlcad.org

I have wanted to use D for a long time but I hadn't taken the 
plunge.
Yesterday I advertised to the BRL-CAD community my new project 
to
attempt to create D bindings for BRL-CAD's C libraries, and I 
created

a branch for the project.

I have been looking for specific information on creating D 
bindings
from C headers for which there seems to be sufficient 
information
available, but I would appreciate recommendations as to the 
best
method.  I have successfully built my first pure D program but 
now

need to test the feasibility of my project.

What I have not seen yet is the exact way to build a D program 
which
uses D bindings and its matching C library.  I have just 
created a
Cookbook page on the D Wiki where I show my first attempt for 
a real
GNU Makefile as an example for the project.  The page link is 
here:


 http://wiki.dlang.org/Using_C_libraries_for_a_D_program

I would appreciate it if an experienced D user would correct 
that

recipe so it should compile the desired binary source correctly
(assuming no errors in the  input files).

Thanks for any help.

Best regards,

-Tom



Hi Tom,
Sadly, I lack the expertise to give you much advice.  I did 
read through your Wiki posting though.  One thing that came to 
mind was you used GMake.  Perhaps you should consider using 
DUB.  For example here is the DUB config file for one of my 
library bindings (in my case I used a static library though):


{
"name": "shplib",
"description": "D bindings for Shapelib. Shapefile 
reader.",
"homepage": 
"https://github.com/craig-dillabaugh/shplib.d";,

"homepage": "http://shapelib.maptools.org/";,
"importPaths":["."],
"targetType": "sourceLibrary",
"authors": [
"Craig Dillabaugh"
],
"sourcePaths": ["./source"],
"libs-posix" : ["libshp"]
}

A little nicer that GMake, more the "D way", and cross platform 
... I think.


Not sure exactly how you change that for linking to a .so lib.

Cheers,

Craig


Some info on DUB can be found at:

http://code.dlang.org/


Re: D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread Tom Browder via Digitalmars-d-learn
On Thu, May 15, 2014 at 8:24 PM, Craig Dillabaugh via
Digitalmars-d-learn  wrote:
> On Friday, 16 May 2014 at 01:16:46 UTC, Craig Dillabaugh wrote:
>> On Thursday, 15 May 2014 at 22:25:47 UTC, Tom Browder via
>> Digitalmars-d-learn wrote:
...
>>> What I have not seen yet is the exact way to build a D program which
>>> uses D bindings and its matching C library.  I have just created a
>>> Cookbook page on the D Wiki where I show my first attempt for a real
>>> GNU Makefile as an example for the project.  The page link is here:
>>>
>>>  http://wiki.dlang.org/Using_C_libraries_for_a_D_program
>>>
>>> I would appreciate it if an experienced D user would correct that
>>> recipe so it should compile the desired binary source correctly
>>> (assuming no errors in the  input files).
...
>> Sadly, I lack the expertise to give you much advice.  I did read through
>> your Wiki posting though.  One thing that came to mind was you used GMake.
>> Perhaps you should consider using DUB.  For example here is the DUB config
>> file for one of my library bindings (in my case I used a static library
>> though):
...
Thanks, Craig, I'll look into dub.

Best regards,

-Tom


Re: XamarinStudio dependency issues(Mono-D)

2014-05-15 Thread Jack via Digitalmars-d-learn

On Friday, 16 May 2014 at 01:11:52 UTC, Kapps wrote:

On Friday, 16 May 2014 at 01:07:28 UTC, Kapps wrote:

On Friday, 16 May 2014 at 00:23:00 UTC, Jack wrote:
I recently downloaded Xamarin Studio from the Mono-Develop 
site(as this was the only available installer on that site) 
and was looking to try out Mono-D.


Then this showed up: http://puu.sh/8NV4V.png

Was wondering where I can get those dependencies, or how to 
solve this particular problem... Thank you


The problem occurs because MonoDevelop 5 is coming out, and 
the 4.2.2 version I believe isn't supported anymore. I think 
that if you go into Help -> Check for Updates and set yourself 
to the Alpha channel it might work on Windows.


I should clarify, I think that the 4.2.2 version isn't supported
by Mono-D anymore (might be wrong). The installation 
instructions
say to make sure you're on the alpha channel and thus 4.3. But 
if

this doesn't work, you might get more useful help on
http://mono-d.alexanderbothe.com, Alex is quite active and
helpful with questions.


Thanks for this guys. Switched to Alpha channel and grabbed the
5.0.
My XStudio was on 4.3 and it didn't even install. It asked for
v5.0 dependencies so this definitely needs XStudio 5.0


Re: D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread FrankLike via Digitalmars-d-learn



read through
your Wiki posting though.  One thing that came to mind was 
you used GMake.
Perhaps you should consider using DUB.  For example here is 
the DUB config
file for one of my library bindings (in my case I used a 
static library

though):

...
Thanks, Craig, I'll look into dub.

Best regards,

-Tom


And use  VisualD.

Frank




Objects(from classes) at Compile time? no gc

2014-05-15 Thread Taylor Hillegeist via Digitalmars-d-learn

The subject says it all really. i have this example:

import core.memory;

class fruit{
  int value=5;
  public int getvalue(){
return value;
  }
}

int main(string[] args) {
GC.disable;
static fruit myfruit;
return myfruit.getvalue();
}

Most of the smart people will see that i want the program to 
return 5 but I did something dumb and didn't put in the "new" 
statement?


So my question is in longer words "Can I create instances of 
objects at compile time?" and if not "why not, i could build 
something (roughly)equivalent out of structs and functions and 
have it at compile time?"


Re: Objects(from classes) at Compile time? no gc

2014-05-15 Thread Tolga Cakiroglu via Digitalmars-d-learn

On Friday, 16 May 2014 at 04:59:46 UTC, Taylor Hillegeist wrote:

The subject says it all really. i have this example:

import core.memory;

class fruit{
  int value=5;
  public int getvalue(){
return value;
  }
}

int main(string[] args) {
GC.disable;
static fruit myfruit;
return myfruit.getvalue();
}

Most of the smart people will see that i want the program to 
return 5 but I did something dumb and didn't put in the "new" 
statement?


So my question is in longer words "Can I create instances of 
objects at compile time?" and if not "why not, i could build 
something (roughly)equivalent out of structs and functions and 
have it at compile time?"


First of all, that "static" keyword is meaningless there. You can 
remove it.


Secondly, the "getvalue" method is not defined as "static". So, 
it requires a object. It is not bound to class.


Thirdly, when you define "fruit myfruit", the variable "myfruit" 
is still "null". It doesn't point anywhere in memory. Therefore, 
the variable "value" is no where in memory. And, thereby there is 
no spoon, I mean 5.


Re: D Newbie Trying to Use D with Major C Libraries

2014-05-15 Thread Jacob Carlborg via Digitalmars-d-learn

On 15/05/14 23:27, Tom Browder via Digitalmars-d-learn wrote:

I am a volunteer developer with the well-known 3D CAD FOSS project BRL-CAD:

   http://brlcad.org

I have wanted to use D for a long time but I hadn't taken the plunge.
Yesterday I advertised to the BRL-CAD community my new project to
attempt to create D bindings for BRL-CAD's C libraries, and I created
a branch for the project.

I have been looking for specific information on creating D bindings
from C headers for which there seems to be sufficient information
available, but I would appreciate recommendations as to the best
method.


You can use DStep [1] to automatically generate bindings. It requires 
some manual tweaking afterwards but it will give you a good start.


[1] https://github.com/jacob-carlborg/dstep

--
/Jacob Carlborg


Re: Objects(from classes) at Compile time? no gc

2014-05-15 Thread Ali Çehreli via Digitalmars-d-learn

On 05/15/2014 09:59 PM, Taylor Hillegeist wrote:

The subject says it all really. i have this example:

import core.memory;

class fruit{
   int value=5;
   public int getvalue(){
 return value;
   }
}

int main(string[] args) {
 GC.disable;
 static fruit myfruit;
 return myfruit.getvalue();
}

Most of the smart people will see that i want the program to return 5
but I did something dumb and didn't put in the "new" statement?

So my question is in longer words "Can I create instances of objects at
compile time?" and if not "why not, i could build something
(roughly)equivalent out of structs and functions and have it at compile
time?"


Here are two ways of achieving it. Although f0 is constructed by new, I 
don't think that new is executed at run time (because it would conflict 
with 'static const'). f1 is definitely not using the GC because it is 
placed on a storage that the module owns:


class Fruit {
int value;

this (int value)
{
this.value = value;
}

public int getvalue() const {
return value;
}
}

static const f0 = new Fruit(42);

ubyte[__traits(classInstanceSize, Fruit)] storage;
static const Fruit f1;

static this()
{
import std.conv;
f1 = emplace!Fruit(storage[], 43);
}

void main() {
assert(f0.getvalue() == 42);
assert(f1.getvalue() == 43);
}

Ali



Re: Objects(from classes) at Compile time? no gc

2014-05-15 Thread Jacob Carlborg via Digitalmars-d-learn

On 16/05/14 06:59, Taylor Hillegeist wrote:

The subject says it all really. i have this example:

import core.memory;

class fruit{
   int value=5;
   public int getvalue(){
 return value;
   }
}

int main(string[] args) {
 GC.disable;
 static fruit myfruit;
 return myfruit.getvalue();
}

Most of the smart people will see that i want the program to return 5
but I did something dumb and didn't put in the "new" statement?

So my question is in longer words "Can I create instances of objects at
compile time?" and if not "why not, i could build something
(roughly)equivalent out of structs and functions and have it at compile
time?"


If you create an immutable instance it's possible to create it at 
compile time:


int main(string[] args) {
 GC.disable;
 immutable fruit myfruit = new immutable(fruit);
 pragma(msg, myfruit.getvalue); // will print 5 at compile time
 return myfruit.getvalue();
}

Although, I don't know if it will allocate it during  runtime as well.

--
/Jacob Carlborg


Re: Objects(from classes) at Compile time? no gc

2014-05-15 Thread Rikki Cattermole via Digitalmars-d-learn

On 16/05/2014 4:59 p.m., Taylor Hillegeist wrote:

The subject says it all really. i have this example:

import core.memory;

class fruit{
   int value=5;
   public int getvalue(){
 return value;
   }
}

int main(string[] args) {
 GC.disable;
 static fruit myfruit;
 return myfruit.getvalue();
}

Most of the smart people will see that i want the program to return 5
but I did something dumb and didn't put in the "new" statement?

So my question is in longer words "Can I create instances of objects at
compile time?" and if not "why not, i could build something
(roughly)equivalent out of structs and functions and have it at compile
time?"


Rules about CTFE, I have:
1. It must be valid D code
2. Source code must be available during compilation
3. If its not passed to a function, its not available
4. You don't care about performance
5. Templates are your friend

I prefer to add pure to any function I intend to be CTFE'd.
So yes you can use new for classes at CTFE, just don't expect it to be 
collected and finalized.