digitalmars.D.learn - what's wrong with my class?
- Jonathan Villa (36/36) May 04 2016 ...
- Brian Schott (3/4) May 04 2016 I see that the types of `id_user` aren't necessarily the same
- Jonathan Villa (4/8) May 04 2016 Oh, they are indeed same (alias). I wrote uint because I didn't
- Brian Schott (18/27) May 04 2016 import std.conv:emplace;
- Brian Schott (11/12) May 04 2016 All right. D's type system is marking the `Session` constructor
- Jonathan Villa (6/18) May 04 2016 You're right! the error doesn't show anymore.
...
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\allocato
\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? xDDDD
What I'm doing wrong? :<
May 04 2016
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`.
May 04 2016
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: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:What I'm doing wrong? :<I see that the types of `id_user` aren't necessarily the same between `create` and `this`.
May 04 2016
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: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".On Wednesday, 4 May 2016 at 23:19:08 UTC, Jonathan Villa wrote: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:What I'm doing wrong? :<I see that the types of `id_user` aren't necessarily the same between `create` and `this`.
May 04 2016
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);`
May 04 2016
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: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 synchronizedWhat 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);`
May 04 2016









Brian Schott <briancschott gmail.com> 