Re: Does the CTFE engine reuse variables?

2017-04-08 Thread Stefan Koch via Digitalmars-d-learn

On Sunday, 9 April 2017 at 05:42:02 UTC, Jethro wrote:
Suppose one has a function that will be used in CTFE and it 
uses a lot of local variables. Does each call of the function 
end up allocating space for these without ever releasing them 
or are they reused? or used on the stack like normal?


Nothing get's reeused.



Re: Variable Arguments

2017-04-08 Thread rikki cattermole via Digitalmars-d-learn

On 09/04/2017 7:30 AM, Jethro wrote:

void foo(A...)(A a)
{
foreach(aa; a)
{
for(int i = 0; i < a.length; i++)
...
}
}

A can be strings or char, how can I easily deal with both? (e.g.,
a.length = 1 for a being a char... and also a[0] = a, so to speak).

That is, I want chars to be treated as strings of length 1, since I have
written my code to work with strings, no reason it shouldn't work with
chars. I realize we can't use the above notation but I can't get the
type of aa because D complains it is unknown at compile time. I could
use A[k] but it requires extra work.


A char and a string is no where near the same thing.
A char is a single byte, a string is a array which is made up of a 
pointer to a set of chars plus a length (size_t WORD size of cpu e.g. 
4/8 bytes).


You would need to wrap up that input char e.g. string s = 
cast(immutable)[c];


But here is what I would recommend:

void foo(char[] c...) {
string[] args;
foreach(v; c) {
args ~= cast(immutable)[c]
}
foo(args);
}

void foo(string[] s...) {
  // ...
}

This will remove the need for template specialization (or "implicit" 
support for e.g. wstring and dstring).


Variable Arguments

2017-04-08 Thread Jethro via Digitalmars-d-learn

void foo(A...)(A a)
{
foreach(aa; a)
{
for(int i = 0; i < a.length; i++)
...
}
}

A can be strings or char, how can I easily deal with both? (e.g., 
a.length = 1 for a being a char... and also a[0] = a, so to 
speak).


That is, I want chars to be treated as strings of length 1, since 
I have written my code to work with strings, no reason it 
shouldn't work with chars. I realize we can't use the above 
notation but I can't get the type of aa because D complains it is 
unknown at compile time. I could use A[k] but it requires extra 
work.





Re: Using template mixin, with or without mixin ?

2017-04-08 Thread Meta via Digitalmars-d-learn

On Saturday, 8 April 2017 at 22:37:18 UTC, ag0aep6g wrote:

On 04/08/2017 11:59 PM, Meta wrote:

enum a = 0;

template test1()
{
enum b1 = a; //Okay, a is in scope at the declaration site
//enum c = d1; Error: undefined identifier d1


This line works just fine, actually. There's really no 
difference between a normal template and a mixin template when 
you use it in a mixin.



}

mixin template test2()
{
enum b2 = a; //Okay, a is in scope at the declaration site
enum c = d1; //Okay, d1 is in scope at the *instantiation* 
site

//enum e = d2; Error: undefined identifier d2
}

void main()
{
enum d1 = 0; //<--d1 is declared here
mixin test1!();
mixin test2!(); //<--so it is in scope here
enum d2 = 0; //d2 was not declared before test2 was mixed 
in

 //so it is not in scope for test2
}


Hmm, you're right, but this is not how it is supposed to behave 
according to the documentation.


https://dlang.org/spec/template-mixin.html


Does the CTFE engine reuse variables?

2017-04-08 Thread Jethro via Digitalmars-d-learn
Suppose one has a function that will be used in CTFE and it uses 
a lot of local variables. Does each call of the function end up 
allocating space for these without ever releasing them or are 
they reused? or used on the stack like normal?




BinaryHeap crashes upon insertion if heapified with an array of length 1?

2017-04-08 Thread TheGag96 via Digitalmars-d-learn
I'm trying to use a binary heap initialized with one element. 
However, this always seems to cause a range violation for some 
reason. This small example will do it:


import std.stdio, std.container;

void main() {
  auto pq = heapify([5]);
  pq.insert(8);
}

...And it produces this error: https://pastebin.com/dyLNRz2W

Oddly enough, if I heapify an array with any other length than 1, 
I can insert as much as I want (that includes an empty array!). 
Is this a bug in Phobos or some odd expected behavior? Thanks 
guys!


