Re: Is it feasible to slowly rewrite a C++ codebase in D?

2018-07-13 Thread Vladimir Panteleev via Digitalmars-d-learn

On Friday, 13 July 2018 at 07:51:51 UTC, Dukc wrote:
I know about Vladimir's d-scripten tools library which would 
help, but it's based on Alawains library copyleft library, 
which makes also Vladimir's work copyleft, so I won't use it.


Hmm, I wasn't aware of this. I wonder if the decision to make the 
redistributable parts of dscripten was intentional (maybe the 
intention was to apply the license only to the scripts which 
build the toolchain).


I'll follow up with Alawain. Regardless, dscripten-tools borrows 
very little from the redistributable parts of dscripten - mostly 
the "minimalistic runtime", which I think was itself borrowed 
from somewhere else.




Re: @safe - why does this compile?

2018-07-13 Thread Dukc via Digitalmars-d-learn

On Friday, 13 July 2018 at 13:52:27 UTC, Timoses wrote:
I suppose this is another good example of how casting can be 
dangerous?


E.g. also:

immutable int i = 3;
int* j = cast(int*)
assert(i == 3);
*j = 4;
assert(j == ); // data occupies same address space
assert(i == 3 && *j == 4); // yet the values differ


No, casting classes to their subclasses is not dangerous to 
program integrity, because it is checked. It is just a regular 
bug that terminates the program when encountered.


But casting away immutable can break program integrity as your 
example demonstrates. For that reason the compiler won't let you 
do that if you wrap that code in @safe, unlike the class cast.


Re: Orange not working?

2018-07-13 Thread JN via Digitalmars-d-learn

On Friday, 13 July 2018 at 19:03:32 UTC, Jacob Carlborg wrote:

On 2018-07-13 20:52, Jacob Carlborg wrote:

Orange master is working properly. The tests are run on each 
push and PR with the latest DMD compiler.


I just added a cron job in Travis CI as well to make sure it 
every month even though there hasn't been a commit. This will 
make sure it always works with the latest compiler.


I'm curious, are the tests in any way OS specific? I see the 
tests are passing, but trying the latest DMD on Windows and 
orange v2.0.0, when I add "@nonSerialized" to a struct member, I 
get this:


C:\Users\jacek\Desktop\test_orange>dub run
Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe for 
x86.

orange 2.0.0: target for configuration "" is up to date.
test_orange ~master: building configuration "application"...
..\..\AppData\Local\dub\packages\orange-2.0.0\orange\orange\serialization\Serializer.d(1504,13):
 Warning: statement is not reachable
..\..\AppData\Local\dub\packages\orange-2.0.0\orange\orange\serialization\Serializer.d(1510,17):
 Warning: statement is not reachable
..\..\AppData\Local\dub\packages\orange-2.0.0\orange\orange\serialization\Serializer.d(1512,13):
 Warning: statement is not reachable
..\..\AppData\Local\dub\packages\orange-2.0.0\orange\orange\serialization\Serializer.d(1514,13):
 Warning: statement is not reachable
C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.


Re: Is it feasible to slowly rewrite a C++ codebase in D?

2018-07-13 Thread Laeeth Isharc via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 18:47:10 UTC, Jordi Gutiérrez 
Hermoso wrote:

I'm specifically thinking of the GNU Octave codebase:

http://hg.savannah.gnu.org/hgweb/octave/file/@

It's a fairly old and complicated C++ codebase. I would like to 
see if I could slowly introduce some D in it, anywhere.


Now, as I understand it, I would need to begin with making 
`main` a D function, because D needs to initialise the runtime. 
Is this correct?


Another possibility might be in dlopen'able functions. 
Currently Octave uses so-called oct functions, which are 
nothing more than C++ object code that is dynamically loaded by 
the interpreter at runtime. They are compiled to the Octave C++ 
API, but we also have a Matlab-compatible C API that perhaps 
could be more amenable for D-ification.


What are your ideas?


If you would like to expose C function and type declarations to 
D, you could take a look at DPP, which allows you to just 
#include a C header.  If you encounter a bug, please file an 
issue and in time we will fix it.


Does not yet work for C++ except in some cases.

