Re: std.data.json formal review

2015-08-16 Thread Walter Bright via Digitalmars-d

On 8/15/2015 11:52 PM, Dmitry Olshansky wrote:

For instance combining decoding and character classification one may side-step
generating the codepoint value itself (because now it doesn't have to produce it
for the top-level algorithm).


Perhaps, but I wouldn't be convinced without benchmarks to prove it on a 
case-by-case basis.


But it's moot, as json lexing never needs to decode.



[Issue 14925] New: replaceInPlace fail compilation

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14925

  Issue ID: 14925
   Summary: replaceInPlace fail compilation
   Product: D
   Version: D2
  Hardware: x86
OS: Linux
Status: NEW
  Severity: enhancement
  Priority: P1
 Component: dmd
  Assignee: nob...@puremagic.com
  Reporter: ts...@free.fr

This code won't compile 

 import std.array;
 import std.stdio;  

 void main()
 {
  char[] a = mon texte 1.dup;
  char[] b = abc.dup;
  size_t x   = 4;
  size_t y   = 9;
  replaceInPlace( a, x , y, b );
  writeln( a );
 }

Compilation fail with error :

replaceInPlace.d(11): Error: std.array.replaceInPlace called with argument
types (char[], uint, uint, char[]) matches both:
/usr/include/dmd/phobos/std/array.d(2279): std.array.replaceInPlace!(char,
char[]).replaceInPlace(ref char[] array, uint from, uint to, char[] stuff)
and:
/usr/include/dmd/phobos/std/array.d(2312): std.array.replaceInPlace!(char,
char[]).replaceInPlace(ref char[] array, uint from, uint to, char[] stuff)

Tested on DMD32 D Compiler v2.068.0-rc1

--


Re: mime - library for parsing shared MIME database

2015-08-16 Thread FreeSlave via Digitalmars-d-announce

On Sunday, 16 August 2015 at 03:56:45 UTC, Rikki Cattermole wrote:

On 16/08/2015 6:30 a.m., FreeSlave wrote:

Currently I'm working on mime library for D. Dub page:
http://code.dlang.org/packages/mime
It can parse MIME database files, including binary ones, like
mime.cache. It also has algorithms for mime type detecting by 
file name.


It's not fully implemented yet and does not have stable API. 
Issues and
goals are listed on the github page: 
https://github.com/MyLittleRobo/mime


If someone is interested in the project, I would be glad to 
discuss

interface and implementation details of the library.

If you don't know what is shared MIME database and why does it 
matter

read this:
http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html#idm140625831778224


I had an mime implementation in Cmsed that basically was a hard 
coded file with a whole bunch of mime types along with file 
extensions.


I would be interested in seeing if this can match it 1:1 for 
features, while not allocating. Say give me the mime type for 
payload.


Possibly with its own override/addition csv files.


This library focuses on shared MIME database used in freedesktop 
systems, usually for detecting file types in file managers to 
display appropriate icon and make correct choice of default 
application (well, that's another spec) to run on file. I don't 
think this suits the web world.


Your hardcoded approach is what usually used in web. If I 
remember correctly mime types are hardcoded in Chromium too.
But mime type is not only about extension. Generally pattern can 
be any glob pattern. That's why file managers can detect Makefile 
type as text/x-makefile, even though it does not have extension. 
Same for CMakeLists.txt - the preferred type is text/x-cmake, not 
just text/plain.


Patterns are not alone. There're magic rules for the rescue when 
mime type can't be detected from the name of file. That's how 
Linux file managers differ shell script from python script even 
if both don't have extension (but they have leading comment like 
#!/bin/sh or #!/usr/bin/python). Also that's how file managers 
detect file with unknown pk3 extension (used in Quake III based 
games) as zip file. Because it's really just zip file by its 
contents.


The whole shared MIME database thing is system and user 
dependent, so again it's not what you want to use for web, 
probably unless you manage the database yourself on the server. 
Even in this case patterns and magic rules are just hints. You 
can't rely on that to check if the uploading file is of the 
needed type. For example, if you want to validate image file, the 
only way to do it is to parse the whole file. Still you can use 
hints to cut off obviously invalid files.


I did not target non-allocating code yet, though I believe it's 
possible to make MimeCache to not allocate when detecting file 
type.


Re: std.data.json formal review

2015-08-16 Thread Dmitry Olshansky via Digitalmars-d

On 16-Aug-2015 03:50, Walter Bright wrote:

On 8/15/2015 3:18 AM, Sönke Ludwig wrote:

There is no reason to validate UTF-8 input. The only place where
non-ASCII code units can even legally appear is inside strings, and
there they can just be copied verbatim while looking for the end of the
string.

The idea is to assume that any char based input is already valid UTF
(as D
defines it), while integer based input comes from an unverified
source, so that
it still has to be validated before being cast/copied into a 'string'.
I think
this is a sensible approach, both semantically and performance-wise.


The json parser will work fine without doing any validation at all. I've
been implementing string handling code in Phobos with the idea of doing
validation only if the algorithm requires it, and only for those parts
that require it.



Aye.


There are many validation algorithms in Phobos one can tack on - having
two implementations of every algorithm, one with an embedded reinvented
validation and one without - is too much.


Actually there are next to none. `validate` that throws on failed 
validation is a misnomer.



The general idea with algorithms is that they do not combine things, but
they enable composition.



At the lower level such as tokenizers combining a couple of simple steps 
together makes sense because it makes things run faster. It usually 
eliminates the need for temporary result that must be digestible by the 
next range.


For instance combining decoding and character classification one may 
side-step generating the codepoint value itself (because now it doesn't 
have to produce it for the top-level algorithm).



--
Dmitry Olshansky


[Issue 14832] iota(UL) + array doesn't work

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14832

Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 OS|Windows |All

--- Comment #4 from Steven Schveighoffer schvei...@yahoo.com ---
(In reply to Walter Bright from comment #2)
 To me this looks invalid. I see no reason to support creating arrays from
 ulongs.

You mean from ranges with ulong length? I don't see why. ulong[] is a valid
type, and a reasonable request. How do I make a ulong[] from iota on 32-bit?

(In reply to Dmitry Olshansky from comment #3)
 Also the funny thing is that
 https://github.com/D-Programming-Language/phobos/pull/3544
 
 adds support for ulong length which AFAIK only iota on ulong give and
 BECAUSE the element type is ulong.sizeof == 8 bytes long nobody will ever be
 able to call array on  ~512 million long iota. 

The point is not to make arrays of larger than size_t elements, the point is to
make arrays out of small ranges that happen to use ulong as a length.

 So all of that effort is leading nowhere, just drop the ulong length please.

You mean from iota? or support from array? Why should a range that has a ulong
length of 10 be disallowed?

Note that a file may have ulong length even on a 32-bit system. This would mean
creating an array out of a file stream range (that reasonably supplies a ulong
length) would be disallowed, even when the length is small.

I really don't understand the objections here, this code is very reasonable. It
means code that makes sense on both 32-bit and 64-bit systems fails on 32-bit
systems.

--


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 16 August 2015 at 12:04:51 UTC, cym13 wrote:

To me the obvious way is to use a lambda, not a delegate:


Lambdas and delegates are the same thing, just different syntax.



Re: D for project in computational chemistry

2015-08-16 Thread Rikki Cattermole via Digitalmars-d

On 17/08/2015 1:11 a.m., Yura wrote:

Good afternoon, gentlemen,

just want to describe my very limited experience. I have re-written
about half of my Python code into D. I got it faster by 6 times. This is
a good news.

However, I was amazed by performance of D vs Python for following simple
nested loops (see below). D was faster by 2 order of magnitude!

Bearing in mind that Python is really used in computational
chemistry/bioinformatics, I am sure D can be a good option in this
field. In the modern strategy for the computational software python is
used as a glue language and the number crunching parts are usually
written in Fortran or C/C++. Apparently, with D one language can be used
to write the entire code. Please, also look at this article:

http://www.worldcomp-proceedings.com/proc/p2012/PDP3426.pdf

Also, I wander about the results of this internship:

http://forum.dlang.org/post/laha9j$pc$1...@digitalmars.com

With kind regards,
Yury


Python:

#!/usr/bin/python
import sys, string, os, glob, random
from math import *

a = 0

l = 1000

for i in range(l):
 for j in range(l):
 for m in range(l):
 a = a +i*i*0.7+j*j*0.8+m*m*0.9

print a

D:

import std.stdio;
// command line argument
import std.getopt;
import std.string;
import std.array;
import std.conv;
import std.math;

// main program starts here
void main(string[] args) {


int l = 1000;
double a = 0;
for (auto i=0;il;i++){
 for (auto j=0;jl;j++) {
 for (auto m=0;ml;m++) {
 a = a + i*i*0.7+j*j*0.8+m*m*0.9;
 }

 }
}
writeln(a);
}


Any chance for when you get the time/content, to create a research paper 
using your use case?

It would be amazing publicity and even more so to get it published!

Otherwise, we could always do with another user story :)


Re: Using replaceInPlace, string and char[]

2015-08-16 Thread TSalm via Digitalmars-d-learn

 Must create a ticket for it ?

I think so. Unless others object in 10 minutes... :)

:-)
Done : https://issues.dlang.org/show_bug.cgi?id=14925

