Does D has built-in stack structure?

2015-06-21 Thread Assembly via Digitalmars-d-learn
Does D has built-in stack structure (if so, which module?) or 
should I implement it myself?


type of a variable as a mixin-able string?

2015-06-21 Thread Timothee Cour via Digitalmars-d-learn
Suppose I have:

import std.range;
auto a=iota(complex_expr_returning_3());

I'd like to have a function/trait/template/compiler magic that takes
variable a and generates a string that can be mixed in to represent the
type of a. The difficulty is that typeid(a).to!string doesn't work for
Voldermort types:
typeid(a).to!string produces "std.range.iota!(int, int).iota.Result", which
is not very useful as it can't be mixed in a program, being a voldemort
type, eg:
mixin("std.range.iota!(int, int).iota.Result a;"); //compile error

However, the compiler could help reduce it to something as simple as
possible, eg:
"typeof(iota(int.init))"
here this would work:

mixin("typeof(iota(int.init)) a;"); //works

Is it possible to do that in a generic way, such that it hides as much as
possible the details of the expression (here, "complex_expr_returning_3()"
should be simplified to int.init)

Or is there another trick I could use to instantiate a variable with same
type as a?


NOTE:
typeof(a) a2;
works even though
http://www.drdobbs.com/cpp/voldemort-types-in-d/232901591?pgno=2 says it
won't work, "Sorry, that won't work, the compiler will not allow a
Voldemort Type to be instantiated outside of its scope "
(although that's doesn't help with my original problem)


Re: Exec function D from C++

2015-06-21 Thread Jacob Carlborg via Digitalmars-d-learn

On 21/06/15 16:09, Sean Campbell wrote:


extern(C++) void d_initialize() {
  Runtime.initialize();
}
extern(C++) void d_terminate()
{
  Runtime.terminate();
}


These two functions are not necessary. There are already functions in 
druntime which are supposed to be called from C/C++:


extern (C) bool rt_init();
extern (C) bool rt_term();

--
/Jacob Carlborg


Re: Exec function D from C++

2015-06-21 Thread Sean Campbell via Digitalmars-d-learn

On Sunday, 21 June 2015 at 13:12:03 UTC, MGW wrote:

Linux 32, dmd 2.067.0

I want to connect and execute function D from main() C++.

 d1.d 
import core.stdc.stdio;

extern (C++) void d1() {
printf("printf - exec d1()");
}

 main_c1.cpp 
#include 

void d1(void);