https://github.com/atilaneves/dpp


Re: Check whether a range is empty

2018-07-13 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/13/18 3:29 PM, vino.B wrote:

On Friday, 13 July 2018 at 19:05:20 UTC, Steven Schveighoffer wrote:

On 7/13/18 2:37 PM, vino.B wrote:

Hi All,

   How do i check whether a range is empty. eg. 
(!PFResutl.toRange).empty. I tired the below, but it is no printing 
Empty if the range is empty it just prints blank line.


if (!(!PFResutl.toRange).empty) { writeln("Empty"); }



Without knowing what PFResutl is, let me simplify a bit:

if( ! (expr).empty) { writeln("Empty"); }

That exclamation point means "not". So you are first checking if the 
range is NOT empty, and if so, printing "Empty". Is that what you meant?




  Sorry there was a typo mistake, so the PFResult contain the results of 
"taskPool.workerLocalStorage" which i print using 
writeln(PFResult.toRange) so the requirement is that if the rage is 
empty it has to print "Empty" else it should print the result.



Eg:

  if (!(PFresult.toRange).empty) {
  foreach(i; chain(PFresult.toRange)) { writeln(i[]); }
  } else { writeln("Empty"); }



Well, empty is how you detect whether any range is empty, and as far as 
ranges are concerned, your code is correctly checking for empty.


A couple comments:

1. Why are you using chain with a single parameter? That just returns 
its parameter.

2. You may want to try grabbing the range ONCE. i.e.:

auto r = PFresult.toRange;
if(!r.empty)
{
   foreach(i; r) ...
}

I'm not familiar with how taskPool works, so I don't know the real answer.

-Steve


Re: Check whether a range is empty

2018-07-13 Thread vino.B via Digitalmars-d-learn
On Friday, 13 July 2018 at 19:05:20 UTC, Steven Schveighoffer 
wrote:

On 7/13/18 2:37 PM, vino.B wrote:

Hi All,

   How do i check whether a range is empty. eg. 
(!PFResutl.toRange).empty. I tired the below, but it is no 
printing Empty if the range is empty it just prints blank line.


if (!(!PFResutl.toRange).empty) { writeln("Empty"); }



Without knowing what PFResutl is, let me simplify a bit:

if( ! (expr).empty) { writeln("Empty"); }

That exclamation point means "not". So you are first checking 
if the range is NOT empty, and if so, printing "Empty". Is that 
what you meant?


-Steve


Hi Steve,

 Sorry there was a typo mistake, so the PFResult contain the 
results of "taskPool.workerLocalStorage" which i print using 
writeln(PFResult.toRange) so the requirement is that if the rage 
is empty it has to print "Empty" else it should print the result.



Eg:

 if (!(PFresult.toRange).empty) {
 foreach(i; chain(PFresult.toRange)) { writeln(i[]); }
 } else { writeln("Empty"); }

From,
Vino.B


Re: Check whether a range is empty

2018-07-13 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/13/18 2:37 PM, vino.B wrote:

Hi All,

   How do i check whether a range is empty. eg. 
(!PFResutl.toRange).empty. I tired the below, but it is no printing 
Empty if the range is empty it just prints blank line.


if (!(!PFResutl.toRange).empty) { writeln("Empty"); }



Without knowing what PFResutl is, let me simplify a bit:

if( ! (expr).empty) { writeln("Empty"); }

That exclamation point means "not". So you are first checking if the 
range is NOT empty, and if so, printing "Empty". Is that what you meant?


-Steve


Re: Orange not working?

2018-07-13 Thread Jacob Carlborg via Digitalmars-d-learn

On 2018-07-13 20:52, Jacob Carlborg wrote:

Orange master is working properly. The tests are run on each push and PR 
with the latest DMD compiler.


I just added a cron job in Travis CI as well to make sure it every month 
even though there hasn't been a commit. This will make sure it always 
works with the latest compiler.


--
/Jacob Carlborg


Re: Orange not working?

2018-07-13 Thread Jacob Carlborg via Digitalmars-d-learn

On 2018-07-13 14:59, Timoses wrote:


Huh, see this issue on github:
https://github.com/jacob-carlborg/orange/issues/39

