Re: cannot access frame of function

2018-03-22 Thread Jordan Wilson via Digitalmars-d-learn

On Monday, 18 September 2017 at 21:58:39 UTC, Alex wrote:

On Monday, 18 September 2017 at 18:49:54 UTC, ag0aep6g wrote:


Doesn't work for me. This still fails compilation with the 
same error:



import std.algorithm.iteration : sum, cumulativeFold;
void main()
{
double[5] a;
auto asum = 1.23;
auto jProbs = a[].cumulativeFold!((a, b) => (a + b)/asum);
}



So, this is a bug, isn't it? I assume, I should file it then...


I have this same issue. Anyone have any thoughts on a workaround?

For example, my attempt at an exponential moving average doesn't 
compile:

auto ema(Range,T) (Range rng, int period, T seed) {
return rng.cumulativeFold!((a,b) => 
a+(2.0/(period+1))*(b-a))(seed.to!double);

}

So I ended up with this:
auto ema(Range,T) (Range rng, int period, T seed) {
struct EMA(Range) {
double currentValue;
double weighting;
Range rng;
this (Range r, int p, double s) {
currentValue = s;
weighting = 2.0 / (p+1);
rng = r;
}
auto front() { return currentValue; }
auto popFront() {
rng.popFront;
if (!rng.empty){
currentValue += (rng.front - 
currentValue)*weighting;

}
}
auto empty() { return rng.empty; }
}
return EMA!Range(rng,period,seed.to!double);
}

Thanks,

Jordan




remote execute program

2018-03-22 Thread Cecil Ward via Digitalmars-d-learn
I am wanting to write a short program (on a ‘server’ you could 
say) that takes a command, runs it (as on the command line, so an 
executable with arguments or a shell command) and returns a 
3-tuple with an int for the return code, plus the textual outputs 
that it generates to stdout and stderr. I can see a number of 
suitable routines in the D runtime libraries, which are already 
D-ified to save me a some trouble mindlessly converting code from 
C.


Where I could do with some help is as follows: I'm needing to 
send the commands to a remote box using http has to be used 
because the local-end program (on an iPad) that I have to 
interfacing to can only speak http/https, and can not handle just 
a straight eg TCP connection. Despite the overheads from using 
http, if I can employ gzip compression on the link then that will 
be a big gain.


Could anyone give me some general pointers for where to look?

The server box is a linux machine. I'm a very experienced 
professional C programmer but amazingly have never done anything 
with *nix in general or http-related C libraries.



I asked a question in this forum earlier about general low-level 
networking, but now this requirement has come up that mandates 
the use of very simple http and needs only synchronous 
operations. The impressive framework that is vibe.d has already 
been mentioned, but the amount of reading matter is rather 
daunting.


A simple D example of an http transaction would be very helpful.






Re: Building Derelict based application under windows

2018-03-22 Thread Mike Parker via Digitalmars-d-learn

On Thursday, 22 March 2018 at 20:51:36 UTC, ANtlord wrote:


Thanks in advance. Please feel free to ask any question


Your errors with the derelict libs are linker errors, with the 
early ones being this one:


"warning LNK4003: invalid library format; library ignored"

At the top of the output, DUB is warning you that you've put -m64 
in the package file and shouldn't do that. I would guess that's 
your problem. The libraries are being compiled as 32-bit OMF, 
which is default on the prepackaged DMD binaries on Windows, but 
the -m64 in your dflags is causing your app to compile as 64-bit. 
Try taking -m64 out of your package file and put -ax86_64 on your 
DUB command line instead.


Re: #import mapi.h

2018-03-22 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 22 March 2018 at 21:45:40 UTC, Martin Tschierschke 
wrote:

On Thursday, 22 March 2018 at 17:42:46 UTC, Paul Backus wrote:
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin 
Tschierschke wrote:
Is there an step by step introduction how to convert a C 
header of an external lib into the right extern(C){} block?


In addition to what others have said, I found the following 
article on the D Wiki useful:


https://wiki.dlang.org/D_binding_for_C


Thank you, this one is very good!


If you have Learning D, chapter 9 is all about interface D with C.


Re: #import mapi.h

2018-03-22 Thread Martin Tschierschke via Digitalmars-d-learn

On Thursday, 22 March 2018 at 17:42:46 UTC, Paul Backus wrote:
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin 
Tschierschke wrote:
Is there an step by step introduction how to convert a C 
header of an external lib into the right extern(C){} block?