Re: Time from timestamp?

2017-04-08 Thread timotheecour via Digitalmars-d-learn
Might I suggest that you simply define an enum for UnixEpoch 
that's a

SysTime. Then you can do whatever you want.


ping on this.


Re: Using template mixin, with or without mixin ?

2017-04-08 Thread ag0aep6g via Digitalmars-d-learn

On 04/08/2017 11:59 PM, Meta wrote:

enum a = 0;

template test1()
{
enum b1 = a; //Okay, a is in scope at the declaration site
//enum c = d1; Error: undefined identifier d1


This line works just fine, actually. There's really no difference 
between a normal template and a mixin template when you use it in a mixin.



}

mixin template test2()
{
enum b2 = a; //Okay, a is in scope at the declaration site
enum c = d1; //Okay, d1 is in scope at the *instantiation* site
//enum e = d2; Error: undefined identifier d2
}

void main()
{
enum d1 = 0; //<--d1 is declared here
mixin test1!();
mixin test2!(); //<--so it is in scope here
enum d2 = 0; //d2 was not declared before test2 was mixed in
 //so it is not in scope for test2
}




Re: Using template mixin, with or without mixin ?

2017-04-08 Thread Meta via Digitalmars-d-learn

On Saturday, 8 April 2017 at 09:47:07 UTC, biocyberman wrote:

On Friday, 7 April 2017 at 23:53:12 UTC, Ali Çehreli wrote:


The difference is that you can't use funcgen as a regular 
template:


funcgen!(void, void);

Error: template instance funcgen!(void, void) mixin templates 
are not regular templates


I think it's good practice to use 'mixin template' if it's 
intended to be so.


Ali


Thanks for a very concise answer.


In addition to Ali's answer, mixin templates do their symbol 
looking at the instantiation site, while regular templates do it 
at the declaration site. Example:


enum a = 0;

template test1()
{
enum b1 = a; //Okay, a is in scope at the declaration site
//enum c = d1; Error: undefined identifier d1
}

mixin template test2()
{
enum b2 = a; //Okay, a is in scope at the declaration site
enum c = d1; //Okay, d1 is in scope at the *instantiation* site
//enum e = d2; Error: undefined identifier d2
}

void main()
{
enum d1 = 0; //<--d1 is declared here
mixin test1!();
mixin test2!(); //<--so it is in scope here
enum d2 = 0; //d2 was not declared before test2 was mixed in
 //so it is not in scope for test2
}


Re: GNU Guile D language interop

2017-04-08 Thread bachmeier via Digitalmars-d-learn

On Saturday, 8 April 2017 at 21:29:58 UTC, new2d wrote:
Can someone experienced in D port the GNU Guile embedding 
tutorial over?


It would be great if Guile can be used to embed in D

Thank you

https://www.gnu.org/software/guile/docs/guile-tut/tutorial.html#Fundamentals


I have embedded Guile inside a D program. I don't know if I still 
have the code, and it's been a while since I did it. It was 
straightforward as I recall.


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread Ali Çehreli via Digitalmars-d-learn

On 04/08/2017 03:11 AM, biocyberman wrote:
> On Saturday, 8 April 2017 at 10:02:01 UTC, Mike Parker wrote:
>>
>> I would expect if you implement it as a function the compiler will
>> inline it. You can always use the pragma(inline, true) [1] with
>> -inline to verify.
>>
>> [1] https://dlang.org/spec/pragma.html#inline
>
> Thanks for mentioning pragma. However, anyway to do it with mixin? It's
> so cool so I want to do more stuffs with it :)

You can mixin declarations with a template but I don't see how it can 
help here. A string mixin would work but it's really ugly at the use site:


string roundUp(alias x)()
if (is (typeof(x) == uint)) {

import std.string : format;
return format(q{
--%1$s;
%1$s |= %1$s >>  1;
%1$s |= %1$s >>  2;
%1$s |= %1$s >>  4;
%1$s |= %1$s >>  8;
%1$s |= %1$s >> 16;
++%1$s;
}, x.stringof);
}

void main() {
uint i = 42;
mixin (roundUp!i);// <-- Ugly

assert(i == 64);
}

Compare that to the following natural syntax that a function provides:

void roundUp(ref uint x) {
// ...
}

void main() {
uint i = 42;
i.roundUp();// <-- Natural
}