which references to https://github.com/jacob-carlborg/mambo . Although 
that repository has last been updated in 2016 whereas Orange's was Oct. 
2017.


Yeah, Mambo was a dependency before but now it's not used anymore. See 
the readme for more details [1].



Went ahead and applied a few fixes for the warnings I encountered:
https://github.com/jacob-carlborg/orange/pull/50


Thanks for that.

[1] https://github.com/jacob-carlborg/orange#d-versions

--
/Jacob Carlborg


Re: Orange not working?

2018-07-13 Thread Jacob Carlborg via Digitalmars-d-learn

On 2018-07-12 22:44, JN wrote:
I am trying to make use of the Orange package, I added the latest 
version from dub to my project: "orange": "~>1.0.0" and copy pasted the 
"simple usage" code from https://github.com/jacob-carlborg/orange , but 
I am getting a long list of errors:


..\..\AppData\Local\dub\packages\orange-1.0.0\orange\orange\serialization\Serializer.d(714,21): 
C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.


Anyone has the library working?

my DMD version is:


Orange master is working properly. The tests are run on each push and PR 
with the latest DMD compiler. I pushed a new tag, v2.0.0, to make sure 
you can use a specific version as well. I picked a new major version 
because typedefs are not supported anymore.


--
/Jacob Carlborg


Check whether a range is empty

2018-07-13 Thread vino.B via Digitalmars-d-learn

Hi All,

  How do i check whether a range is empty. eg. 
(!PFResutl.toRange).empty. I tired the below, but it is no 
printing Empty if the range is empty it just prints blank line.


if (!(!PFResutl.toRange).empty) { writeln("Empty"); }


From,
Vino.B




Re: @safe - why does this compile?

2018-07-13 Thread ketmar via Digitalmars-d-learn

Steven Schveighoffer wrote:


To emphasize the point, this is @safe as well:

X2 x2; // = null
x2.run();

D does not consider a segmentation fault due to null dereferencing to be 
unsafe -- no memory corruption happens.


yeah. in simple words: safe code is *predictable*, but not "segfault-less". 
segfaults (null dereferences) in safe code are allowed, 'cause they have 
completely predictable behavior (instant program termination).


@safe doesn't free you from doing your null checks, it protects you from 
so-called "undefined behavior" (aka "unpredictable execution results"). so 
when we are talking about "memory safety", it doesn't mean that your code 
cannot segfault, it means that your code won't corrupt random memory due to 
misbehaving.


Re: @safe - why does this compile?

2018-07-13 Thread Steven Schveighoffer via Digitalmars-d-learn

On 7/13/18 7:22 AM, ketmar wrote:

Piotr Mitana wrote:


This code:

 import std.stdio;

 class X1 {}
 class X2 : X1
 {
void run() @safe
 {
 writeln("DONE");
 }
 }

 void main() @safe
 {
 X1 x1 = new X1;
 X2 x2 = cast(X2) x1;
 x2.run();
 }

is obviously wrong gets killed by OS's signal. Why is it @safe? I 
thought @safe should prevent such errors as well.


there is nothing wrong here. dereferencing null reference is completely 
safe (in terms of result predictability).


To emphasize the point, this is @safe as well:

X2 x2; // = null
x2.run();

D does not consider a segmentation fault due to null dereferencing to be 
unsafe -- no memory corruption happens.


-Steve


Re: @safe - why does this compile?

2018-07-13 Thread Timoses via Digitalmars-d-learn

On Friday, 13 July 2018 at 11:04:40 UTC, Piotr Mitana wrote:

This code:

import std.stdio;

class X1 {}
class X2 : X1
{
void run() @safe
{
writeln("DONE");
}
}

void main() @safe
{
X1 x1 = new X1;
X2 x2 = cast(X2) x1;
x2.run();
}

is obviously wrong gets killed by OS's signal. Why is it @safe? 
I thought @safe should prevent such errors as well.


I suppose this is another good example of how casting can be 
dangerous?


E.g. also:

immutable int i = 3;
int* j = cast(int*)
assert(i == 3);
*j = 4;
assert(j == ); // data occupies same address space
assert(i == 3 && *j == 4); // yet the values differ