In addition to what others have said, I found the following 
article on the D Wiki useful:


https://wiki.dlang.org/D_binding_for_C


Thank you, this one is very good!



Building Derelict based application under windows

2018-03-22 Thread ANtlord via Digitalmars-d-learn
Hello! First of all I don't know what my problem is but please 
don't close the thread and give me a chance.


I have been working on DMD under Linux since I met D. Short time 
ago I got a reason to make an application for Windows. I took 
DerelictAL[1], DerelictOgg[2] and DerelictVorbis[3] implementing 
basic prototype under Linux. So everything ok under Linux. 
Unfortunately I get a couple of issue under Windows 7. The 
Windows is launched under VirtualBox.


The first one is target architecture the application is compiled 
for. I installed DMD 2.079 and Visual Studio 2015.2 so I 
corrected sc.ini consider the article[4] from the wiki. The file 
has the content that is available by the link [5]. But I'm not 
sure that dmd compiles code for 64 bit architecture because an 
address of any pointer consists of 6 symbols instead of 16. A 
pointer in C++ application consists of 16 symbols. There is D 
code [6].


The second one is warnings and errors during compilation of my 
application. Typical messages shows up inability of linker to 
recognize any direlict library so there are a couple of errors 
about unresolved symbols of direlict libraries. Full list of 
errors and warnings is available by the link [7].


Please help me to figure out how I can fix the issue. I don't 
want use another language to create the application.


My environment
Windows 7 64bit (under VirtualBox)
DMD 2.079
Visual Studio 2015.2

[1] https://github.com/DerelictOrg/DerelictAL
[2] https://github.com/DerelictOrg/DerelictVorbis
[3] https://github.com/DerelictOrg/DerelictOgg
[4] https://wiki.dlang.org/Installing_DMD
[5] 
https://gist.github.com/ANtlord/0c78180f627eb544cc69770ecbe997a7
[6] 
https://gist.github.com/ANtlord/e0a7e183498095b2f8a8726f35e63803
[7] 
https://gist.github.com/ANtlord/e8a0cc6f20b2b46124ae3cd72ed04bed


Thanks in advance. Please feel free to ask any question


Re: Help debugging an core.exception.OutOfMemoryError

2018-03-22 Thread Ali Çehreli via Digitalmars-d-learn

On 03/22/2018 02:45 AM, Danny Arends wrote:

Hey all,

When running a D program that i wrote (32bit mode, windows10), an 
OutOfMemory exception being thrown when executing the program:


core.exception.OutOfMemoryError@src\core\exception.d(702): Memory 
allocation failed


However there is no stack trace being generated (due to the program 
being out of memory).


How do I figure out where in the user code the out of memory error is 
coming from/occurring ?


Thanks in advance,
Danny

ps. Using Linux 64bit, this error does not occur


I don't think you can point at any one place because it's probably 
really running out of memory. The fact that it's only in 32-bit suggests 
that you're allocating large amounts of memory (perhaps simply some 
arrays are getting larger) and that some values in unrelated parts of 
the code are mistaken to be pointers into that memory, making them 
"alive" in the eye of the GC.


This is a common issue with imprecise GCs like the current one that D 
uses. (It's highly likely that D's GC will always be imprecise because D 
allows casting a pointer to an integral type. For that reason, the GC 
cannot be sure whether your integer value 0x12345678 is a pointer or not.)


If that really is the reason, a common advice is moving to 64-bit, where 
the likelihood of false pointers is about 4 billion times less than 
happening in 32-bit. (If my math is correct. :) )


Ali


Re: Checking if a flag was passed to the compiler

2018-03-22 Thread Nordlöw via Digitalmars-d-learn

On Thursday, 22 March 2018 at 14:58:56 UTC, Seb wrote:

For reference and completeness, a simple example:

```d
void main()
{
enum isDIP1000 = __traits(compiles, () @safe {
 int x;
 int* p;
 p = 
});
pragma(msg, isDIP1000);
}
```

normal: https://run.dlang.io/is/RID7vh
-dip1000: https://run.dlang.io/is/1yJfVQ

(this could of course be fancier)


Thanks


Re: Is it possible to exit a contract?

2018-03-22 Thread berni via Digitalmars-d-learn

On Thursday, 22 March 2018 at 16:22:04 UTC, Ali Çehreli wrote:
Never thought of it before. :) I would put all that code into a 
function and call from the out block.