Thanks for your help


2.068 Regression in EnumMembers?

2015-08-16 Thread Nordlöw

I tried rebuilding my knowledge graph project at

https://github.com/nordlow/justd/tree/master/knet

with DMD 2.068 and it seems like we have a regression in 
std.traits: EnumMembers:


/usr/include/dmd/phobos/std/traits.d(3432,21): Error: template 
instance std.traits.EnumMembers!(Lang).WithIdentifier!fortran 
recursive expansion

scons: *** [knet/traversal.o] Error 1
/usr/include/dmd/phobos/std/traits.d(3432,21): Error: template 
instance 
std.traits.EnumMembers!(Lang).WithIdentifier!firstAcademic 
recursive expansion

scons: *** [knet/base.o] Error 1
/usr/include/dmd/phobos/std/traits.d(3432,21): Error: template 
instance 
std.traits.EnumMembers!(const(Lang)).WithIdentifier!physics 
recursive expansion

scons: *** [knet/io.o] Error 1
/usr/include/dmd/phobos/std/traits.d(3432,21): Error: template 
instance std.traits.EnumMembers!(Lang).WithIdentifier!modelica 
recursive expansion

scons: *** [knet/tests.o] Error 1

Sadly the diagnositcs give no help whatsoever on what's wrong. 
What happened to the instantiation stack trace here?


Try cloning https://github.com/nordlow/justd/tree

and building for instance

scons knet/base.o

provided that SCons has been installed (on Ubuntu as `sudo 
apt-get install scons`)


It builds without errors nor warnings on 2.067.

Help, please.


Re: 2.068 Regression in EnumMembers?

2015-08-16 Thread Nordlöw

On Sunday, 16 August 2015 at 11:25:48 UTC, Nordlöw wrote:

I tried rebuilding my knowledge graph project at

https://github.com/nordlow/justd/tree/master/knet


Should be, https://github.com/nordlow/justd.


Re: 2.068 Regression in EnumMembers?

2015-08-16 Thread Nordlöw

On Sunday, 16 August 2015 at 11:25:48 UTC, Nordlöw wrote:

Try cloning https://github.com/nordlow/justd/tree


Should be:

git clone https://github.com/nordlow/justd


How to convert a ubyte to a byte without losing information?

2015-08-16 Thread CharmingChocolate via Digitalmars-d-learn
I can cast a ubyte to a byte, but as far as I know I'd only get 
positive values out of that conversion. If I was instead getting 
a ubyte out of a function and I want to assign those bits to a 
byte and have some of those numbers be interpreted as negative 
values by the new type they're in, how would I cast that in D?


Re: D for project in computational chemistry

2015-08-16 Thread Yura via Digitalmars-d

Good afternoon, gentlemen,

just want to describe my very limited experience. I have 
re-written about half of my Python code into D. I got it faster 
by 6 times. This is a good news.


However, I was amazed by performance of D vs Python for following 
simple nested loops (see below). D was faster by 2 order of 
magnitude!


Bearing in mind that Python is really used in computational 
chemistry/bioinformatics, I am sure D can be a good option in 
this field. In the modern strategy for the computational software 
python is used as a glue language and the number crunching parts 
are usually written in Fortran or C/C++. Apparently, with D one 
language can be used to write the entire code. Please, also look 
at this article:


http://www.worldcomp-proceedings.com/proc/p2012/PDP3426.pdf

Also, I wander about the results of this internship:

http://forum.dlang.org/post/laha9j$pc$1...@digitalmars.com

With kind regards,
Yury


Python:

#!/usr/bin/python
import sys, string, os, glob, random
from math import *

a = 0

l = 1000

for i in range(l):
for j in range(l):
for m in range(l):
a = a +i*i*0.7+j*j*0.8+m*m*0.9

print a

D:

import std.stdio;
// command line argument
import std.getopt;
import std.string;
import std.array;
import std.conv;
import std.math;

// main program starts here
void main(string[] args) {


int l = 1000;
double a = 0;
for (auto i=0;il;i++){
for (auto j=0;jl;j++) {
for (auto m=0;ml;m++) {
a = a + i*i*0.7+j*j*0.8+m*m*0.9;
}

}
}
writeln(a);
}





Re: std.data.json formal review

2015-08-16 Thread Dmitry Olshansky via Digitalmars-d

On 16-Aug-2015 11:30, Walter Bright wrote:

On 8/15/2015 11:52 PM, Dmitry Olshansky wrote:

For instance combining decoding and character classification one may
side-step
generating the codepoint value itself (because now it doesn't have to
produce it
for the top-level algorithm).


Perhaps, but I wouldn't be convinced without benchmarks to prove it on a
case-by-case basis.


About x2 faster then decode + check-if-alphabetic on my stuff:

https://github.com/DmitryOlshansky/gsoc-bench-2012

I haven't updated it in a while. There are nice bargraphs for decoding 
versions by David comparing DMD vs LDC vs GDC:


Page 15 at http://dconf.org/2013/talks/nadlinger.pdf



But it's moot, as json lexing never needs to decode.


Agreed.

--
Dmitry Olshansky


∅MQD messaging library v1.0 released

2015-08-16 Thread Lars T. Kyllingstad via Digitalmars-d-announce
I am happy to announce the (somewhat overdue) release of ∅MQD 
v1.0!


∅MQD is a D library that wraps the low-level C API of the ∅MQ 
(aka. ZeroMQ) messaging framework.  It is a rather thin wrapper 
that maps closely to the C API, while providing modern D features 
that make the framework safer, easier and more pleasant to use.  
(In other words, ∅MQD is not just a plain C binding(*)!)


Code: https://github.com/kyllingstad/zmqd
Dub package:  http://code.dlang.org/packages/zmqd
API docs: https://kyllingstad.github.io/zmqd/zmqd_v1.0.html

This version works with ∅MQ v4.x, and has 100% support for all 
∅MQ v4.0 functionality.  A few small ∅MQ 4.1 features are 
missing, but support for those will be added in an upcoming 1.1 
release in the not-too-distant future.


Before use, please have a look at the README, specifically the 
section A word of caution about the C library bindings.  The 
∅MQ guys have introduced some ABI incompatibilities in recent 
versions, so it it important that the D-to-C bindings match the 
library version you have installed.


-Lars


(*) If plain C bindings are what you want, check out 
Deimos/ZeroMQ:


https://github.com/D-Programming-Deimos/ZeroMQ
http://code.dlang.org/packages/zeromq

∅MQD depends on these bindings, so I try to keep them up to date 
with stable ∅MQ releases.






Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn

On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote:

Let's say I want to map some range using some context.
The obvious way is to do:

uint[3] arr = [1,2,3];
uint context = 2;
auto r = arr[].map!(delegate(value) { return value * context; 
});


The problem is that this allocates delegate, so it can't be 
used in @nogc code.

What I want to do might look like this:

static struct Caller
{
this(uint context) @nogc {
_context = context;
}
auto opCall(uint value) @nogc {
return value * _context;
}
uint _context;
}

auto caller = Caller(2);
auto r = arr[].map!(caller.opCall);

But it will not work of course since function must be a 
compile-time parameter.


So the way to go would be:

auto caller = Caller(2);
auto r = arr[].map!(Caller.opCall)(caller);

But map and other algorithms don't support this interface.

The other way is

auto r = arr.map!(Caller(2));

But again, since it's template parameter, it can't use 
variables unknown at compile time:


uint context = ...;
auto r = arr.map!(Caller(context)); //will not work

So what's the solution? Of course besides rewriting the whole 
std.algorithm.


Ok, so as my lambda proposition obviously doesn't work, here is 
one way that does using a templated function. There may be a way 
to make it shorter, I don't know.



import std.conv;
import std.stdio;

template fun(uint context) {
static uint withContext(uint value) {
return value * context;
}

auto fun(uint[] arr) @nogc {
return arr.map!withContext;
}
}

void main(string[] args) {
[1, 2, 3].to!(uint[])
 .fun!2
 .writeln;
}



Re: ∅MQD messaging library v1.0 released