Re: Orange not working?

2018-07-13 Thread Timoses via Digitalmars-d-learn

On Friday, 13 July 2018 at 05:39:24 UTC, JN wrote:

On Friday, 13 July 2018 at 05:29:58 UTC, Timoses wrote:

On Thursday, 12 July 2018 at 20:44:43 UTC, JN wrote:
I am trying to make use of the Orange package, I added the 
latest version from dub to my project: "orange": "~>1.0.0" 
and copy pasted the "simple usage" code from 
https://github.com/jacob-carlborg/orange , but I am getting a 
long list of errors:


..\..\AppData\Local\dub\packages\orange-1.0.0\orange\orange\serialization\Serializer.d(714,21):
 Error: undefined identifier typedef
[...]


Hm, v1.0.0 is from 2016 and meanwhile typedef was deprecated
https://dlang.org/deprecate.html#typedef .

Perhaps use

"orange": "~master"

to use the latest code.


Looks better, but still doesn't compile:

C:\Users\jacek\Desktop\test_orange>dub run
[...]
test_orange ~master: building configuration "application"...
..\sertest\orange-master\orange\serialization\archives\Archive.d(1313,9): 
Warning: statement is not reachable
[...]
..\sertest\orange-master\orange\serialization\Serializer.d(1215,9): Warning: 
statement is not reachable
C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.


Huh, see this issue on github:
https://github.com/jacob-carlborg/orange/issues/39

which references to https://github.com/jacob-carlborg/mambo . 
Although that repository has last been updated in 2016 whereas 
Orange's was Oct. 2017.


Went ahead and applied a few fixes for the warnings I encountered:
https://github.com/jacob-carlborg/orange/pull/50


Re: @safe - why does this compile?

2018-07-13 Thread bauss via Digitalmars-d-learn

On Friday, 13 July 2018 at 11:04:40 UTC, Piotr Mitana wrote:

This code:

import std.stdio;

class X1 {}
class X2 : X1
{
void run() @safe
{
writeln("DONE");
}
}

void main() @safe
{
X1 x1 = new X1;
X2 x2 = cast(X2) x1;
x2.run();
}

is obviously wrong gets killed by OS's signal. Why is it @safe? 
I thought @safe should prevent such errors as well.


See: https://dlang.org/spec/function.html#function-safety


Re: UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

On Friday, 13 July 2018 at 11:17:32 UTC, Radu wrote:

On Friday, 13 July 2018 at 11:12:47 UTC, Michael wrote:

On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote:

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

[...]


Do you try to call member functions? UFCS only works with 
free functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


I'm not intentionally trying to call member functions, no. The 
functions are all static functions of a class, but the 
chaining of them using UFCS doesn't seem to work.


OK, static functions of a class are static *member* functions, 
they are not free functions.


Oh, really? Well that will explain it then. I was sure I had used 
this syntax before but perhaps not. Thanks for your help!


Re: @safe - why does this compile?

2018-07-13 Thread ketmar via Digitalmars-d-learn

Piotr Mitana wrote:


This code:

 import std.stdio;

 class X1 {}
 class X2 : X1
 {
void run() @safe
 {
 writeln("DONE");
 }
 }

 void main() @safe
 {
 X1 x1 = new X1;
 X2 x2 = cast(X2) x1;
 x2.run();
 }

is obviously wrong gets killed by OS's signal. Why is it @safe? I thought 
@safe should prevent such errors as well.


there is nothing wrong here. dereferencing null reference is completely 
safe (in terms of result predictability).


Re: UFCS confusion

2018-07-13 Thread Radu via Digitalmars-d-learn

On Friday, 13 July 2018 at 11:12:47 UTC, Michael wrote:

On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote:

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

[...]


Do you try to call member functions? UFCS only works with free 
functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


I'm not intentionally trying to call member functions, no. The 
functions are all static functions of a class, but the chaining 
of them using UFCS doesn't seem to work.


OK, static functions of a class are static *member* functions, 
they are not free functions.


Re: UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote:

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

Hello,