I rejected this, because I've got an unease feeling using extra 
functions in contract programming. Can't tell, where this feeling 
comes from; maybe just because I've too few experience with 
contract programming...




Re: #import mapi.h

2018-03-22 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin Tschierschke 
wrote:
Is there an step by step introduction how to convert a C header 
of an external lib into the right extern(C){} block?


In addition to what others have said, I found the following 
article on the D Wiki useful:


https://wiki.dlang.org/D_binding_for_C


Re: Is it possible to exit a contract?

2018-03-22 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 22 March 2018 at 13:16:00 UTC, berni wrote:

Part of my code looks like this:


out (result)
{
  if (result==true)
  {
lots of assertions...
  }
}


I would prefer an early exit like:


out (result)
{
  if (result==false) return;

  lots of assertions...
}


Is this possible somehow (return and break don't do the 
job...). The only thing I found is using goto, which I prefer 
over that gigantic if-block, but maybe there is something 
better, I don't know of?


It's a hack, but it works:

void bar()
in {(){
return;
assert(false);
}();}
body
{
}

unittest {
bar();
}

--
  Simen


Re: Is it possible to exit a contract?

2018-03-22 Thread Ali Çehreli via Digitalmars-d-learn

On 03/22/2018 06:16 AM, berni wrote:

Part of my code looks like this:


out (result)
{
  if (result==true)
  {
    lots of assertions...
  }
}


I would prefer an early exit like:


out (result)
{
  if (result==false) return;

  lots of assertions...
}


Is this possible somehow (return and break don't do the job...). The 
only thing I found is using goto, which I prefer over that gigantic 
if-block, but maybe there is something better, I don't know of?


Never thought of it before. :) I would put all that code into a function 
and call from the out block.


Ali



Re: OK, I'm stumped on this one: dstep, struct, mixin, bitfields

2018-03-22 Thread Jacob Carlborg via Digitalmars-d-learn

On Thursday, 22 March 2018 at 09:55:44 UTC, Russel Winder wrote:


I am guessing you mean against DStep rather than D :-)


Yes :)

Though clearly T and I would prefer version not to be a D 
keyword.


I suspect I have seen one place where DStep has turned version 
into version_ where version was a variable name. In this case 
"version" was a string used to create a bitfield using a 
mixin(bitfield!(…)) construct. Is that likely easy to fix?


Yes. It's not a string literal in the C code. The identifier 
should be renamed before it's added to the call to "bitfield".


--
/Jacob Carlborg


Re: why does this compile fine, but dies when you run it.

2018-03-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 22 March 2018 at 14:05:02 UTC, steven kladitis wrote:

-- what is the difference between int[] x; and int[int] x; 


int[] x; is like
struct _Int_Array
{
size_t len;
int* ptr;
}
_Int_Array y;
int i = 5;
x[i] = i; // this and the line below crash or assert, due to a 
null pointer

*(y.ptr +i) = 5;
are both the same;

int[int] is a completely different beast. is it a hashtable that 
maps ints to int.

its a lot closer to int[string] than it is to int[]

see https://dlang.org/spec/arrays.html vs 
https://dlang.org/spec/hash-map.html


Re: Checking if a flag was passed to the compiler

2018-03-22 Thread Seb via Digitalmars-d-learn

On Thursday, 22 March 2018 at 12:02:40 UTC, Nicholas Wilson wrote:

On Thursday, 22 March 2018 at 10:08:31 UTC, Nordlöw wrote:
Is there a way to check in code whether a specific flag, 
-dip1000 in my case, was passed to the compiler?


Most command line arguments that are detectable set a version, 
e.g. D_Coverage, unittest, assert,D_BetterC. I don't see 
dip1000 in LDC's list of version statements.


You could do a !__traits(compiles, { something forbidden by 
dip1000} );


For reference and completeness, a simple example:

```d
void main()
{
enum isDIP1000 = __traits(compiles, () @safe {
 int x;
 int* p;
 p = 
});
pragma(msg, isDIP1000);
}
```

normal: https://run.dlang.io/is/RID7vh
-dip1000: https://run.dlang.io/is/1yJfVQ

(this could of course be fancier)


Re: #import mapi.h

2018-03-22 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin Tschierschke 
wrote:
Is there an step by step introduction how to convert a C header 
of an external lib into the right extern(C){} block?