Ali



-fPIC and 32-bit dmd.conf settings

2017-04-08 Thread Joseph Rushton Wakeling via Digitalmars-d-learn

Hello folks,

The default dmd.conf settings for 64-bit environments include the 
-fPIC flag (for good reason), but the settings for 32-bit 
environments do not.  Any particular reason for this?


Thanks & best wishes,

-- Joe


GNU Guile D language interop

2017-04-08 Thread new2d via Digitalmars-d-learn
Can someone experienced in D port the GNU Guile embedding 
tutorial over?


It would be great if Guile can be used to embed in D

Thank you

https://www.gnu.org/software/guile/docs/guile-tut/tutorial.html#Fundamentals


Re: Function names and lambdas

2017-04-08 Thread Jacob Carlborg via Digitalmars-d-learn

On 2017-04-07 23:05, Ali Çehreli wrote:


Main reason for D not supporting the name-to-pointer mapping? I don't
think so because as far as I know this has been the case since very
early on but UFCS came very much later.


More likely due to properties, i.e. calling functions without 
parentheses. It's more difficult to know if the function should be 
called or if the address of the function should be taken.


--
/Jacob Carlborg


Re: Is DMD breaking BigInt?

2017-04-08 Thread Meta via Digitalmars-d-learn

On Saturday, 8 April 2017 at 12:14:31 UTC, Russel Winder wrote:
On Fri, 2017-04-07 at 22:47 +, Meta via Digitalmars-d-learn 
wrote:

[…]

Do you have the -dip1000 switch enabled?


Not as far as I know. Why would I want to do that?


You wouldn't as the std lib doesn't work with it yet.


Re: Is DMD breaking BigInt?

2017-04-08 Thread ag0aep6g via Digitalmars-d-learn

On 04/07/2017 07:06 PM, Russel Winder via Digitalmars-d-learn wrote:

factorial.d(71,15): Error: template std.bigint.BigInt.__ctor cannot deduce 
function from argument types !()(string) immutable


On 04/08/2017 02:18 PM, Russel Winder via Digitalmars-d-learn wrote:

https://github.com/russel/Factorial/tree/master/D


So, factorial.d:71 is this:


tuple(30, immutable BigInt("26525285981219105863630848000")),


And the BigInt constructor you're trying to use there is this one [1]:


this(Range)(Range s) if (
isBidirectionalRange!Range &&
isSomeChar!(ElementType!Range) &&
!isInfinite!Range)


In order to construct an immutable object, the constructor must either 
have the `immutable` attribute, or it must be `pure` so that the object 
is known to be unique. The constructor being templated, I think `pure` 
should be inferred when possible. But when I add it explicitly, your 
code compiles, so the attribute is apparently not inferred. Looks like a 
compiler bug to me. I've filed an issue:

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

It works with LDC because their BigInt doesn't have the templated 
constructor (yet, I guess).



[1] 
https://github.com/dlang/phobos/blob/df82dea9caf4a01f00734001fae62cee38ee4f4e/std/bigint.d#L64-L67


Re: Is DMD breaking BigInt?

2017-04-08 Thread Johan Engelen via Digitalmars-d-learn

On Saturday, 8 April 2017 at 12:16:10 UTC, Russel Winder wrote:


Fedora Rawhide is now on LLVM 4.0 is that going to be a problem 
building LDC?


Of course not! ;-)

-Johan



Re: Is DMD breaking BigInt?

2017-04-08 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2017-04-07 at 20:29 +, Jack Stouffer via Digitalmars-d-
learn wrote:
> On Friday, 7 April 2017 at 17:06:31 UTC, Russel Winder wrote:
> > Simple Dub build of a Factorial example using Unit-Threaded for 
> > testing. Works fine with ldc2 breaks with dmd.
> 
> Can you post the code your using?

I thought I had given a URL to the code on GitHub, maybe not, so here
it is:

https://github.com/russel/Factorial/tree/master/D


-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: Is DMD breaking BigInt?

2017-04-08 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2017-04-07 at 20:38 +, David Nadlinger via Digitalmars-d-
learn wrote:
> […]
> 
> You might want to check with LDC from Git master first to see 
> whether it is in fact a 2.073-related problem. — David

Rats, I thought I'd got away from manually building LDC.