I am nesting some function calls, and I'm pretty used to 
making use of D's Uniform Function Call Syntax, but I'm 
getting an error if I try to convert this line:



createSet(createVector(langSize, index)).length;


which works, into this line:


createVector(langSize, index).createSet.length;


receiving the error "Error: no property createSet for type 
int[]", when the signature for createSet is:



static auto createSet(in int[] vector) pure


What am I missing here? I'm sure it's stupidly obvious but it 
expects one argument, so I'm just calling it without 
parentheses.


Do you try to call member functions? UFCS only works with free 
functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


I'm not intentionally trying to call member functions, no. The 
functions are all static functions of a class, but the chaining 
of them using UFCS doesn't seem to work.


@safe - why does this compile?

2018-07-13 Thread Piotr Mitana via Digitalmars-d-learn

This code:

import std.stdio;

class X1 {}
class X2 : X1
{
void run() @safe
{
writeln("DONE");
}
}

void main() @safe
{
X1 x1 = new X1;
X2 x2 = cast(X2) x1;
x2.run();
}

is obviously wrong gets killed by OS's signal. Why is it @safe? I 
thought @safe should prevent such errors as well.


Re: UFCS confusion

2018-07-13 Thread Radu via Digitalmars-d-learn

On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote:

Hello,

I am nesting some function calls, and I'm pretty used to making 
use of D's Uniform Function Call Syntax, but I'm getting an 
error if I try to convert this line:



createSet(createVector(langSize, index)).length;


which works, into this line:


createVector(langSize, index).createSet.length;


receiving the error "Error: no property createSet for type 
int[]", when the signature for createSet is:



static auto createSet(in int[] vector) pure


What am I missing here? I'm sure it's stupidly obvious but it 
expects one argument, so I'm just calling it without 
parentheses.


Do you try to call member functions? UFCS only works with free 
functions, meaning declared at module level.


https://dlang.org/spec/function.html#pseudo-member


UFCS confusion

2018-07-13 Thread Michael via Digitalmars-d-learn

Hello,

I am nesting some function calls, and I'm pretty used to making 
use of D's Uniform Function Call Syntax, but I'm getting an error 
if I try to convert this line:



createSet(createVector(langSize, index)).length;


which works, into this line:


createVector(langSize, index).createSet.length;


receiving the error "Error: no property createSet for type 
int[]", when the signature for createSet is:



static auto createSet(in int[] vector) pure


What am I missing here? I'm sure it's stupidly obvious but it 
expects one argument, so I'm just calling it without parentheses.


Re: Is it feasible to slowly rewrite a C++ codebase in D?

2018-07-13 Thread Dukc via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 18:47:10 UTC, Jordi Gutiérrez 
Hermoso wrote:
Now, as I understand it, I would need to begin with making 
`main` a D function, because D needs to initialise the runtime. 
Is this correct?


No. Some initialization is required if you use the GC, as I 
understand it. But there's no rule it has to be done in a D main 
function, you can initialize it from anywhere you like.


And if you avoid using stuff that requires the runtime (I'm not 
sure what it constitutes in addition to GC. Perhaps classes, 
perhaps typeid, perhaps static constructors and destructors) you 
do not need to initialize it. -betterC style stuff can be done 
without it, and as I said it does not need the -betterC switch 
anymore, at least in LDC.


Re: Is it feasible to slowly rewrite a C++ codebase in D?

2018-07-13 Thread Dukc via Digitalmars-d-learn

On Thursday, 12 July 2018 at 15:12:51 UTC, Seb wrote:

it might also be feasible to simply use normal D.

Have you already tried this?


There's no strict distinction between using D normally and in 
systems programming fashion for me, because my main function 
isn't written in D.


But in practice it's systems-style code. I mostly use stdc suff 
which can conveniently link into libraries coming with 
Emscripten, and std.range and std.algorithm, which do not require 
instantiations. There are a few instantiated thing from DRuntime 
and Phobos too, at least slice copying and stuff from std.random. 
I do not use the GC, nor anything else which has required to call 
a global initializator.


I know about Vladimir's d-scripten tools library which would 
help, but it's based on Alawains library copyleft library, which 
makes also Vladimir's work copyleft, so I won't use it.