int main(void) {
d1();
return 0;
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  --->  This option works correctly!


If I try to cause the functions connected to GC in the D 
function, there is an error.

 d1.d 
import import std.stdio;

extern (C++) void d1() {
writeln("writeln - exec d1()");
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  --->  Error:
'./main_c1' terminated by signal SIGSEGV (Address boundary 
error)


Examples from dmd2/html/d/dll-linux.html don't work if instead 
of printf()
to use writeln(). I think that a problem in initialization of 
Phobos.


actually its that druntime hasn't been initialized just add:
import core.runtime;
import std.stdio;

extern(C++) void d1() {
 writeln("writeln - exec d1()");
}

extern(C++) void d_initialize() {
 Runtime.initialize();
}
extern(C++) void d_terminate()
{
 Runtime.terminate();
}

and the C++
#include 

void d1(void);
void d_initialize(void);
void d_terminate(void);

int main(void) {
d_initialize();
d1();
d_terminate();
return 0;
}



Exec function D from C++

2015-06-21 Thread MGW via Digitalmars-d-learn

Linux 32, dmd 2.067.0

I want to connect and execute function D from main() C++.

 d1.d 
import core.stdc.stdio;

extern (C++) void d1() {
printf("printf - exec d1()");
}

 main_c1.cpp 
#include 

void d1(void);

int main(void) {
d1();
return 0;
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  --->  This option works correctly!


If I try to cause the functions connected to GC in the D 
function, there is an error.

 d1.d 
import import std.stdio;

extern (C++) void d1() {
writeln("writeln - exec d1()");
}

 compile 
dmd -c d1.d
gcc main_c1.cpp d1.o libphobos2.a -lpthread -o main_c1
./main_c1  --->  Error:
'./main_c1' terminated by signal SIGSEGV (Address boundary error)

Examples from dmd2/html/d/dll-linux.html don't work if instead of 
printf()
to use writeln(). I think that a problem in initialization of 
Phobos.


Re: Passing functions as template parameter and assigning default values to them

2015-06-21 Thread kerdemdemir via Digitalmars-d-learn

On Sunday, 21 June 2015 at 10:06:15 UTC, biozic wrote:
You can use a template alias parameter with a default value 
that is your default lambda:


int indexOfMax(alias fun = a => a, R)(R range)
{
// Use `fun` here like a function.
}

-- Nico


Thanks a lot, it works !!


Re: Passing functions as template parameter and assigning default values to them

2015-06-21 Thread biozic via Digitalmars-d-learn

On Sunday, 21 June 2015 at 09:34:51 UTC, kerdemdemir wrote:

Hi,

I need to find the index of maximum element so my code:

int indexOfMax(R)(R range)
{   
alias Type = typeof(range.front().re);  > I don't like 
.re here

Type max = 0;
size_t maxIndex = 0;
foreach ( index,elem; range )
{
if ( elem.re > max )-> And also here
{
max = elem.re;
maxIndex = index;
}
}
return maxIndex;
}

Since my range contains Complex!double types I have to add 
".re" for current implementation. But I want to be more 
generic. Like how std.algorithm.map is.

Like: range.map!(a => a.re).

So what I want to achive is :

indexOfMax!(a => a.re)(complexRange); > implement algorithm 
on a.re
indexOfMax(intRange); -> if a function is not given act 
like (a => a)


Any advice with template function parameters or mixins will 
make me happy.


Regards
Erdem


You can use a template alias parameter with a default value that 
is your default lambda:


int indexOfMax(alias fun = a => a, R)(R range)
{
// Use `fun` here like a function.
}

-- Nico



Passing functions as template parameter and assigning default values to them

2015-06-21 Thread kerdemdemir via Digitalmars-d-learn

Hi,

I need to find the index of maximum element so my code:

int indexOfMax(R)(R range)
{   
alias Type = typeof(range.front().re);  > I don't like 
.re here

Type max = 0;
size_t maxIndex = 0;
foreach ( index,elem; range )
{
if ( elem.re > max )-> And also here
{
max = elem.re;
maxIndex = index;
}
}
return maxIndex;
}

Since my range contains Complex!double types I have to add ".re" 
for current implementation. But I want to be more generic. Like 
how std.algorithm.map is.

Like: range.map!(a => a.re).

So what I want to achive is :

indexOfMax!(a => a.re)(complexRange); > implement algorithm 
on a.re
indexOfMax(intRange); -> if a function is not given act like 
(a => a)


Any advice with template function parameters or mixins will make 
me happy.


Regards
Erdem




Re: pass by value && elide dtor + post-blit

2015-06-21 Thread Namespace via Digitalmars-d-learn

Dear Ali,

thank you for helping! Problem happens when passing by value as 
in param.


Change 'foo' to this:

ref S foo(ref S s)
{
s.val+=1;
return s;
}




Re: pass by value && elide dtor + post-blit

2015-06-21 Thread Xiaoxi via Digitalmars-d-learn

On Saturday, 20 June 2015 at 22:44:17 UTC, Ali Çehreli wrote:

On 06/20/2015 02:09 PM, Xiaoxi wrote:
The output:

before
deneme.S.this
after
deneme.S.~this

Ali


Dear Ali,

thank you for helping! Problem happens when passing by value as 
in param.


DMD32 D Compiler v2.067.0
deneme.S.this
before
deneme.S.__postblit
deneme.S.__postblit
deneme.S.~this
deneme.S.~this
after
deneme.S.~this

import std.stdio;

struct S
{
int val;
this(int par)
{
val = par;
writeln(__FUNCTION__);
}

this(this)
{
writeln(__FUNCTION__);
}

~this()
{
writeln(__FUNCTION__);
}
}

S foo(S s)
{
s.val+=1;
return s;
}

void main()
{
auto s = S(42);
writeln("before");
foo(s);
writeln("after");
}