2015-08-16 Thread Nick Sabalausky via Digitalmars-d-announce
Cool. I haven't actually used ZeroMQ yet, but it's something I've had my 
eye on.


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 16 August 2015 at 12:30:54 UTC, cym13 wrote:

On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote:

[...]


Ok, so as my lambda proposition obviously doesn't work, here is 
one way that does using a templated function. There may be a 
way to make it shorter, I don't know.



import std.conv;
import std.stdio;

template fun(uint context) {
static uint withContext(uint value) {
return value * context;
}

auto fun(uint[] arr) @nogc {
return arr.map!withContext;
}
}

void main(string[] args) {
[1, 2, 3].to!(uint[])
 .fun!2
 .writeln;
}


It works only because 2 is known constant at compile time.


Re: Mid level IR

2015-08-16 Thread via Digitalmars-d

On Saturday, 15 August 2015 at 21:13:33 UTC, deadalnix wrote:
On Friday, 14 August 2015 at 16:13:07 UTC, Ola Fosheim Grøstad 
wrote:


Another option would be to interface with Rust by generating 
Rust MIR from D, if that is possible.


What do you think?


This is what SDC is doing already.


Interesting, is the format documented somewhere?


How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn

Let's say I want to map some range using some context.
The obvious way is to do:

uint[3] arr = [1,2,3];
uint context = 2;
auto r = arr[].map!(delegate(value) { return value * context; });

The problem is that this allocates delegate, so it can't be used 
in @nogc code.

What I want to do might look like this:

static struct Caller
{
this(uint context) @nogc {
_context = context;
}
auto opCall(uint value) @nogc {
return value * _context;
}
uint _context;
}

auto caller = Caller(2);
auto r = arr[].map!(caller.opCall);

But it will not work of course since function must be a 
compile-time parameter.


So the way to go would be:

auto caller = Caller(2);
auto r = arr[].map!(Caller.opCall)(caller);

But map and other algorithms don't support this interface.

The other way is

auto r = arr.map!(Caller(2));

But again, since it's template parameter, it can't use variables 
unknown at compile time:


uint context = ...;
auto r = arr.map!(Caller(context)); //will not work

So what's the solution? Of course besides rewriting the whole 
std.algorithm.


Re: 2.068 Regression in EnumMembers?

2015-08-16 Thread anonymous via Digitalmars-d-learn

On Sunday, 16 August 2015 at 11:25:48 UTC, Nordlöw wrote:

I tried rebuilding my knowledge graph project at

https://github.com/nordlow/justd/tree/master/knet

with DMD 2.068 and it seems like we have a regression in 
std.traits: EnumMembers:


[...]

It builds without errors nor warnings on 2.067.

Help, please.


Did it still work in 2.068a ?
If so then it's possible that the resolution of issue 14844, 
which changed the traits allMembers, is the cause.


Because if you look at std.traits 'blame' on GH
(https://github.com/D-Programming-Language/phobos/blame/master/std/traits.d , 
around line 3412) that's clear that the template EnumMembers hasn't changed 
since February 2014. So it must be one of the call inside...

Just an idea.


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn

On Sunday, 16 August 2015 at 11:53:42 UTC, FreeSlave wrote:

Let's say I want to map some range using some context.
The obvious way is to do:

uint[3] arr = [1,2,3];
uint context = 2;
auto r = arr[].map!(delegate(value) { return value * context; 
});


To me the obvious way is to use a lambda, not a delegate:

uint[3] arr = [1,2,3];
uint context = 2;
auto r = arr[].map!(value = value * context);


Re: std.data.json formal review

2015-08-16 Thread Sönke Ludwig via Digitalmars-d

Am 16.08.2015 um 02:50 schrieb Walter Bright:

On 8/15/2015 3:18 AM, Sönke Ludwig wrote:

I don't know what 'isStringInputRange' is. Whatever it is, it should be
a 'range of char'.


I'll rename it to isCharInputRange. We don't have something like that
in Phobos,
right?


That's right, there isn't one. But I use:

 if (isInputRange!R  is(Unqual!(ElementEncodingType!R) == char))

I'm not a fan of more names for trivia, the deluge of names has its own
costs.