Fedora Rawhide is now on LLVM 4.0 is that going to be a problem
building LDC?

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: Is DMD breaking BigInt?

2017-04-08 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2017-04-07 at 22:47 +, Meta via Digitalmars-d-learn wrote:
> […]
> 
> Do you have the -dip1000 switch enabled?

Not as far as I know. Why would I want to do that?

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread biocyberman via Digitalmars-d-learn

On Saturday, 8 April 2017 at 11:24:02 UTC, Nicholas Wilson wrote:

The ':' means that it applies to everything that follows it, so 
while it doesn't matters in this example if you had

pragma( inline, true ):
int kroundup32( int x) { ... }

auto someVeryLargeFunction( Args args)
{
// ...
}

and then you used someVeryLargeFunction in a bunch of places 
then that would cause a lot of binary bloat.


That's big difference! Thank you for pointing this out for me.


if you want the the function to affect the variable use a 'ref' 
as in


 void kroundup32(T)(ref T x) {
 pragma(inline, true);
 --(x);
 (x)|=(x)>>1;
 (x)|=(x)>>2;
 (x)|=(x)>>4;
 (x)|=(x)>>8;
 (x)|=(x)>>16;
 return ++(x);
 }

 int main(){
   int num = 31;
   writeln("Before: ",num); // 31
   kroundup32(num);
   writeln("After: ", num);   //32
   return 0;
 }

is it a good idea? I would not think it is necessary.

As an aside the C version has parentheses around the "x" 
because it is a macro and it is substituted as text not 
symbolically, they are not needed in D.


This thing now is clear and settled while I try to navigate my 
mind around many new things. Really appreciate your help, Nicolas.





Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 8 April 2017 at 11:01:34 UTC, biocyberman wrote:

On Saturday, 8 April 2017 at 10:09:47 UTC, Mike Parker wrote:

T kroundup32(T)(T x) {
pragma(inline, true);
--(x);
(x)|=(x)>>1;
(x)|=(x)>>2;
(x)|=(x)>>4;
(x)|=(x)>>8;
(x)|=(x)>>16;
return ++(x);
}



I also came up with this:

import std.stdio;
pragma( inline, true ):
static int kroundup32( int x){
--(x);
writeln("X: ",x);
 (x)|=(x)>>1;
writeln("X: ",x);
 (x)|=(x)>>2;
writeln("X: ",x);
(x)|=(x)>>4;
writeln("X: ",x);
 (x)|=(x)>>8;
writeln("X: ",x);
 (x)|=(x)>>16;
writeln("X: ",x);
 ++(x);
writeln("X: ",x);

 return x;
}

int main(){
  int num = 31;
  num = kroundup32(num);
  writeln("Num:", num);
  return 0;
}

Is this way of using pragma the same as your way? I am still 
new to this so I want to understand more.


The ':' means that it applies to everything that follows it, so 
while it doesn't matters in this example if you had

pragma( inline, true ):
int kroundup32( int x) { ... }

auto someVeryLargeFunction( Args args)
{
// ...
}

and then you used someVeryLargeFunction in a bunch of places then 
that would cause a lot of binary bloat.


And is it a good idea to do manipulate 'num' directly so I can 
omit 'return' and avoid re-assigning statement? That's what C 
version does.


if you want the the function to affect the variable use a 'ref' 
as in


 void kroundup32(T)(ref T x) {
 pragma(inline, true);
 --(x);
 (x)|=(x)>>1;
 (x)|=(x)>>2;
 (x)|=(x)>>4;
 (x)|=(x)>>8;
 (x)|=(x)>>16;
 return ++(x);
 }

 int main(){
   int num = 31;
   writeln("Before: ",num); // 31
   kroundup32(num);
   writeln("After: ", num);   //32
   return 0;
 }

is it a good idea? I would not think it is necessary.

As an aside the C version has parentheses around the "x" because 
it is a macro and it is substituted as text not symbolically, 
they are not needed in D.


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread biocyberman via Digitalmars-d-learn

On Saturday, 8 April 2017 at 10:09:47 UTC, Mike Parker wrote:

T kroundup32(T)(T x) {
pragma(inline, true);
--(x);
(x)|=(x)>>1;
(x)|=(x)>>2;
(x)|=(x)>>4;
(x)|=(x)>>8;
(x)|=(x)>>16;
return ++(x);
}



I also came up with this:

import std.stdio;
pragma( inline, true ):
static int kroundup32( int x){
--(x);
writeln("X: ",x);
 (x)|=(x)>>1;
writeln("X: ",x);
 (x)|=(x)>>2;
writeln("X: ",x);
(x)|=(x)>>4;
writeln("X: ",x);
 (x)|=(x)>>8;
writeln("X: ",x);
 (x)|=(x)>>16;
writeln("X: ",x);
 ++(x);
writeln("X: ",x);

 return x;
}

int main(){
  int num = 31;
  num = kroundup32(num);
  writeln("Num:", num);
  return 0;
}

Is this way of using pragma the same as your way? I am still new 
to this so I want to understand more.


And is it a good idea to do manipulate 'num' directly so I can 
omit 'return' and avoid re-assigning statement? That's what C 
version does.


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread biocyberman via Digitalmars-d-learn

On Saturday, 8 April 2017 at 10:02:01 UTC, Mike Parker wrote:


I would expect if you implement it as a function the compiler 
will inline it. You can always use the pragma(inline, true) [1] 
with -inline to verify.


[1] https://dlang.org/spec/pragma.html#inline


Thanks for mentioning pragma. However, anyway to do it with 
mixin? It's so cool so I want to do more stuffs with it :)


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 8 April 2017 at 10:02:01 UTC, Mike Parker wrote:



I would expect if you implement it as a function the compiler 
will inline it. You can always use the pragma(inline, true) [1] 
with -inline to verify.


[1] https://dlang.org/spec/pragma.html#inline


This gives me no error, so it does inline it.

T kroundup32(T)(T x) {
pragma(inline, true);
--(x);
(x)|=(x)>>1;
(x)|=(x)>>2;
(x)|=(x)>>4;
(x)|=(x)>>8;
(x)|=(x)>>16;
return ++(x);
}


Re: Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 8 April 2017 at 09:53:47 UTC, biocyberman wrote:


What is the D mixin version equivalent to this macro:

#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, 
(x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))


The macro looks cryptic. What the macro does has been explained 
here: 
http://stackoverflow.com/questions/3384852/could-someone-help-explain-what-this-c-one-liner-does


But I still don't know how to convert that to D mixin. I would 
like 'mixin' instead of a function is to avoid function call 
overhead. Also because it is short, so I think a mixin is 
enough, not a 'template mixin'.


I would expect if you implement it as a function the compiler 
will inline it. You can always use the pragma(inline, true) [1] 
with -inline to verify.


[1] https://dlang.org/spec/pragma.html#inline


Convert this C macro kroundup32 to D mixin?

2017-04-08 Thread biocyberman via Digitalmars-d-learn


What is the D mixin version equivalent to this macro:

#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, 
(x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))


The macro looks cryptic. What the macro does has been explained 
here: 
http://stackoverflow.com/questions/3384852/could-someone-help-explain-what-this-c-one-liner-does


But I still don't know how to convert that to D mixin. I would 
like 'mixin' instead of a function is to avoid function call 
overhead. Also because it is short, so I think a mixin is enough, 
not a 'template mixin'.





Re: Using template mixin, with or without mixin ?

2017-04-08 Thread biocyberman via Digitalmars-d-learn

On Friday, 7 April 2017 at 23:53:12 UTC, Ali Çehreli wrote:


The difference is that you can't use funcgen as a regular 
template:


funcgen!(void, void);

Error: template instance funcgen!(void, void) mixin templates 
are not regular templates


I think it's good practice to use 'mixin template' if it's 
intended to be so.


Ali


Thanks for a very concise answer.



Re: pointer not aligned

2017-04-08 Thread Mogaki via Digitalmars-d-learn

On Sunday, 2 April 2017 at 19:00:30 UTC, Jon Degenhardt wrote:

On Friday, 31 March 2017 at 04:41:10 UTC, Joel wrote:

Linking...
ld: warning: pointer not aligned at address 0x10017A4C9 
(_D30TypeInfo_AxS3std4file8DirEntry6__initZ + 16 from 
.dub/build/application-debug-posix.osx-x86_64-dmd_2072-EFDCDF4D45F944F7A9B1AEA5C32F81ED/spellit.o)

...

and this goes on forever!


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


Add -L-w to DFLAGS in dmd.conf to suppress all warning messages 
of 'ld'