In short, they should be binary compatible: where is a pointer on 
C side, should be a pointer on D side, same for integer sizes, 
data layout an calling conventions.


Re: #import mapi.h

2018-03-22 Thread Martin Tschierschke via Digitalmars-d-learn
On Thursday, 22 March 2018 at 12:53:23 UTC, Martin Tschierschke 
wrote:

On Wednesday, 21 March 2018 at 17:12:07 UTC, Timoses wrote:

[...]

I got it, my first mini test with C bindings!
Important missing at first: (stderr)

import core.stdc.stdio : stderr, FILE;
and use of toStringz inside:  mapi_query(dbh, toStringz(q) )

After installing monetdb and starting the server with:
https://www.monetdb.org/Documentation/UserGuide/Tutorial

My minimal version of the example compiled with
dmd app.d -I /usr/local/lib/libmapi.so

(My dub.json is still incomplete so the linking failed.)

Here is the code: (only the minimum of mapi.h is icluded!)

```
 std.stdio;
import std.string;

/* interfacing winth monetdb #include mapi.h */
/* minimum to run code of example at: 
https://www.monetdb.org/Documentation/Manuals/SQLreference/Programming/MAPI */

import core.stdc.stdio : stderr,FILE;
import core.stdc.stdlib;

struct MapiStruct;
alias Mapi = MapiStruct*;

struct MapiStatement;
alias MapiHdl = MapiStatement*;

enum MOK = 0 ;

alias MapiMsg = int;

extern(System){
Mapi mapi_connect(const char* host, int port, const char* 
username, const char* password, const char* lang, const char* 
dbname);

MapiHdl mapi_query(Mapi mid, const char *cmd);
int mapi_fetch_row(MapiHdl hdl);
char *mapi_fetch_field(MapiHdl hdl, int fnr);
MapiMsg mapi_error(Mapi mid);
MapiMsg mapi_explain(Mapi mid, FILE* fd);
MapiMsg mapi_destroy(Mapi mid);
MapiMsg mapi_close_handle(MapiHdl hdl);
MapiMsg mapi_explain_result(MapiHdl hdl, FILE* fd);
MapiMsg mapi_next_result(MapiHdl hdl);
MapiMsg mapi_explain_query(MapiHdl hdl, FILE* fd);
char *mapi_result_error(MapiHdl hdl);
}


void die(Mapi dbh, MapiHdl hdl) {
  if (hdl != null) {
mapi_explain_query(hdl, stderr);
do {
  if (mapi_result_error(hdl) != null)
mapi_explain_result(hdl, stderr);
} while (mapi_next_result(hdl) == 1);
mapi_close_handle(hdl);
mapi_destroy(dbh);
  } else if (dbh != null) {
mapi_explain(dbh, stderr);
mapi_destroy(dbh);
  } else {
fprintf(stderr, "command failed\n");
  }
  exit(-1);
}


MapiHdl query(Mapi dbh, string q) {
  MapiHdl ret = null;
  if ((ret = mapi_query(dbh, toStringz(q) )) == null || 
mapi_error(dbh) != MOK)

die(dbh, ret);
  return(ret);
}

void update(Mapi dbh, string q) {
  MapiHdl ret = query(dbh, q);
  if (mapi_close_handle(ret) != MOK)
die(dbh, ret);
}

void main()
{

auto  dbh = mapi_connect("localhost", 5, "monetdb", 
"monetdb", "sql", "voc");

  writeln("DB-connect");
  update(dbh, "CREATE TABLE emp (name VARCHAR(20), age INT)");
  writeln("create");
  update(dbh, "INSERT INTO emp VALUES ('John', 23)");
  update(dbh, "INSERT INTO emp VALUES ('Mary', 22)");
auto hdl = query(dbh, "SELECT * FROM emp");
  while (mapi_fetch_row(hdl)) {
auto name = mapi_fetch_field(hdl, 0);
auto age = mapi_fetch_field(hdl, 1);
printf("%s is %s\n", name, age);
  }
}
```

Will try to make a complete binding available later...


Re: why does this compile fine, but dies when you run it.

2018-03-22 Thread steven kladitis via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 18:55:34 UTC, Adam D. Ruppe wrote:
On Wednesday, 21 March 2018 at 18:53:39 UTC, steven kladitis 
wrote:

int[] array3;
array3[0]=4;


array3 is empty. You are trying to set a value that doesn't 
exist..