Good, I'll use `if (isInputRange!R  
(isSomeChar!(ElementEncodingType!R) || 
isIntegral!(ElementEncodingType!R))`. It's just used in number of places 
and quite a bit more verbose (twice as long) and I guess a large number 
of algorithms in Phobos accept char ranges, so that may actually warrant 
a name in this case.



There is no reason to validate UTF-8 input. The only place where
non-ASCII code units can even legally appear is inside strings, and
there they can just be copied verbatim while looking for the end of the
string.

The idea is to assume that any char based input is already valid UTF
(as D
defines it), while integer based input comes from an unverified
source, so that
it still has to be validated before being cast/copied into a 'string'.
I think
this is a sensible approach, both semantically and performance-wise.


The json parser will work fine without doing any validation at all. I've
been implementing string handling code in Phobos with the idea of doing
validation only if the algorithm requires it, and only for those parts
that require it.


Yes, and it won't do that if a char range is passed in. If the integral 
range path gets removed there are basically two possibilities left, 
perform the validation up-front (slower), or risk UTF exceptions in 
unrelated parts of the code base. I don't see why we shouldn't take the 
opportunity for a full and fast validation here. But I'll relay this to 
Andrei, it was his idea originally.



There are many validation algorithms in Phobos one can tack on - having
two implementations of every algorithm, one with an embedded reinvented
validation and one without - is too much.


There is nothing reinvented here. It simply implicitly validates all 
non-string parts of a JSON document and uses validate() for parts of 
JSON strings that can contain unicode characters.



The general idea with algorithms is that they do not combine things, but
they enable composition.


It's just that there is no way to achieve the same performance using 
composition in this case.



Why do both? Always return an input range. If the user wants a string,
he can pipe the input range to a string generator, such as .array

Convenience for one.


Back to the previous point, that means that every algorithm in Phobos
should have two versions, one that returns a range and the other a
string? All these variations will result in a combinatorical explosion.


This may be a factor of two, but not a combinatorial explosion.


The other problem, of course, is that returning a string means the
algorithm has to decide how to allocate that string. As much as
possible, algorithms should not be making allocation decisions.


Granted, the fact that format() and to!() support input ranges (I didn't 
notice that until now) makes the issue less important. But without 
those, it would basically mean that almost all places that generate JSON 
strings would have to import std.array and append .array. Nothing 
particularly bad if viewed in isolation, but makes the language appear a 
lot less clean/more verbose if it occurs often. It's also a stepping 
stone for language newcomers.



The lack of number to input range conversion functions is
another concern. I'm not really keen to implement an input range style
floating-point to string conversion routine just for this module.


Not sure what you mean. Phobos needs such routines anyway, and you still
have to do something about floating point.


There are output range and allocation based float-string conversions 
available, but no input range based one. But well, using an internal 
buffer together with formattedWrite would probably be a viable workaround...



Finally, I'm a little worried about performance. The output range
based approach
can keep a lot of state implicitly using the program counter register.
But an
input range would explicitly have to keep track of the current JSON
element, as
well as the current character/state within that element (and possibly
one level
deeper, for example for escape sequences). This means that it will
require
either multiple branches or indirection for each popFront().


Often this is made up for by not needing to allocate storage. Also, that
state is in the cached hot zone on top of the stack, which is much
faster to access than a cold uninitialized array.


Just branch misprediction will most probably be problematic. But I think 
this can be made fast enough anyway by making the input range partially 
eager and serving chunks of strings at a time. That 

Re: std.data.json formal review

2015-08-16 Thread Walter Bright via Digitalmars-d

On 8/16/2015 3:39 AM, Dmitry Olshansky wrote:

About x2 faster then decode + check-if-alphabetic on my stuff:

https://github.com/DmitryOlshansky/gsoc-bench-2012

I haven't updated it in a while. There are nice bargraphs for decoding versions
by David comparing DMD vs LDC vs GDC:

Page 15 at http://dconf.org/2013/talks/nadlinger.pdf


Thank you.


Re: How to convert a ubyte to a byte without losing information?

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 16 August 2015 at 11:43:24 UTC, CharmingChocolate 
wrote:
I can cast a ubyte to a byte, but as far as I know I'd only get 
positive values out of that conversion.


ubyte b = 255;
byte b2 = cast(byte) b;
assert(b2 == -1);


No information is lost by that cast, all the bits remain exactly 
the same.


Re: ∅MQD messaging library v1.0 released

2015-08-16 Thread Lars T. Kyllingstad via Digitalmars-d-announce

On Sunday, 16 August 2015 at 12:53:39 UTC, Nick Sabalausky wrote:
Cool. I haven't actually used ZeroMQ yet, but it's something 
I've had my eye on.


If you want to look into it, I heartily recommend the ZeroMQ 
Guide:


http://zguide.zeromq.org/

In addition to being a comprehensive and well-written 
(entertaining, even) user's guide for ZeroMQ, it contains a lot 
of good information and advice on how to build distributed 
software systems in general.


I have translated the examples for the first few chapters to D.  
You'll find them in the examples subdirectory of the ∅MQD repo.


-Lars


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn

On 08/16/2015 04:53 AM, FreeSlave wrote:

 The problem is that this allocates delegate, so it can't be used in
 @nogc code.

Would constructing the delegate by setting its .funcptr and .ptr 
properties work in this case? You can have a pool of context objects 
which become the context for the delegate.


  http://ddili.org/ders/d.en/lambda.html#ix_lambda..funcptr

Ali



Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 16 August 2015 at 16:23:05 UTC, FreeSlave wrote:

On Sunday, 16 August 2015 at 15:29:10 UTC, Ali Çehreli wrote:

On 08/16/2015 04:53 AM, FreeSlave wrote:

 The problem is that this allocates delegate, so it can't be
used in
 @nogc code.

Would constructing the delegate by setting its .funcptr and 
.ptr properties work in this case? You can have a pool of 
context objects which become the context for the delegate.


  http://ddili.org/ders/d.en/lambda.html#ix_lambda..funcptr

Ali


I don't see how this can solve the problem.

What I tried:

import std.stdio;
import std.range;
import std.algorithm;

struct Caller
{
this(uint context) {
_context = context;
}
uint method(uint value) {
return _context * value;
}

uint _context;
}

@nogc auto func(uint[] arr, uint function(uint) f)
{
return arr.map!(f);
}

void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = c.method;
writeln(func(arr, d.funcptr));
}

It still says it needs allocation:

test.d(17): Error: function test.func @nogc function allocates 
a closure with the GC


funcptr does not play any role here, since passing the delegate 
directly leads to the same error.


Forgot about data ptr.

@nogc auto func(uint[] arr, uint function(uint) f, void* data)
{
uint delegate(uint) d;
d.funcptr = f;
d.ptr = data;
return arr.map!(d);
}

void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = c.method;
writeln(func(arr, d.funcptr, d.ptr));
}

Still the same error though.


Re: std.data.json formal review

2015-08-16 Thread Jacob Carlborg via Digitalmars-d

On 2015-08-16 14:34, Sönke Ludwig wrote:


Good, I'll use `if (isInputRange!R 
(isSomeChar!(ElementEncodingType!R) ||
isIntegral!(ElementEncodingType!R))`. It's just used in number of places
and quite a bit more verbose (twice as long) and I guess a large number
of algorithms in Phobos accept char ranges, so that may actually warrant
a name in this case.


I agree. Signatures like this are what's making std.algorithm look more 
complicated than it is.


--
/Jacob Carlborg


Re: D for project in computational chemistry

2015-08-16 Thread Idan Arye via Digitalmars-d

On Sunday, 16 August 2015 at 13:11:12 UTC, Yura wrote:

Good afternoon, gentlemen,

just want to describe my very limited experience. I have 
re-written about half of my Python code into D. I got it faster 
by 6 times. This is a good news.


However, I was amazed by performance of D vs Python for 
following simple nested loops (see below). D was faster by 2 
order of magnitude!


Bearing in mind that Python is really used in computational 
chemistry/bioinformatics, I am sure D can be a good option in 
this field. In the modern strategy for the computational 
software python is used as a glue language and the number 
crunching parts are usually written in Fortran or C/C++. 
Apparently, with D one language can be used to write the entire 
code. Please, also look at this article:


http://www.worldcomp-proceedings.com/proc/p2012/PDP3426.pdf

Also, I wander about the results of this internship:

http://forum.dlang.org/post/laha9j$pc$1...@digitalmars.com

With kind regards,
Yury


Python:

#!/usr/bin/python
import sys, string, os, glob, random
from math import *

a = 0

l = 1000

for i in range(l):
for j in range(l):
for m in range(l):
a = a +i*i*0.7+j*j*0.8+m*m*0.9

print a

D:

import std.stdio;
// command line argument
import std.getopt;
import std.string;
import std.array;
import std.conv;
import std.math;

// main program starts here
void main(string[] args) {


int l = 1000;
double a = 0;
for (auto i=0;il;i++){
for (auto j=0;jl;j++) {
for (auto m=0;ml;m++) {
a = a + i*i*0.7+j*j*0.8+m*m*0.9;
}

}
}
writeln(a);
}


Initially I thought the Python version is so slow because it uses 
`range` instead of `xrange`, but I tried them both and they both 
take about the same, so I guess the Python JIT(or even 
interpreter!) can optimize these allocations away.


BTW - if you want to iterate over a range of numbers in D, you 
can use a foreach loop:


foreach (i; 0 .. l) {
foreach (j; 0 .. l) {
foreach (m; 0 .. l) {
a = a + i * i * 0.7 + j * j * 0.8 + m * m * 0.9;
}

}
}

Or, to make it look more like the Python version, you can iterate 
over a range-returning function:


import std.range : iota;
foreach (i; iota(l)) {
foreach (j; iota(l)) {
foreach (m; iota(l)) {
a = a + i * i * 0.7 + j * j * 0.8 + m * m * 0.9;
}

}
}

There are also functions for building ranges from other ranges:

import std.algorithm : cartesianProduct;
import std.range : iota;
foreach (i, j, m; cartesianProduct(iota(l), iota(l), 
iota(l))) {

a = a + i * i * 0.7 + j * j * 0.8 + m * m * 0.9;
}

Keep in mind though that using these functions, while making the 
code more readable(to those with some experience in D, at least), 
is bad for performance - for my first version I got about 5 
seconds when building with DMD in debug mode, while for the last 
version I get 13 seconds when building with LDC in release mode.


Re: Mid level IR

2015-08-16 Thread deadalnix via Digitalmars-d
On Sunday, 16 August 2015 at 10:12:07 UTC, Ola Fosheim Grøstad 
wrote:

On Saturday, 15 August 2015 at 21:13:33 UTC, deadalnix wrote:
On Friday, 14 August 2015 at 16:13:07 UTC, Ola Fosheim Grøstad 
wrote:


Another option would be to interface with Rust by generating 
Rust MIR from D, if that is possible.


What do you think?


This is what SDC is doing already.


Interesting, is the format documented somewhere?


No, and it likely to change significantly during the dev at this 
stage.


GDB for D debugging on OS X seems to be broken

2015-08-16 Thread Jack Stouffer via Digitalmars-d-learn

For reference:
OSX 10.10.5
GDB 7.9.1 (non apple; from homebrew)
yes, it is code signed
Compiling with dub: dflags: [-gc, -gs]

I would also like to preface this post by saying that everything 
works fine in GDB on linux.


When finding that a bug in my program was a null pointer bug, I 
decided to drop into GDB to try to figure out what was happening. 
But I quickly realized that trying to set a breakpoint in a file 
other than the one where the main function is, and stepping into 
functions in a file other than the one where the main function 
is, don't work. GDB will return something like:


Cannot insert breakpoint 2.
Cannot access memory at address 0x9bc06

when trying to insert a breakpoint in another file. GDB also 
treats the aforementioned step command as a continue because it 
can't read the file where the function is defined. Nothing 
changes when running the command as sudo either.


Two more pain points: `info locals` doesn't work. It responds 
with No symbol table info available. And `info variables` 
returns the mangled D names for variables and not their real 
name, despite the language being set to D and doing `demangle 
[some_name]` returns the correct names.


App Build Error

2015-08-16 Thread Benjamin via Digitalmars-d-learn


I'm having an issue with building my app - even a simple trivial 
app (shown below).


I receive the following error message:

	cc -arch i386 -framework CoreFoundation -lobjc -liconv: No such 
file or directory

--- errorlevel 255

I've removed and reinstalled DMD - same issue.  I'm not really 
sure whats going on.	


---

BENJAMINs-MacBook-Pro:source benjamin$ ls
app.d
BENJAMINs-MacBook-Pro:source benjamin$ cat app.d
import std.stdio;

void main()
{
writeln(Hello D!);
}
BENJAMINs-MacBook-Pro:source benjamin$ dmd --version
DMD64 D Compiler v2.068
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$ dmd app.d
cc -arch i386 -framework CoreFoundation -lobjc -liconv: No such 
file or directory

--- errorlevel 255
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$
cc -arch i386 -framework CoreFoundation -lobjc -liconv app.o -o 
app -m64 -L/usr/local/Cellar/dmd/2.068.0/lib -lphobos2 -lpthread 
-lm
cc -arch i386 -framework CoreFoundation -lobjc -liconv: No such 
file or directory

--- errorlevel 255
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$
BENJAMINs-MacBook-Pro:source benjamin$ cc -arch i386 -framework 
CoreFoundation -lobjc -liconv app.o -o app -m64 
-L/usr/local/Cellar/dmd/2.068.0/lib -lphobos2 -lpthread -lm
ld: warning: ignoring file app.o, file was built for unsupported 
file format ( 0xCF 0xFA 0xED 0xFE 0x07 0x00 0x00 0x01 0x03 0x00 
0x00 0x00 0x01 0x00 0x00 0x00 ) which is not the architecture 
being linked (i386): app.o
ld: warning: ignoring file 
/usr/local/Cellar/dmd/2.068.0/lib/libphobos2.a, file was built 
for archive which is not the architecture being linked (i386): 
/usr/local/Cellar/dmd/2.068.0/lib/libphobos2.a

Undefined symbols for architecture i386:
  _main, referenced from:
 implicit entry/start for main executable
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

BENJAMINs-MacBook-Pro:source benjamin$


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 16 August 2015 at 15:29:10 UTC, Ali Çehreli wrote:

On 08/16/2015 04:53 AM, FreeSlave wrote:

 The problem is that this allocates delegate, so it can't be
used in
 @nogc code.

Would constructing the delegate by setting its .funcptr and 
.ptr properties work in this case? You can have a pool of 
context objects which become the context for the delegate.


  http://ddili.org/ders/d.en/lambda.html#ix_lambda..funcptr

Ali


I don't see how this can solve the problem.

What I tried:

import std.stdio;
import std.range;
import std.algorithm;

struct Caller
{
this(uint context) {
_context = context;
}
uint method(uint value) {
return _context * value;
}

uint _context;
}

@nogc auto func(uint[] arr, uint function(uint) f)
{
return arr.map!(f);
}

void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = c.method;
writeln(func(arr, d.funcptr));
}

It still says it needs allocation:

test.d(17): Error: function test.func @nogc function allocates a 
closure with the GC


funcptr does not play any role here, since passing the delegate 
directly leads to the same error.




SciD has a new home

2015-08-16 Thread Lars T. Kyllingstad via Digitalmars-d-announce
As of today, the main SciD repository will be hosted under the 
DlangScience organisation on GitHub:


https://github.com/DlangScience/scid

SciD is a collection of freely usable numerical routines for, 
among other things,


 - numerical integration (quadrature) and differentiation
 - nonlinear equation solving (one- and multidimensional)
 - (a little bit of) linear algebra

With this move, I hope more people will discover the library, 
find it useful, and even contribute to it.


Documentation: https://dlangscience.github.io/scid
Dub package:   http://code.dlang.org/packages/scid

-Lars


[Issue 14924] Error in std.uri definition/description

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14924

github-bugzi...@puremagic.com changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |FIXED

--


[Issue 14924] Error in std.uri definition/description

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14924

--- Comment #4 from github-bugzi...@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/169101f7bf64c2f394c39a2ba297eabd2220ecf5
fix issue 14924

https://github.com/D-Programming-Language/phobos/commit/d697e0fd4f0bcd692b0d5fedaf9baede75f8984b
added unittests for issue 14924

https://github.com/D-Programming-Language/phobos/commit/3241856fe4c84f7ae4bfb075b06aa27bb878891a
Merge pull request #3558 from BBasile/BBasile-patch-issue14924

fix issue14924

--


[Issue 14920] [REG2.067.0] SList.insertAfter on uninitialized list triggers assertion in _first

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14920

--- Comment #3 from github-bugzi...@puremagic.com ---
Commits pushed to stable at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/0c28c7717437b10570677994112fc874e196d26a
fix issue 14920

https://github.com/D-Programming-Language/phobos/commit/aac09abf0a8f8624e81bbac4ded38b941cc768d9
Merge pull request #3557 from tosttost/issue_14920

fix issue 14920 - [REG2.067.0] SList.insertAfter on uninitialized list triggers
assertion in _first targeting stable branch

--


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn

On 08/16/2015 09:26 AM, FreeSlave wrote:

 It still says it needs allocation:

 test.d(17): Error: function test.func @nogc function allocates a
 closure with the GC

I wrapped it inside a class object but it still thinks it needs to allocate:

import std.stdio;
import std.range;
import std.algorithm;

struct Caller
{
this(uint context) {
_context = context;
}
uint method(uint value) {
return _context * value;
}

uint _context;
}

class DelegateRef
{
uint delegate(uint) d;

this (uint delegate(uint) d)
{
this.d = d;
}
}

// HERE:
// Error: function deneme.func @nogc function allocates
//a closure with the GC
@nogc auto func(uint[] arr, DelegateRef d)
{
return arr.map!(a = d.d(a));
}

void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = c.method;

writeln(func(arr, new DelegateRef(d)));
}

Ali



Re: Truly lazy ranges, transient .front, and std.range.Generator

2015-08-16 Thread Alex Parrill via Digitalmars-d
On Saturday, 15 August 2015 at 10:06:13 UTC, Joseph Rushton 
Wakeling wrote:

...


I had this issue recently when reading from a command-line-style 
TCP connection; I needed to read the line up to the \n separator, 
but consuming the separator meant waiting for the next byte that 
would never arrive unless a new command was sent.


So I made a wrapper range that evaluates the wrapped range's 
popFront only when front/empty is first called (just in time). 
Source code here: 
https://gist.github.com/ColonelThirtyTwo/0dfe76520efcda02d848


You can throw it in a UFCS chain anywhere except (for some 
reason) after something that takes a delegate template parameter 
like map. For example:


auto reader = SocketReader(socket).joiner.jitRange.map!(byt 
= cast(char) byt);




D Classes Passed By Reference or Value?

2015-08-16 Thread Brandon Ragland via Digitalmars-d-learn
Hi All, I'm a bit confused as to how Classes in D are passed in 
arguments and returns.


Take this for example:

class MyClass{
int x = 2;
}

And then in app.d

ref MyClass doStuff(){
MyClass mc = new MyClass() // Heap allocation, using new
return mc;
}

The above fails, as escaping reference to local variable 
however, this was created using new Not understanding what 
the deal is, this should be a valid heap allocated object, and 
therefore, why can I not pass this by reference? I don't want 
this to be a local variable...


So this begs the question: Are Classes (Objects) passed by 
reference already?


-Brandon


Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Alex Parrill via Digitalmars-d-learn

On Sunday, 16 August 2015 at 22:31:02 UTC, Brandon Ragland wrote:
Hi All, I'm a bit confused as to how Classes in D are passed in 
arguments and returns.


Take this for example:

class MyClass{
int x = 2;
}

And then in app.d

ref MyClass doStuff(){
MyClass mc = new MyClass() // Heap allocation, using new
return mc;
}

The above fails, as escaping reference to local variable 
however, this was created using new Not understanding what 
the deal is, this should be a valid heap allocated object, and 
therefore, why can I not pass this by reference? I don't want 
this to be a local variable...


So this begs the question: Are Classes (Objects) passed by 
reference already?


-Brandon


Classes are reference types, a la Java/C#.

class MyClass {
int x = 5;
}

void main() {
auto c1 = new MyClass();
auto c2 = c1;
c1.x = 123;
assert(c2.x == 123);
}



Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Alex Parrill via Digitalmars-d-learn

On Sunday, 16 August 2015 at 22:31:02 UTC, Brandon Ragland wrote:

ref MyClass doStuff(){
MyClass mc = new MyClass() // Heap allocation, using new
return mc;
}



This attempts to return a reference to the _variable_ `mc`, not a 
reference to the class. Just remove `ref` from the function 
signature.


Re: App Build Error

2015-08-16 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 16 August 2015 at 17:33:52 UTC, Benjamin wrote:


I'm having an issue with building my app - even a simple 
trivial app (shown below).


[...]


OS X version?
Have you configured your dmd.conf? iirc it requires linker path 
changes or something.

Have you looked in /usr/local/lib for libiconv.* ?
Have you any idea why it's targeting i386 and not, say i686 and 
not passing -m64 by default?
(You're getting the arch mismatch because you passed -m64 and dmd 
for some reason didn't)


Re: Visual Studio Code

2015-08-16 Thread bitwise via Digitalmars-d

On Sunday, 16 August 2015 at 05:12:06 UTC, Joakim wrote:

On Saturday, 15 August 2015 at 18:04:20 UTC, bitwise wrote:
Just a side note, looking at the main page of dlang.org, I 
don't see  any reference to who's using/contributing to D, or 
a link thereto.


I think it would help a lot of the logos of the D language's 
top sponsors could be seen somewhere on the main page. Maybe 
along the bottom as Proud D users or something. The top ~10 
sponsors could be chosen based of the dollar amounts or man 
hours contributed.


C++ has Gold members on their about page:
https://isocpp.org/about

Rust has a Team page:
https://www.rust-lang.org/team.html

Python has success stories:
https://www.python.org/about/success/

I could probably find more, but suffice it to say, it's a 
common occurrence.


Heh, funny you mention this, as I have a tab open in my browser 
open to the dlang.org github to remind me to submit a PR for 
just such an about page.  However, those examples are not 
that great for D, as it has no foundation or levels of 
sponsorship like C++, no formal teams like Rust, and that 
python page is actually not very good, though certainly long.


At the very least, the logos of Facebook and Sociomantic could be 
displayed at the bottom of the page. I'm not sure who else would 
be included, but I don't think Walter and Andrei would have any 
trouble coming up with a decent size list. The point is, I 
believe there should be proof at a glance that D is doing well 
in several real world scenarios.


I was thinking a page to briefly recap the language's genesis, 
introduce the two BDFLs, and mention corporate and project 
successes, along with some quotes from prominent users.


I believe there is a place for this information, but my specific 
recommendation is to present meaningful proof of D's usefulness 
to potential users as soon and succinctly as possible.


Feel free to submit a PR with what you have in mind and we 
could write it together:


Whether it would ever actually be merged is a different 
question. ;)


Unfortunately, I am a little out of the loop with respect to who 
exactly is using D, but if Walter or Andrei agreed with this 
idea, doing the actual work would be trivial.


Anyways, not making demands here, just my 2 cents :)

Bit


Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn

On 08/16/2015 03:36 PM, cym13 wrote:

On Sunday, 16 August 2015 at 22:22:07 UTC, Ali Çehreli wrote:


// HERE:
// Error: function deneme.func @nogc function allocates
//a closure with the GC
@nogc auto func(uint[] arr, DelegateRef d)
{
return arr.map!(a = d.d(a));
}


Aren't you making another delegate in the map by using = that needs
to allocate because it uses 'd' which is out of its scope?


I did not see that at all. :) I've finally gotten it to work by stepping 
into template realm where the compiler is a master of attributes: :)


import std.stdio;
import std.range;
import std.algorithm;

struct Caller
{
this(uint context) {
_context = context;
}

// ADDED pure
pure uint method(uint value) {
return _context * value;
}

uint _context;
}

// Now the type of d is a template parameter
@nogc auto func(Func)(uint[] arr, Func d)
{
return arr.map!(d);
}

void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = c.method;

writeln(func(arr, d));
}

Prints:

  [2, 4, 6]

Ali



Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Brandon Ragland via Digitalmars-d-learn

On Sunday, 16 August 2015 at 22:35:15 UTC, Alex Parrill wrote:
On Sunday, 16 August 2015 at 22:31:02 UTC, Brandon Ragland 
wrote:
Hi All, I'm a bit confused as to how Classes in D are passed 
in arguments and returns.


Take this for example:

class MyClass{
int x = 2;
}

And then in app.d

ref MyClass doStuff(){
MyClass mc = new MyClass() // Heap allocation, using new
return mc;
}

The above fails, as escaping reference to local variable 
however, this was created using new Not understanding what 
the deal is, this should be a valid heap allocated object, and 
therefore, why can I not pass this by reference? I don't want 
this to be a local variable...


So this begs the question: Are Classes (Objects) passed by 
reference already?


-Brandon


Classes are reference types, a la Java/C#.

class MyClass {
int x = 5;
}

void main() {
auto c1 = new MyClass();
auto c2 = c1;
c1.x = 123;
assert(c2.x == 123);
}


Thanks,

That makes more sense. Though it does make the ref method 
signature unclear, as it only applies to literals at this point?


Would you still need the ref signature for method parameters for 
classes to avoid a copy? Such that I could work on the class 
itself, and not a copy.



//This is reference?
void doStuff(ref MyClass mc){
return;
}

or would this also be a valid reference type:

//This is a copy?
void doStuff(MyClass mc){
return;
}


Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Ali Çehreli via Digitalmars-d-learn

On 08/16/2015 04:13 PM, Brandon Ragland wrote:

 That makes more sense. Though it does make the ref method
 signature unclear, as it only applies to literals at this
 point?

As long as the returned object will be valid after the function leaves, 
it can be anything: one of the ref parameters, a module-level variable, etc.


 Would you still need the ref signature for method parameters
 for classes to avoid a copy? Such that I could work on the
 class itself, and not a copy.

Obviously, you meant the object itself.

 //This is reference?
 void doStuff(ref MyClass mc){
 return;
 }

Yes, that's a reference to a class variable. Since class variables are 
references anyway, unless intended, there is one too many level of 
indirection there. (Although, it is valid and it may exactly be what is 
needed.)


 or would this also be a valid reference type:

 //This is a copy?
 void doStuff(MyClass mc){
 return;
 }

That's the normal way of doing it. mc is class reference to an object 
that was presumably created somewhere else.


Ali



Re: Weird error message.

2015-08-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, August 16, 2015 21:32:08 Warwick via Digitalmars-d-learn wrote:
 Dont know what to make of this, I pretty much get it every other
 time I call rdmd. It'll alternate between running fine and then
 giving me this error...

 C:\Program Files (x86)\Notepad++rdmd J:\Code\statproc.d
 std.process.ProcessException@std\process.d(550): Failed to spawn
 new process (The process cannot access the file because
   it is being used by another process.)
 
 0x00434C59
 0x00426F47
 0x00403C9C
 0x00403D17
 0x0042F60B
 0x0042F521
 0x0042258C
 0x76B533CA in BaseThreadInitThunk
 0x775A9ED2 in RtlInitializeExceptionChain
 0x775A9EA5 in RtlInitializeExceptionChain

 Any ideas?

Well, Windows has file locks, so tyically, if one program has a file open,
another can't have it open. So, I'd guess that either something has the file
open when you're running rdmd, so rdmd can't access it, or rdmd is trying to
access it from multiple processes and sometimes one of its processes is
accessing it when another one tries to. But I don't know if rdmd creates
multiple processes or not.

Personally, I wish that Windows didn't have file locks... :(

- Jonathan M Davis



Re: How to provide this arg or functor for algorithm?

2015-08-16 Thread cym13 via Digitalmars-d-learn

On Sunday, 16 August 2015 at 22:22:07 UTC, Ali Çehreli wrote:


// HERE:
// Error: function deneme.func @nogc function allocates
//a closure with the GC
@nogc auto func(uint[] arr, DelegateRef d)
{
return arr.map!(a = d.d(a));
}


Aren't you making another delegate in the map by using = that 
needs to allocate because it uses 'd' which is out of its scope?


Just updated to 2.068, get random profilegc.log created all over the place

2015-08-16 Thread deadalnix via Digitalmars-d
It looks like every run of whatever I compiled generate a 
profilegc.log file, that only contains :


bytes allocated, type, function, file:line

And that's it. Flag used to compile are -w -debug -gc -unittest . 
Bug or feature ?


Re: Just updated to 2.068, get random profilegc.log created all over the place

2015-08-16 Thread Walter Bright via Digitalmars-d

On 8/16/2015 4:16 PM, deadalnix wrote:

It looks like every run of whatever I compiled generate a profilegc.log file,
that only contains :

bytes allocated, type, function, file:line

And that's it. Flag used to compile are -w -debug -gc -unittest . Bug or 
feature ?


Bug. Please post to bugzilla as a regression.


Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Brandon Ragland via Digitalmars-d-learn

On Sunday, 16 August 2015 at 23:31:46 UTC, Ali Çehreli wrote:

On 08/16/2015 04:13 PM, Brandon Ragland wrote:

 That makes more sense. Though it does make the ref method
 signature unclear, as it only applies to literals at this
 point?

As long as the returned object will be valid after the function 
leaves, it can be anything: one of the ref parameters, a 
module-level variable, etc.


 Would you still need the ref signature for method parameters
 for classes to avoid a copy? Such that I could work on the
 class itself, and not a copy.

Obviously, you meant the object itself.

 //This is reference?
 void doStuff(ref MyClass mc){
 return;
 }

Yes, that's a reference to a class variable. Since class 
variables are references anyway, unless intended, there is one 
too many level of indirection there. (Although, it is valid and 
it may exactly be what is needed.)


 or would this also be a valid reference type:

 //This is a copy?
 void doStuff(MyClass mc){
 return;
 }

That's the normal way of doing it. mc is class reference to an 
object that was presumably created somewhere else.


Ali


If I understand you correctly than:

void doStuff(MyClass mc){
mc.x = 7;
}

Would be working with the reference to the object instantiated 
elsewhere. This would NOT be a copy of the object.


That would mean that (ref MyClass mc) is the equivalent to a 
pointer to a pointer (sorta, though these are references, same 
idea follows).


-Brandon


Re: D Classes Passed By Reference or Value?

2015-08-16 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 16 August 2015 at 23:40:41 UTC, Brandon Ragland wrote:

On Sunday, 16 August 2015 at 23:31:46 UTC, Ali Çehreli wrote:

On 08/16/2015 04:13 PM, Brandon Ragland wrote:

 That makes more sense. Though it does make the ref method
 signature unclear, as it only applies to literals at this
 point?

As long as the returned object will be valid after the 
function leaves, it can be anything: one of the ref 
parameters, a module-level variable, etc.


 Would you still need the ref signature for method parameters
 for classes to avoid a copy? Such that I could work on the
 class itself, and not a copy.

Obviously, you meant the object itself.

 //This is reference?
 void doStuff(ref MyClass mc){
 return;
 }

Yes, that's a reference to a class variable. Since class 
variables are references anyway, unless intended, there is one 
too many level of indirection there. (Although, it is valid 
and it may exactly be what is needed.)


 or would this also be a valid reference type:

 //This is a copy?
 void doStuff(MyClass mc){
 return;
 }

That's the normal way of doing it. mc is class reference to an 
object that was presumably created somewhere else.


Ali


If I understand you correctly than:

void doStuff(MyClass mc){
mc.x = 7;
}

Would be working with the reference to the object instantiated 
elsewhere. This would NOT be a copy of the object.


That would mean that (ref MyClass mc) is the equivalent to a 
pointer to a pointer (sorta, though these are references, same 
idea follows).


-Brandon


Yes.

void doStuff(ref MyClass a, ref MyClass b, ref MyClass c, bool d)
{
if (d)
b = a;
else
c = a;
}

void main(string[] args)
{
auto a = new MyClass(); a.x = 1;
auto b = new MyClass(); b.x = 2;
auto c = new MyClass(); c.x = 3;
assert(a !is b);
assert(a.x == 1);
assert(a !is c);
assert(b.x == 2);
assert(c !is b);
assert(c.x == 3);
doStuff(a,b,c,true);
assert(a is b);
assert(b.x == 1);
b.x = 2;
assert( a.x == 2);
assert(a !is c);
assert(c.x == 3);
doStuff(a,b,c,false);
assert(a is c);
assert(c.x == 2);
}


Pointers to Dynamic Arrays

2015-08-16 Thread Brandon Ragland via Digitalmars-d-learn

Howdy,

Since Dynamic Arrays / Slices are a D feature, using pointers to 
these has me a bit confused...


Consider:

string c2s(int* pos, char[]* file, int l){
char[] s;
for(int i = 0; i  l; i++){
s ~= file[(*pos + i)];
}
return s.dup;
}

Now what is especially confusing about this, is that the above 
seems to works fine, while this does not:


if(file[(*pos + i)] == '}'){
*pos += i;
return;
}

This fails with this error:
Error: incompatible types for ((file[cast(ulong)(*pos + i)]) == 
('}')): 'char[]' and 'char'


Now what I do not understand, is if the above works, by appending 
come chars gathered from the dynamic array via pointer to me new 
dynamic array named s, and the below does not seem to work on 
comparison of two chars, what is really going on?


I can no longer assume that using the dynamic array pointer works 
anything like a standard pointer to an array, or a pointer to a 
dynamic array.


Is there something I'm missing?

Remember, that de-referencing a dynamic array was deprecated. So 
what I would normally have done: dynamic_array_pntr does not 
work any longer, and there is no new spec on what to do...


-Brandon


pragma(mangle, on a template)

2015-08-16 Thread Freddy via Digitalmars-d-learn

I can't get pragma(mangle) to work on templates(or structs).

import std.stdio;

struct MyStruct(T...)
{
int var;
void func()
{
writeln(var);
}
}

pragma(mangle, MyAlias) alias MyAlias = MyStruct!(a, b, c 
/+very long symbol bloating list+/ );


void main()
{
auto v = MyAlias(2);
v.func();
}



Re: Pointers to Dynamic Arrays

2015-08-16 Thread Freddy via Digitalmars-d-learn

On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote:

if(file[(*pos + i)] == '}'){
*pos += i;
return;
}


That code doesn't do what you want it do. file is a ((char[])*) 
you are indexing the pointer(accessing invalid memory) and 
getting a char[].


Re: Pointers to Dynamic Arrays

2015-08-16 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote:

Howdy,

Since Dynamic Arrays / Slices are a D feature, using pointers 
to these has me a bit confused...


Consider:



Now what is especially confusing about this, is that the above 
seems to works fine, while this does not:


if(file[(*pos + i)] == '}'){
*pos += i;
return;
}

This fails with this error:
Error: incompatible types for ((file[cast(ulong)(*pos + i)]) == 
('}')): 'char[]' and 'char'



This is wrong. what you want is (*file)[(*pos + i)] == '}'
file is a pointer. deref it you get a char array. index that you 
get a char.
Now what I do not understand, is if the above works, by 
appending come chars gathered from the dynamic array via 
pointer to me new dynamic array named s, and the below does 
not seem to work on comparison of two chars, what is really 
going on?


I can no longer assume that using the dynamic array pointer 
works anything like a standard pointer to an array,


It is. arr.ptr[n] is the same as arr[n] (modulo bounds checking)


or a pointer to a dynamic array.

Is there something I'm missing?

Remember, that de-referencing a dynamic array was deprecated. 
So what I would normally have done: dynamic_array_pntr does 
not work any longer, and there is no new spec on what to do...


-Brandon



string c2s(int* pos, char[]* file, int l){
char[] s;
for(int i = 0; i  l; i++){
s ~= file[(*pos + i)];
}
return s.dup;
}



would more idiomatically be written as
string c2s(int* pos, char[]* file, int k)
{
char* p = *file.ptr+*pos;
return  p[0 .. k].dup
}

or changing the signature
string c2s(char[] file, size_t offset,size_t length)
{
return file[offset .. offset + length].dup;
}


Re: Pointers to Dynamic Arrays

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote:
Since Dynamic Arrays / Slices are a D feature, using pointers 
to these has me a bit confused...


Short answer: pointers to slices are usually a mistake, you 
probably don't actually want it, but rather should be using a 
regular slice instead.



string c2s(int* pos, char[]* file, int l){
char[] s;
for(int i = 0; i  l; i++){
s ~= file[(*pos + i)];


Here, for example, you've accidentally escaped slice land and are 
unto unchecked pointer arithmetic.


Since file is declared char[]* instead of char[], indexing it 
works C style: it at the index as an offset from the base char[].


In other words, it works more as if you wrote `char** file` in C. 
(still not identically, a D slice is different than a C char*, 
but same idea).


The above will only really work fine for index 0. Anything else 
would be a wild pointer. If this didn't crash on you, you just 
got kinda lucky with the index.


The append compiles because dereferencing a `char[]*` yields a 
`char[]`, which D can append normally.



Now what is especially confusing about this, is that the above 
seems to works fine, while this does not:


if(file[(*pos + i)] == '}'){


This errors for the same reason the top one succeeded: what's 
pointed to by a char[]* is char[], not char. So you are trying to 
compare a string on the left hand side to an individual character 
on the right hand side.


In other words, what your error message told :)

I can no longer assume that using the dynamic array pointer 
works anything like a standard pointer to an array, or a 
pointer to a dynamic array.


char[] in D is like:

struct char_array {
size_t length;
char* ptr;
}

in C. Since there's already a pointer in there, you typically 
don't want the address of this struct, you just want to pass it 
right down by value and let the pointer be copied (it still 
points to the same actual data).


BTW you can access those .length and .ptr values in D:

char[] slice;
char* a = slice.ptr; // works

The only time you'd actually want a char[]* in D is if you need 
to write back to the original *variable* once you append or 
shrink it. (The contents are fine, they can be modified through 
the slice with no special effort.)



Bottom line again is char[] in D is like char* in C. So char[]* 
in D is more like char** in C.





Re: pragma(mangle, on a template)

2015-08-16 Thread Freddy via Digitalmars-d-learn

On Monday, 17 August 2015 at 03:14:16 UTC, Adam D. Ruppe wrote:

On Monday, 17 August 2015 at 02:46:02 UTC, Freddy wrote:
Mangling is done at a different level in the compiler than 
aliases, so I don't think this is intended to work.

Is there any way I can mangle a template struct then?


Re: pragma(mangle, on a template)

2015-08-16 Thread BBasile via Digitalmars-d-learn

On Monday, 17 August 2015 at 02:46:02 UTC, Freddy wrote:

I can't get pragma(mangle) to work on templates(or structs).
[...]


I don't know why but it looks like it only works on functions. 
Even if a struct is not a template the custom symbol mangle won't 
be handled:


---
import std.stdio;

pragma(mangle, a0) class MyClass{}
pragma(mangle, a1) struct MyStruct{}
pragma(mangle, a2) void body_func();
pragma(mangle, a3) struct MyStructh
{ pragma(mangle, a4) void foo(){}}

void main()
{
writeln(MyClass.mangleof);
writeln(MyStruct.mangleof);
writeln(body_func.mangleof);
writeln(MyStructh.mangleof);
writeln(MyStructh.foo.mangleof);
}
---

which outputs:

---
C13temp_019455687MyClass
S13temp_019455688MyStruct
a2
S13temp_019455689MyStructh
a4
---

'a4' being printed and not 'a3' is interesting BTW ;)
I think that the manual is not clear enough about this pragma:

http://dlang.org/pragma.html#mangle

Unless the spec. are more detailed this could be considered as a 
bug.




Re: Weird error message.

2015-08-16 Thread BBasile via Digitalmars-d-learn

On Monday, 17 August 2015 at 00:00:11 UTC, Jonathan M Davis wrote:
On Sunday, August 16, 2015 21:32:08 Warwick via 
Digitalmars-d-learn wrote:
Dont know what to make of this, I pretty much get it every 
other time I call rdmd. It'll alternate between running fine 
and then giving me this error...


C:\Program Files (x86)\Notepad++rdmd J:\Code\statproc.d
std.process.ProcessException@std\process.d(550): Failed to 
spawn

new process (The process cannot access the file because
  it is being used by another process.)
[...]
Any ideas?


Well, Windows has file locks, so tyically, if one program has a 
[...]

Personally, I wish that Windows didn't have file locks... :(


It's locked unless it's specified during the call to 
`CreateFile()` that the file can be shared for reading/writing 
(FILE_SHARE_READ / FILE_SHARE_WRITE).






Re: pragma(mangle, on a template)

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 August 2015 at 02:46:02 UTC, Freddy wrote:
pragma(mangle, MyAlias) alias MyAlias = MyStruct!(a, b, 
c /+very long symbol bloating list+/ );


Mangling is done at a different level in the compiler than 
aliases, so I don't think this is intended to work.


[Issue 14925] replaceInPlace fail compilation

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14925

bb.t...@gmx.com changed:

   What|Removed |Added

 CC||bb.t...@gmx.com
  Component|dmd |phobos

--


pragma(mangle) only works on functions

2015-08-16 Thread Freddy via Digitalmars-d
I felt this was important enough to big over to the D general 
chat.
Original 
Thread:http://forum.dlang.org/thread/zmmsodqrffvcdqidv...@forum.dlang.org


BBasile's Example:

On Monday, 17 August 2015 at 04:32:47 UTC, BBasile wrote:

On Monday, 17 August 2015 at 02:46:02 UTC, Freddy wrote:

I can't get pragma(mangle) to work on templates(or structs).
[...]


I don't know why but it looks like it only works on functions. 
Even if a struct is not a template the custom symbol mangle 
won't be handled:


---
import std.stdio;

pragma(mangle, a0) class MyClass{}
pragma(mangle, a1) struct MyStruct{}
pragma(mangle, a2) void body_func();
pragma(mangle, a3) struct MyStructh
{ pragma(mangle, a4) void foo(){}}

void main()
{
writeln(MyClass.mangleof);
writeln(MyStruct.mangleof);
writeln(body_func.mangleof);
writeln(MyStructh.mangleof);
writeln(MyStructh.foo.mangleof);
}
---

which outputs:

---
C13temp_019455687MyClass
S13temp_019455688MyStruct
a2
S13temp_019455689MyStructh
a4
---

'a4' being printed and not 'a3' is interesting BTW ;)
I think that the manual is not clear enough about this pragma:

http://dlang.org/pragma.html#mangle

Unless the spec. are more detailed this could be considered as 
a bug.





xcb-d - First release

2015-08-16 Thread Wild via Digitalmars-d-announce

Hi!
I've just release the first version of xcb-d.
It's a bind for xcb that is generated via d_client.py, a heavy 
modified c_client.py script from the libxcb repository. This file 
uses the xml files from the libxcb-proto repository.
With this approach it will be really easily updated to the lasted 
version of xcb when it is released.

Currently it's for xcb version 1.11.

I'm looking forward to some feedback if there is any.

DUB package: http://code.dlang.org/packages/xcb-d
Github: https://github.com/Vild/xcb-d

-Dan




[Issue 14925] replaceInPlace fail compilation

2015-08-16 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=14925

Florent GABRIEL ts...@free.fr changed:

   What|Removed |Added

   Severity|enhancement |normal

--


Re: std.data.json formal review

2015-08-16 Thread Walter Bright via Digitalmars-d

On 8/16/2015 5:34 AM, Sönke Ludwig wrote:

Am 16.08.2015 um 02:50 schrieb Walter Bright:

 if (isInputRange!R  is(Unqual!(ElementEncodingType!R) == char))

I'm not a fan of more names for trivia, the deluge of names has its own
costs.


Good, I'll use `if (isInputRange!R  (isSomeChar!(ElementEncodingType!R) ||
isIntegral!(ElementEncodingType!R))`. It's just used in number of places and
quite a bit more verbose (twice as long) and I guess a large number of
algorithms in Phobos accept char ranges, so that may actually warrant a name in
this case.


Except that there is no reason to support wchar, dchar, int, ubyte, or anything 
other than char. The idea is not to support something just because you can, but 
there should be an identifiable, real use case for it first. Has anyone ever 
seen Json data as ulongs? I haven't either.




The json parser will work fine without doing any validation at all. I've
been implementing string handling code in Phobos with the idea of doing
validation only if the algorithm requires it, and only for those parts
that require it.


Yes, and it won't do that if a char range is passed in. If the integral range
path gets removed there are basically two possibilities left, perform the
validation up-front (slower), or risk UTF exceptions in unrelated parts of the
code base. I don't see why we shouldn't take the opportunity for a full and fast
validation here. But I'll relay this to Andrei, it was his idea originally.


That argument could be used to justify validation in every single algorithm that 
deals with strings.




Why do both? Always return an input range. If the user wants a string,
he can pipe the input range to a string generator, such as .array

Convenience for one.


Back to the previous point, that means that every algorithm in Phobos
should have two versions, one that returns a range and the other a
string? All these variations will result in a combinatorical explosion.


This may be a factor of two, but not a combinatorial explosion.


We're already up to validate or not, to string or not, i.e. 4 combinations.



The other problem, of course, is that returning a string means the
algorithm has to decide how to allocate that string. As much as
possible, algorithms should not be making allocation decisions.


Granted, the fact that format() and to!() support input ranges (I didn't notice
that until now) makes the issue less important. But without those, it would
basically mean that almost all places that generate JSON strings would have to
import std.array and append .array. Nothing particularly bad if viewed in
isolation, but makes the language appear a lot less clean/more verbose if it
occurs often. It's also a stepping stone for language newcomers.


This has been argued before, and the problem is it applies to EVERY algorithm in 
Phobos, and winds up with a doubling of the number of functions to deal with it. 
I do not view this as clean.


D is going to be built around ranges as a fundamental way of coding. Users will 
need to learn something about them. Appending .array is not a big hill to climb.




There are output range and allocation based float-string conversions available,
but no input range based one. But well, using an internal buffer together with
formattedWrite would probably be a viable workaround...


I plan to fix that, so using a workaround in the meantime is appropriate.



Weird error message.

2015-08-16 Thread Warwick via Digitalmars-d-learn
Dont know what to make of this, I pretty much get it every other 
time I call rdmd. It'll alternate between running fine and then 
giving me this error...


C:\Program Files (x86)\Notepad++rdmd J:\Code\statproc.d
std.process.ProcessException@std\process.d(550): Failed to spawn 
new process (The process cannot access the file because

 it is being used by another process.)

0x00434C59
0x00426F47
0x00403C9C
0x00403D17
0x0042F60B
0x0042F521
0x0042258C
0x76B533CA in BaseThreadInitThunk
0x775A9ED2 in RtlInitializeExceptionChain
0x775A9EA5 in RtlInitializeExceptionChain

Any ideas?