Re: what's wrong with my class?

2016-05-04 Thread Jonathan Villa via Digitalmars-d-learn

On Thursday, 5 May 2016 at 00:03:34 UTC, Brian Schott wrote:

On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote:

What I'm doing wrong? :<


All right. D's type system is marking the `Session` constructor 
as `shared`. This makes the check `static if 
(is(typeof(result.__ctor(args` in std.conv.emplace fail 
because `result` is a non-shared `Session`. `emplace` is used 
by `make` to actually initialize the memory returned by 
`malloc`, which is why you care about it here. The solution to 
this is to tell `make` that you want it to return a `shared 
Session`. Once you do this the type checks should pass.


tldr: `return Mallocator.instance.make!(shared 
Session)(_parent, id_user);`


You're right! the error doesn't show anymore.
Just one question about 'shared':
That class has an ASI class, should I tag it as 'synchronized' 
too or it won't be necessary? (because is already inside in a 
synchronized class) I suppose I should tag it too as synchronized


Re: what's wrong with my class?

2016-05-04 Thread Brian Schott via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote:

What I'm doing wrong? :<


All right. D's type system is marking the `Session` constructor 
as `shared`. This makes the check `static if 
(is(typeof(result.__ctor(args` in std.conv.emplace fail 
because `result` is a non-shared `Session`. `emplace` is used by 
`make` to actually initialize the memory returned by `malloc`, 
which is why you care about it here. The solution to this is to 
tell `make` that you want it to return a `shared Session`. Once 
you do this the type checks should pass.


tldr: `return Mallocator.instance.make!(shared Session)(_parent, 
id_user);`





Re: what's wrong with my class?

2016-05-04 Thread Brian Schott via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 23:34:52 UTC, Jonathan Villa wrote:

On Wednesday, 4 May 2016 at 23:33:28 UTC, Brian Schott wrote:

On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote:

What I'm doing wrong? :<


I see that the types of `id_user` aren't necessarily the same 
between `create` and `this`.


Oh, they are indeed same (alias). I wrote uint because I didn't 
want you to confuse with such a large name and I forgot to 
change the other one c:


import std.conv:emplace;

synchronized class Session
{
this(uint u)
{
this.u = u;
}
private:
uint u;
}

void main(string[] args)
{
ubyte[128] mem;
Session s = emplace!Session(mem, 10);
}

Looks like the bug is actually with std.conv.emplace. It works if 
you remove "synchronized".




Re: what's wrong with my class?

2016-05-04 Thread Jonathan Villa via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 23:33:28 UTC, Brian Schott wrote:

On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote:

What I'm doing wrong? :<


I see that the types of `id_user` aren't necessarily the same 
between `create` and `this`.


Oh, they are indeed same (alias). I wrote uint because I didn't 
want you to confuse with such a large name and I forgot to change 
the other one c:


Re: what's wrong with my class?

2016-05-04 Thread Brian Schott via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote:

What I'm doing wrong? :<


I see that the types of `id_user` aren't necessarily the same 
between `create` and `this`.


what's wrong with my class?

2016-05-04 Thread Jonathan Villa via Digitalmars-d-learn

...
import std.experimental.allocator : make, dispose;
import std.experimental.allocator.mallocator : Mallocator;

public synchronized class Session
{
private:
ASI parent;
cUser user;
public:
static Session create(ASI _parent, 
accounts.user.id_user_t id_user) @nogc

{
return Mallocator.instance.make!Session(_parent, 
id_user);

}

this (ASI _parent, uint id_user)
{
assert(_parent !is null);
assert(id_user != 0);
parent = _parent;
user = Mallocator.instance.make!cUser(id_user, 
parent.stringManager);

}

~this ()
{
Mallocator.instance.dispose(user);
}
}

trying to build it throws "Don't know how to initialize an object 
of type Session with arguments (ASI, uint)

D:\Dmd\dmd2\windows\bin\..\..\src\phobos\std\experimental\allocator\package.d(455,48):
instantiated from here: emplace!(Session, ASI, uint)
source\alfred\memory.d(51,52):instantiated from here: 
make!(Session, shared(Mallocator), ASI, uint)

dmd failed with exit code 1."

Can't you see the constructor arguments? x
What I'm doing wrong? :<


template auto instantiation when parameters empty

2016-05-04 Thread Erik Smith via Digitalmars-d-learn
I want to have a struct template auto instantiate when the 
template parameters are defaulted or missing.  Example:


struct Resource(T=int) {
static auto create() {return Resource(null);}
this(string s) {}
}

auto resource = Resource.create;

As a plain struct it works, but not as a template:

struct Resource {   // works
struct Resource() {  // fails
struct Resource(T=int) {  // fails

At the call site, this works, but I'm hoping for a few less 
symbols:


auto resource = Resource!().create;

Any ideas?



Re: Strange stack variable corruption error after calling extern(C) function

2016-05-04 Thread cc via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 09:40:55 UTC, Benjamin Thaut wrote:

On Tuesday, 3 May 2016 at 19:06:30 UTC, cc wrote:


it fails to link with "Error 42: Symbol Undefined 
_FMOD_System_CreateSound@20".  With extern(C) it compiles and 
runs but the problem from above persists.


Is this on Windows x64? Try replacing FMOD_RESULT by int. When 
declaring the fmod create sound function and see if that helps.


The OS is Win64 though the program is being compiled as 32-bit 
and I'm using the 32-bit distributed DLL.

fmod.dll: PE32 executable (DLL) (GUI) Intel 80386, for MS Windows

Tried int and long as the return type, same issue both ways.  
Tried void too just in case, same thing though.


Re: Accepting function or delegate as function argument

2016-05-04 Thread Rene Zwanenburg via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote:
Two constructors, one accepting a function and the other one 
accepting a delegate would do the job for the API. Is there a 
simple method to convert a function pointer into a delegate 
pointer that is also efficient ?


http://dlang.org/phobos/std_functional.html#.toDelegate

You can also make your constructor a template, constrain it on 
isCallable if you wish, and then use toDelegate. If the argument 
is already a delegate toDelegate will avoid doing extra work.


Re: Accepting function or delegate as function argument

2016-05-04 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 14:54:39 UTC, chmike wrote:
Two constructors, one accepting a function and the other one 
accepting a delegate would do the job for the API. Is there a 
simple method to convert a function pointer into a delegate 
pointer that is also efficient ?


Do the overload and convert function to delegate with 
std.functional.toDelegate


http://dpldocs.info/experimental-docs/std.functional.toDelegate.html


stored_dg = toDelegate(passed_function);


Accepting function or delegate as function argument

2016-05-04 Thread chmike via Digitalmars-d-learn

I have implemented the following class (simplified ;) )


class Foo(K,T) {
this(T delegate (K) factory) { m_factory = factory; }
T delegate (K) m_factory;
T bar(K key) { return m_factory(key); }
}

string dummyFactory(string key) { return "Hello "~key; }

void main()
{
auto foo = new Foo!(string,string)(&dummyFactory);
writeln(foo.bar("world"));
}


The compiler complains that dummyFactory is a pointer to a 
function and not a delegate.
How can I modify the class definition so that it accepts a 
function or a delegate transparently from the user perspective ?


Two constructors, one accepting a function and the other one 
accepting a delegate would do the job for the API. Is there a 
simple method to convert a function pointer into a delegate 
pointer that is also efficient ?


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread chmike via Digitalmars-d-learn

Thank you Basile and Teoh.




Re: Code example for function/delegate as template argument ?

2016-05-04 Thread Basile B. via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 11:19:59 UTC, chmike wrote:

I think you misunderstood the second question.

Here is another attempt with an example.

// function accepting a function as argument
void foo(function void fg(int)) {
fg(5);
}

// A class with a none static method with the same signature as 
the argument function of foo

class Bar {
void fizz(int a) { writefln("Arg: %s", a); }
}

// An instance of class Bar
auto bar = new Bar;

// Calling foo by passing bar and the method fizz so that 
bar.fizz() is called when foo calls fg

foo( ??? );

Does the argument type need to be a delegate ?


It's much more simple then. You need

void foo(void delegate int() dg) {dg(5);}

and yes it must be a delegate.


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, May 04, 2016 at 06:21:36AM +, chmike via Digitalmars-d-learn wrote:
> Hello,
> 
> I failed to find some code example for a template class/struct that
> accept a function/delegate as template argument. All examples I could
> find use simple value types like int or double.

The usual way is to use an alias argument:

class MyClass(alias fun)
if (is(typeof(fun(0 // assuming fun takes 1 int argument
{
void method() {
fun(0);
}
}


> I piggy bag another question. Defining a function/delegate as function
> argument is shown in examples. What I could not find is how would I
> pass an object instance with a method to call ? In C++ we use
> std::bind. How do we do that in D ?

You probably need a delegate, since a function (== function pointer) in
D is a pointer to a (module-global) function with no context. If you
need a context, e.g., an object instance, you need a delegate. Example:

class C {
void method(int x) {
import std.stdio;
writeln("bing!");
}
}
void fun(void delegate(int) dg) {
dg(1);
}
void main() {
auto c = new C;
fun(&c.method);
}


T

-- 
Long, long ago, the ancient Chinese invented a device that lets them see 
through walls. It was called the "window".


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread Basile B. via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 10:58:04 UTC, chmike wrote:

On Wednesday, 4 May 2016 at 06:59:00 UTC, Basile B. wrote:

. . .
void main(string[] args)
{
alias fun = (a) => a.writeln;
auto foo = Foo!fun("hello");
}


Is this equivalent to Foo!(a => a.writeln) or is it required to 
split this in two instructions as you did ? I also thought the 
parenthesis around the lambda arguments are not required. Is 
that right ?


Yes this is equivalent, the parens are not required. With a 
delegate literal they are: "auto foo = 
Foo!((a){a.writeln;})("hello");


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread chmike via Digitalmars-d-learn

I think you misunderstood the second question.

Here is another attempt with an example.

// function accepting a function as argument
void foo(function void fg(int)) {
fg(5);
}

// A class with a none static method with the same signature as 
the argument function of foo

class Bar {
void fizz(int a) { writefln("Arg: %s", a); }
}

// An instance of class Bar
auto bar = new Bar;

// Calling foo by passing bar and the method fizz so that 
bar.fizz() is called when foo calls fg

foo( ??? );

Does the argument type need to be a delegate ?


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread chmike via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 06:59:00 UTC, Basile B. wrote:

. . .
void main(string[] args)
{
alias fun = (a) => a.writeln;
auto foo = Foo!fun("hello");
}


Is this equivalent to Foo!(a => a.writeln) or is it required to 
split this in two instructions as you did ? I also thought the 
parenthesis around the lambda arguments are not required. Is that 
right ?





Re: What can cause error: Previous Definition Different

2016-05-04 Thread Suliman via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 10:01:54 UTC, Suliman wrote:

After some minor modification of my code I begun to get error:
 Error 1: Previous Definition Different : 
_D3app14onlinetestdataFC4vibe4http6server17HTTPServerRequestC4vibe4http6server18HTTPServerResponseZv


Here is my code: 
https://gist.github.com/bubnenkoff/db0632bb14eebb7690b6563c47395831


Is there any ideas?


The error is cause because two different routers was link on same 
handler.


What can cause error: Previous Definition Different

2016-05-04 Thread Suliman via Digitalmars-d-learn

After some minor modification of my code I begun to get error:
 Error 1: Previous Definition Different : 
_D3app14onlinetestdataFC4vibe4http6server17HTTPServerRequestC4vibe4http6server18HTTPServerResponseZv


Here is my code: 
https://gist.github.com/bubnenkoff/db0632bb14eebb7690b6563c47395831


Is there any ideas?


Re: ubyte[] -> immutable(ubyte)[]

2016-05-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/3/16 5:31 PM, vino wrote:

On Friday, 10 September 2010 at 15:15:38 UTC, Andrej Mitrovic wrote:

Yeah, one would think the destination is on the left (just like the
standard C way of doing it), but it's not. I checked it in the docs
and the source. And idup works, thanks.

Kagamin Wrote:


Andrej Mitrovic Wrote:

> foreach (ubyte[] buffer; stdin.byChunk(bufferSize))
> {
> immutable(ubyte)[] copy_buffer;
> copy(buffer, copy_buffer);
> > writeln(copy_buffer);  // writes nothing
> > send(tid, copy_buffer);
> }

Isn't destination the left argument?
Why you don't use send(tid, buffer.idup);
?


Hi Andrei/All,

  Request your help, I am trying the program written by you with few
changes such as the example program read the input from stdin and prints
the data to stdout, but my program reads the input from the
file(readfile.txt) and writes the output to another file(writefile.txt),
and I am getting the below errors while compiling

Testing:
Read a file(readfile.txt : Contain 20,000 lines) by chunk into a
buffer(read buffer)
Pass the buffered data to an output buffer(write buffer) and then write
the output buffer to a to another file(writefile.txt).
Error:

[root@localhost DProjects]# dmd readwriteb.d
readwriteb.d(7): Error: cannot implicitly convert expression
(__aggr2859.front()) of type ubyte[] to immutable(ubyte)[]
readwriteb.d(15): Error: cannot implicitly convert expression
(receiveOnly()) of type immutable(ubyte)[] to std.outbuffer.OutBuffer
[root@localhost DProjects]#

Version: DMD64 D Compiler v2.071.0

Code:

import std.algorithm, std.concurrency, std.stdio, std.outbuffer, std.file;

void main() {
enum bufferSize = 1024 * 100;
auto file = File("readfile.txt", "r");
auto tid = spawn(&fileWriter);
foreach (immutable(ubyte)[] buffer; file.byChunk(bufferSize)) {
   send(tid, buffer);
}
}

void fileWriter() {
auto wbuf  = new OutBuffer();
for (;;) {
   wbuf = receiveOnly!(immutable(ubyte)[])();
   write("writefile.txt", wbuf);
}
}

After few changes as below I was able to compile, but when i run the
program there is no data in the writefile.txt

import std.algorithm, std.concurrency, std.stdio, std.file;

void main() {
enum bufferSize = 1024 * 100;
auto file = File("readfile.txt", "r");
auto tid = spawn(&fileWriter);
foreach (ubyte[] buffer; file.byChunk(bufferSize)) {
   send(tid, buffer.idup);
}
}

void fileWriter() {
auto file = File("writefile.txt", "w");
for (;;) {
  auto  wbuf = receiveOnly!(ubyte[])();


buffer.idup turns into immutable(ubyte)[]. You must receive this type:

auto wbuf = receiveOnly!(immutable(ubyte)[])();

What is likely happening is that this receiveOnly throws an exception 
you aren't catching, and then the program hangs. Try catching any 
errors/exceptions in your fileWriter thread function and print them out 
to see what is really happening.


-Steve


Re: Strange stack variable corruption error after calling extern(C) function

2016-05-04 Thread Benjamin Thaut via Digitalmars-d-learn

On Tuesday, 3 May 2016 at 19:06:30 UTC, cc wrote:


it fails to link with "Error 42: Symbol Undefined 
_FMOD_System_CreateSound@20".  With extern(C) it compiles and 
runs but the problem from above persists.


Is this on Windows x64? Try replacing FMOD_RESULT by int. When 
declaring the fmod create sound function and see if that helps.


Re: Stacktraces in static constructors

2016-05-04 Thread Benjamin Thaut via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 06:37:28 UTC, Nordlöw wrote:

On Tuesday, 3 May 2016 at 12:31:10 UTC, Benjamin Thaut wrote:

I assume this is on windows? Yes its a known issue (I know


No, the problem occurs on my Linux aswell.


From core.runtime:

static this()
{
// NOTE: Some module ctors will run before this handler is 
set, so it's
//   still possible the app could exit without a stack 
trace.  If
//   this becomes an issue, the handler could be set in C 
main

//   before the module ctors are run.
Runtime.traceHandler = &defaultTraceHandler;
}

So a possible workaround would be to either import core.runtime 
in each of your modules or manually run the line 
Runtime.traceHandler = &defaultTraceHandler.
Also I find it strange that the trace handler is initialized in a 
regular module constructor. In my eyes it should be a shared 
module ctor because the storage behind Runtime.traceHandler is 
__gshared anyway.


Kind Regards
Benjamin Thaut


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread Basile B. via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 06:21:36 UTC, chmike wrote:

Hello,

I failed to find some code example for a template class/struct 
that accept a function/delegate as template argument. All 
examples I could find use simple value types like int or double.



I piggy bag another question. Defining a function/delegate as 
function argument is shown in examples. What I could not find 
is how would I pass an object instance with a method to call ? 
In C++ we use std::bind. How do we do that in D ?


As for the second question, finding the right method dynamically 
(virtual method or interface) is easy so I suppose you want to 
find a member using D reflection.

Here is a quick example:


struct Bar
{
void fun(string text) {text.writeln;}
}

struct Foo
{
void delegate(string) dg;

this(T)(T t)
{
foreach(member; __traits(allMembers, T))
{
foreach(i, overload; __traits(getOverloads, T, 
member)[])

{
auto overloadDg = &__traits(getOverloads, t, 
member)[i];

static if (is(typeof(overloadDg) == typeof(dg)))
{
dg = &__traits(getOverloads, t, member)[i];
break;
}
}
}
if (dg)
dg("found");
}
}

void main(string[] args)
{
Bar bar;
Foo foo = Foo(bar);
}


More checkings are possible. Here I just verify that a pointer to 
a member function is of same type as the delegate to assign.


Re: Code example for function/delegate as template argument ?

2016-05-04 Thread Basile B. via Digitalmars-d-learn

On Wednesday, 4 May 2016 at 06:21:36 UTC, chmike wrote:

Hello,

I failed to find some code example for a template class/struct 
that accept a function/delegate as template argument. All 
examples I could find use simple value types like int or double.



I piggy bag another question. Defining a function/delegate as 
function argument is shown in examples. What I could not find 
is how would I pass an object instance with a method to call ? 
In C++ we use std::bind. How do we do that in D ?


Hello, you can use an alias this to pass a lmbda (or a delegate):


module runnable;

import std.stdio;

struct Foo(alias fun)
{
this(string text)
{
fun(text);
}
}

void main(string[] args)
{
alias fun = (a) => a.writeln;
auto foo = Foo!fun("hello");
}


I suppose that a constraint would be welcome be that's not the 
point here.