import std.stdio;

void main(){
int[3] array1 = [ 10, 20, 30 ];
auto array2 = array1; // array2's elements are different
// from array1's
array2[0] = 11;
int[] array3;
//array4[0]=3;
array3 ~=4;
auto array4 = array3;
array4 ~=2;
int[string] a1;
int[int] a2;

a1["V"]=2;
a2[4]=3;

writeln(array1,'\n',array2,'\n',array3,'\n',array4,'\n',a1,'\n',a2);
}

-- this works, why??
-- what is the difference between int[] x; and int[int] x; 
-- thanks
-- Steven


Re: Static Arrays: Behaviour of Passing & Returning

2018-03-22 Thread Quantum Nerd via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 12:15:13 UTC, Simen Kjærås wrote:



I'm pretty sure you're seeing NRVO - Named Return Value 
Optimization. Also called copy elision:

https://en.wikipedia.org/wiki/Copy_elision

The compiler sees that when you return r, it's immediately 
assigned to b. Since it's a waste of cycles to allocate twice 
and copy the values, the function is told where the result will 
end up, and writes it there immediately.


The reason you don't see that with c is that the compiler isn't 
very smart, especially not with optimizations turned off, so it 
doesn't know for sure that it can safely overwrite the values 
already in c at that point.





Thanks a lot, that explains things.

I was really surprised, as I had compiler optimizations
in mind before I posted. I ran the tests without any optimization
using ldc2 -g -O0, but the outcome was the same.



Thanks for your help,


qnerd



Is it possible to exit a contract?

2018-03-22 Thread berni via Digitalmars-d-learn

Part of my code looks like this:


out (result)
{
  if (result==true)
  {
lots of assertions...
  }
}


I would prefer an early exit like:


out (result)
{
  if (result==false) return;

  lots of assertions...
}


Is this possible somehow (return and break don't do the job...). 
The only thing I found is using goto, which I prefer over that 
gigantic if-block, but maybe there is something better, I don't 
know of?


Re: #import mapi.h

2018-03-22 Thread Martin Tschierschke via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 17:12:07 UTC, Timoses wrote:
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin 
Tschierschke wrote:
Is there an step by step introduction how to convert a C 
header of an external lib into the right extern(C){} block?


A blog post or tutorial, or chapter in a D book? (I have those 
from Packt Publishing)


While googling I found this:
https://dlang.org/blog/2017/12/05/interfacing-d-with-c-getting-started/

Haven't read it yet, but might give you some more insight.

For automatic conversion I stumbled across Dstep which I so far 
like a lot:

https://github.com/jacob-carlborg/dstep


Thank you! I will have a look at both, an Mike Parker is pointing 
to his book, too,
so now together with the post  from "nkm1" I should be able to 
make it!


@ketmar, I learned C but the counter of years from that time was 
stored in a nibble and so I got an overflow...error :-)


Re: #import mapi.h

2018-03-22 Thread Martin Tschierschke via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 18:42:43 UTC, nkm1 wrote:
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin 
Tschierschke wrote:

[...]


The easiest thing to do is to write a wrapper in C. The wrapper 
will include all necessary headers and provide some easy to use 
functions that you can call from D. Of course, you'll need to 
use a C compiler to compile it.
Anyway, this header looks very straightforwar, no particular 
tricks with the preprocessor. It should be something like this 
(untested, obviously):


[...]

Thank you!
Especially this was a missing link:

struct MapiStruct;
alias Mapi = MapiStruct*;


Re: Checking if a flag was passed to the compiler

2018-03-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Thursday, 22 March 2018 at 10:08:31 UTC, Nordlöw wrote:
Is there a way to check in code whether a specific flag, 
-dip1000 in my case, was passed to the compiler?


Most command line arguments that are detectable set a version, 
e.g. D_Coverage, unittest, assert,D_BetterC. I don't see dip1000 
in LDC's list of version statements.


You could do a !__traits(compiles, { something forbidden by 
dip1000} );




Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Dukc via Digitalmars-d-learn

On Thursday, 22 March 2018 at 09:59:28 UTC, Kagamin wrote:

On Thursday, 22 March 2018 at 09:04:12 UTC, Dukc wrote:
So the real difference is that random-accesss ranges are not 
input-ranges in C# (or Rust, judging by a quick look I 
recently took) but can be used to fetch one. A bit cumbersome 
perhaps, but logical. I think I understand it now. Thank you.


IList inherits from IEnumerable, which is an input range.


Oops. Well, all the better!


Re: Issue with traits usability

2018-03-22 Thread Simen Kjærås via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 15:36:01 UTC, Márcio Martins wrote:

Hi!

How do I get past this?


static struct X {
int x;
private enum T = 1;
private alias M = string;
}

foreach (Member; __traits(allMembers, X)) {
	pragma(msg, __traits(getProtection, __traits(getMember, X, 
Member)));

}


Output:

public
private
c.d(1084): Error: argument string has no protection
c.d(1084):while evaluating pragma(msg, 
__traits(getProtection, string))



What I want to achieve essentially is skip all X's aliases and 
templates, but get everything else, like enums and data 
members. How would I go about this in a robust way?

I actually want to skip aliases, and templates.



__traits(compiles) is your friend:

unittest {
static struct X {
int x;
private enum T = 1;
private alias M = string;
}

foreach (Member; __traits(allMembers, X)) {
static if (__traits(compiles, __traits(getProtection, 
__traits(getMember, X, Member {
pragma(msg, __traits(getProtection, 
__traits(getMember, X, Member)));

}
}
}

--
  Simen



Re: OK, I'm stumped on this one: dstep, struct, mixin, bitfields

2018-03-22 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 19:41:39 UTC, H. S. Teoh wrote:

version(all) { ... }
version(none) { ... }
version(Posix) { ... }
version(Windows) { ... }

But yeah, using "version" for this purpose makes the very 
common identifier "version" unavailable for use.  I've been 
bitten by this multiple times.


auto version = getVersion();// NG
auto ver = getVersion();// OK

struct PacketHeader {
ushort version; // NG
ushort ver; // OK
}


C code also uses `in`, `out` and `with` as identifiers.

I think, with some funny escape sequences we can do without any 
keyword at all:

\1(all) { ... }
\1(none) { ... }
\1(Posix) { ... }
\1(Windows) { ... }
\5 version = getVersion();
\2 PacketHeader {
\6 version;
}


Checking if a flag was passed to the compiler

2018-03-22 Thread Nordlöw via Digitalmars-d-learn
Is there a way to check in code whether a specific flag, -dip1000 
in my case, was passed to the compiler?


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Kagamin via Digitalmars-d-learn

On Thursday, 22 March 2018 at 09:04:12 UTC, Dukc wrote:
So the real difference is that random-accesss ranges are not 
input-ranges in C# (or Rust, judging by a quick look I recently 
took) but can be used to fetch one. A bit cumbersome perhaps, 
but logical. I think I understand it now. Thank you.


IList inherits from IEnumerable, which is an input range.


Re: OK, I'm stumped on this one: dstep, struct, mixin, bitfields

2018-03-22 Thread Russel Winder via Digitalmars-d-learn
On Wed, 2018-03-21 at 22:18 +0100, Jacob Carlborg via Digitalmars-d-learn
wrote:
> On 2018-03-21 20:30, Russel Winder wrote:
> 
> > Thanks to Adam and Ali, it was clear and obvious.
> 
> Please report and issue so it's not forgotten.

I am guessing you mean against DStep rather than D :-)

Though clearly T and I would prefer version not to be a D keyword.

I suspect I have seen one place where DStep has turned version into version_
where version was a variable name. In this case "version" was a string used to
create a bitfield using a mixin(bitfield!(…)) construct. Is that likely easy
to fix?  

-- 
Russel.
==
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


signature.asc
Description: This is a digitally signed message part


Help debugging an core.exception.OutOfMemoryError

2018-03-22 Thread Danny Arends via Digitalmars-d-learn

Hey all,

When running a D program that i wrote (32bit mode, windows10), an 
OutOfMemory exception being thrown when executing the program:


core.exception.OutOfMemoryError@src\core\exception.d(702): Memory 
allocation failed


However there is no stack trace being generated (due to the 
program being out of memory).


How do I figure out where in the user code the out of memory 
error is coming from/occurring ?


Thanks in advance,
Danny

ps. Using Linux 64bit, this error does not occur


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Dukc via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 08:40:25 UTC, Kagamin wrote:

On Wednesday, 21 March 2018 at 07:40:01 UTC, Dukc wrote:
...except that IEnumerables cannot popBack(), so can only do 
that by doing an additional copy and reversing that. But I 
quess there's no better alternative, short of doing it 
C-style...


A random access range would be represented as IList, 
backward iteration can be just a part of chunking.


So the real difference is that random-accesss ranges are not 
input-ranges in C# (or Rust, judging by a quick look I recently 
took) but can be used to fetch one. A bit cumbersome perhaps, but 
logical. I think I understand it now. Thank you.


Re: Understanding slide

2018-03-22 Thread Jordan Wilson via Digitalmars-d-learn

On Thursday, 22 March 2018 at 03:58:35 UTC, Seb wrote:

On Thursday, 22 March 2018 at 03:39:38 UTC, Jordan Wilson wrote:

auto a = iota(5).slide!(Yes.withPartial)(3);
auto b = iota(5).slide!(No.withPartial)(3);
assert (a.equal(b));

The assert passes, but I would expect it to fail? They both 
are:

[[0,1,2],[1,2,3],[2,3,4]]

Thanks,

Jordan


See:
https://forum.dlang.org/post/asocdlqaihkskiilr...@forum.dlang.org


Ah I see. Apologies, I normally read the forums every day, must 
have just missed this one.


Thanks


Re: Manipulate slice or specific element of range and return range

2018-03-22 Thread David Bennett via Digitalmars-d-learn

On Thursday, 22 March 2018 at 04:49:39 UTC, Seb wrote:


No need for regular expressions. D is powerful enough without 
them:


```
alias lowercased = (m, n) => 
m.take(n).asLowerCase.chain(m.drop(n));

```

https://run.dlang.io/is/cSL0si


And with the filter, so it passes the assert:

```
string input = "My Capital String";
auto lower1 = 
input.take(1).asLowerCase.chain(input.drop(1).filter!(c => c != ' 
')).array;

assert(lower1 == "myCapitalString");
```

Also a few more options (including a slice version):

https://run.dlang.io/is/dZHcSo

As the input is never read until the array() is run, each element 
in the array should only be copied from RAM to the stack once.


And if you compile with LDC a lot of it can be inlined and the 
stack copies of the range structs and elements should be 
minimised even further.


Re: recursive template expansion: Why does this not compile?

2018-03-22 Thread Ontonator via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 23:05:22 UTC, Jonathan M Davis 
wrote:
On Wednesday, March 21, 2018 22:50:32 Ontonator via 
Digitalmars-d-learn wrote:

On Wednesday, 21 March 2018 at 06:39:22 UTC, ag0aep6g wrote:
> On 03/21/2018 01:47 AM, Ontonator wrote:
>> The following code does not compile:
>>> [...]
>>
>> It gives the error:
>>> [...]
>>
>> The aliases do not have to be aliases, as long as there is 
>> some reference to the class (e.g. method and variable 
>> declarations also work). What exactly is the reason for 
>> this error?

>
> Compiler bug. It works when you move the declaration of `B` 
> before the one of `A`. Order shouldn't matter there.


Is this a known bug, or should I report it?


If you can't find it searching on bugzilla, report it.

- Jonathan M Davis


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


Re: Calling Windows Command

2018-03-22 Thread rumbu via Digitalmars-d-learn

On Wednesday, 21 March 2018 at 18:50:38 UTC, Vino wrote:

Hi All,

 Request your help in calling the windows command to delete all 
file and folders recursively as the D function rmdirRecurse 
does not delete file in the permission of the file is readonly 
in windows 2008 R2


import std.process: execute;
import std.array: empty;
auto RemoveDir () (
auto dFiles = "C:\\Temp\Test1";
auto Step = "run";
if (Step == "run" && !dFiles.empty) {
version(Windows)
 {
  foreach(d; dFiles) {
  execute(["rmdir.exe","-command", "/S /Q", `~d~`]); } } } 
return dFiles;

)

void main () {
writeln(RemoveDir);
}

From,
Vino.N


It is faster if you use SHFileOperationW directly on Windows (and 
also it will ignore readonly attributes):


https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx


//not tested
void winrmdir(string path)
{
import core.sys.windows.shellapi;
import std.exception : enforce;
import std.file: FileException;
import std.utf: toUTF16;

auto shpath = (path ~ "\0\0").toUTF16();
SHFILEOPSTRUCTW s;
s.wFunc = FO_DELETE;
s.pFrom = shpath.ptr;
s.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
enforce!FileException(SHFileOperationW() == 0 && 
!s.fAnyOperationsAborted);

}