Re: problem with isnan
Dne 10.11.2016 v 17:41 Charles Hixson via Digitalmars-d-learn napsal(a): The line: assert(isnan (c.curActivation), "cell has unexpected curActivation: %s".format(c.curActivation)); throws the exception: core.exception.AssertError@cell.d(285): cell has unexpected curActivation: nan and I've looked at it backwards and forwards and don't understand why. It's *supposed* to be nan, and the assert message reports that it is, but it should pass the assert test, not throw an assertion. What am I doing wrong? You need to use https://dlang.org/library/std/math/is_nan.html
Re: problem with isnan
On 11/10/2016 08:47 AM, Adam D. Ruppe via Digitalmars-d-learn wrote: On Thursday, 10 November 2016 at 16:41:56 UTC, Charles Hixson wrote: It's *supposed* to be nan, and the assert message reports that it is, but it should pass the assert test, not throw an assertion. What am I doing wrong? How did you set it? There are like billions of different NaNs. I'm not sure if isnan checks for all of them. (I'm also not sure that it doesn't, the docs don't specify.) you might try using std.math.isNaN instead and see what it does. It was default initialized by the class instance: classCell ... floatcurActivation; ... The this method doesn't have any mention of a few variables that are supposed to be default initialized, or which curActivation is one. I suppose I could set it to be -2.0 or something, but that doesn't really tell me what's going on.
Re: cross_module function overloading & alias & template: how to ?
On Thursday, 10 November 2016 at 20:12:10 UTC, Jonathan M Davis wrote: On Thursday, November 10, 2016 17:41:02 Picaud Vincent via Digitalmars-d- learn wrote: It is certainly a compiler problem: I used gdc -> compile error, but with dmd it compiles and runs fine. Full details in the git repo. Don't bother with gdc at this point. Unless there's a development version of it that's actually up-to-date and hasn't been released yet, it's too old to be reliable with regards to what correct D is. If something doesn't work in gdc, it could easily be because of a bug that has since been fixed. Unfortunately, the gdc folks have never managed to release an updated gdc that uses the D version of the compiler front-end, so as I understand it, they're still stuck at 2.067 (which is over 2 years old), whereas dmd is now at 2.072. ldc should be reasonably up-to-date if you want an alternative to dmd, and I expect that the gdc guys will get there eventually, but they haven't yet. - Jonathan M Davis I just have compiled my example with ldc and everything is ok! I was not aware of that concerning gdc. Thank you for this information, I lost around 2 hours struggling with initial code trying to guess what was wrong. Conclusion: now I will only use ldc and dmd. Thanks to Steven too for his clarification concerning template<->symbol Vincent
Re: Correct way to create singleton?
On Thursday, November 10, 2016 14:25:49 Steven Schveighoffer via Digitalmars-d-learn wrote: > There's a specific way to create a singleton that David Simcha outlined > in a talk at dconf 2013, that avoids all the race condition issues that > singletons traditionally have: > > https://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/ And what's particular cool about it from the perspective of promoting D is that it's _very_ easy to implement in D, whereas in most other languages, it involves using constructs that many folks don't even know exist, let alone know how to use (in particular, you need to use TLS). - Jonathan M Davis
Re: cross_module function overloading & alias & template: how to ?
On Thursday, November 10, 2016 17:41:02 Picaud Vincent via Digitalmars-d- learn wrote: > It is certainly a compiler problem: I used gdc -> compile error, > but with dmd it compiles and runs fine. Full details in the git > repo. Don't bother with gdc at this point. Unless there's a development version of it that's actually up-to-date and hasn't been released yet, it's too old to be reliable with regards to what correct D is. If something doesn't work in gdc, it could easily be because of a bug that has since been fixed. Unfortunately, the gdc folks have never managed to release an updated gdc that uses the D version of the compiler front-end, so as I understand it, they're still stuck at 2.067 (which is over 2 years old), whereas dmd is now at 2.072. ldc should be reasonably up-to-date if you want an alternative to dmd, and I expect that the gdc guys will get there eventually, but they haven't yet. - Jonathan M Davis
Re: cross_module function overloading & alias & template: how to ?
On Thursday, November 10, 2016 14:32:28 Steven Schveighoffer via Digitalmars-d-learn wrote: > On 11/10/16 12:12 PM, Jonathan M Davis via Digitalmars-d-learn wrote: > > On Thursday, November 10, 2016 15:46:11 Picaud Vincent via > > Digitalmars-d- > > > > learn wrote: > >> ---> What am I missing? What is the right way to do that? > > > > Honestly, I'm surprised that the compiler let you alias > > std.algorithm.comparison.min, because it's a templated function, and in > > the case of templates, you alias instantiations of templates, not the > > templates themselves. > > This statement is not true at all. You alias symbols. Templates are > symbols. Hmmm. I was sure that you couldn't, but I think that I was confusing myself over issues having to do with partial template instantiation. So, I stand corrected. - Jonathan M Davis
Re: cross_module function overloading & alias & template: how to ?
On 11/10/16 12:12 PM, Jonathan M Davis via Digitalmars-d-learn wrote: On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: ---> What am I missing? What is the right way to do that? Honestly, I'm surprised that the compiler let you alias std.algorithm.comparison.min, because it's a templated function, and in the case of templates, you alias instantiations of templates, not the templates themselves. This statement is not true at all. You alias symbols. Templates are symbols. -Steve
Re: Correct way to create singleton?
On 11/10/16 12:17 PM, Konstantin Kutsevalov wrote: Hi, what is a correct (and simple) way to create an singleton? This is how I see that now: ``` class ApMessageRouter { static ApMessageRouter instance = null; private this() { } // for disable constructor to use from outside public ApMessageRouter getInstance() { if (this.instance is null) { this.instance = new ApMessageRouter(); } return this.instance; } } ``` Thank you. There's a specific way to create a singleton that David Simcha outlined in a talk at dconf 2013, that avoids all the race condition issues that singletons traditionally have: https://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/ -Steve
Re: cross_module function overloading & alias & template: how to ?
On Thursday, 10 November 2016 at 17:12:32 UTC, Jonathan M Davis wrote: On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: [...] Honestly, I'm surprised that the compiler let you alias std.algorithm.comparison.min, because it's a templated function, and in the case of templates, you alias instantiations of templates, not the templates themselves. std.algorithm.comparison.min is just a template for a function, not an actual function. Something like std.algorithm.comparison.min!(int, int) would be an actual function. [...] Hi Jonathan, I just read your answer, thank you a lot Unfortunately I have not the time right now to answer (I am leaving my job it is 6:53PM). I will answer later. However I just created a github repo to reproduce my observations: https://github.com/vincent-picaud/DLang_overloading It is certainly a compiler problem: I used gdc -> compile error, but with dmd it compiles and runs fine. Full details in the git repo. More answers later... I have to leave
Re: Correct way to create singleton?
On Thursday, 10 November 2016 at 17:22:48 UTC, Anonymouse wrote: On Thursday, 10 November 2016 at 17:17:51 UTC, Konstantin Kutsevalov wrote: Hi, what is a correct (and simple) way to create an singleton? https://wiki.dlang.org/Low-Lock_Singleton_Pattern great!
Re: Correct way to create singleton?
On Thursday, 10 November 2016 at 17:17:51 UTC, Konstantin Kutsevalov wrote: Hi, what is a correct (and simple) way to create an singleton? https://wiki.dlang.org/Low-Lock_Singleton_Pattern
Correct way to create singleton?
Hi, what is a correct (and simple) way to create an singleton? This is how I see that now: ``` class ApMessageRouter { static ApMessageRouter instance = null; private this() { } // for disable constructor to use from outside public ApMessageRouter getInstance() { if (this.instance is null) { this.instance = new ApMessageRouter(); } return this.instance; } } ``` Thank you.
Re: Class copy
On Thursday, 10 November 2016 at 16:44:24 UTC, Satoshi wrote: But this implementation of dup doesn't work. The dup will need to be a virtual function in the base class for best results, reimplemented in each child class. You could also copy the bytes using runtime type info... void* data = cast(void *)typeid(this).create(); data[0 .. __traits(classInstanceSize, R)] = typeid(R).init[]; auto ret = cast(R)data; The first line there uses the runtime info, but the other two use the static type (this template parameters still use the static type just at the usage point instead of inside the class. So here, it is the base class since the static type of `Foo foo = new Bar;` is Foo.) Instead, you can change that second line to be something like: data[0 .. typeid(this).initializer.length] = (cast(void*)this)[0 .. typeid(this).initializer.length]; or something along those lines - the typeid(this) is the key factor instead of __traits so it uses the runtime info. The initializer.length should be the same as the class instance size. ... I think.
Re: cross_module function overloading & alias & template: how to ?
On Thursday, November 10, 2016 15:46:11 Picaud Vincent via Digitalmars-d- learn wrote: > ---> What am I missing? What is the right way to do that? Honestly, I'm surprised that the compiler let you alias std.algorithm.comparison.min, because it's a templated function, and in the case of templates, you alias instantiations of templates, not the templates themselves. std.algorithm.comparison.min is just a template for a function, not an actual function. Something like std.algorithm.comparison.min!(int, int) would be an actual function. But even if the compiler lets you alias std.algorithm.comparison.min, the only reason that the compiler then distinguishes between your overload of min and the one in std.algorithm is that yours is non-templated, and non-templated function overloads take precedence over templated overloads. Once they're both templates, the only way to distinguish is their template constraints and the number of arguments. T min(T)(T a, T b) { return a < b ? a : b; } has no template constraint. It matches every call to it where there are two arguments of the same type. Its internals may not compile with every type, but the overload resolution doesn't care about that. So, without a template constraint that distinguishes your min from std.algorithm's min, there's no way that it's going to work. And it's not actually _possible_ for you to declare a template constraint that makes your min match and not std.algorithm's min. For starters, you seem to be trying to pass it arguments which obviously work with std.algorith's min. So, by definition, you cannot possibly create something that overloads with it and does what you want. The only way that it would make sense to overload with std.algorithm's min would be if your min accepted arguments that std.algorithm's min didn't, and it doesn't sound like that's what you're trying to do. But perhaps more importantly if you actually look at std.algorithm.comparison.min's template constraint MinType!T min(T...)(T args) if (T.length >= 2) { ... } you'll see that it basically matches _everything_, pretty much like yours does. Its only restriction is that there have to be at least two arguments. It doesn't even have anything in the constraint to make sure that the arguments are comparable. It does that with a static assertion internally. So, the _only_ way to overload with std.algorithm's min is by declaring a non-templated function so that std.alorgithm's templated min function isn't even considered. If you want to have a min function that does something different and still be able to call std.algorithm's min, then you're going to need to wrap std.algorithm's min. e.g something like T min(T)(T a, T b) { static if(some condition that makes it want to use your min) { return a < b ? a : b; } else { import std.algorithm.comparison : min; return min(a, b); } } With a function like min though, I would think that it would be better to just use std.algorithm's min, and if you wanted to do something different with yours to just declare a function with a different name. After all, it's pretty standard what min does, so if yours is doing anything different, then it should probably have a different name, and if it does the same thing, then there's no point in having it. And if your goal is to simply mess around with D's overloading rules, you picked a very poor function to do that with, because std.algorithm.comparison.min matches basically everything. If you want to overload a function, then you really need to be picking one that has stuff that it doesn't accept that you can then declare a function that accepts. - Jonathan M Davis
Re: problem with isnan
On Thursday, 10 November 2016 at 16:41:56 UTC, Charles Hixson wrote: It's *supposed* to be nan, and the assert message reports that it is, but it should pass the assert test, not throw an assertion. What am I doing wrong? How did you set it? There are like billions of different NaNs. I'm not sure if isnan checks for all of them. (I'm also not sure that it doesn't, the docs don't specify.) you might try using std.math.isNaN instead and see what it does.
Class copy
Hello, how can I copy class when I have pointer to the parent of it? eg. class Foo { int m_a; this(const(Foo) other) { m_a = other.m_a; } } class Bar : Foo { int m_b; this(const(Bar) other) { super(other); m_b = other.m_b; } R dup(this R)() { void* data = cast(void *)typeid(this).create(); data[0 .. __traits(classInstanceSize, R)] = typeid(R).init[]; auto ret = cast(R)data; ret.__ctor(this); return ret; } } Foo foo = new Bar; Foo copyOfFoo = foo.dup; // ?? But this implementation of dup doesn't work. Thanks
problem with isnan
The line: assert(isnan (c.curActivation), "cell has unexpected curActivation: %s".format(c.curActivation)); throws the exception: core.exception.AssertError@cell.d(285): cell has unexpected curActivation: nan and I've looked at it backwards and forwards and don't understand why. It's *supposed* to be nan, and the assert message reports that it is, but it should pass the assert test, not throw an assertion. What am I doing wrong?
cross_module function overloading & alias & template: how to ?
Hi All, In my adventure to learn a little bit of D coming from C++ I am now faced with the following problem: I have read about "cross-module overloading", ยง5.5.2 page 146 of Andrei Alexandrescu book. That makes sense to me and this is interesting. As a concrete example here the scenario: I want to define a specialized version of the min function. In D knowing that if there is an ambiguity between modules the compiler generates an error, the following example does NOT work: == // I am the my_min.d file module my_min; int min(int a,int b) { return (a// my_min.min at source/my_min.d:7:5 conflicts with // std.algorithm.comparison.min!(int, int).min auto dc=min(4.5,b); // OK (no ambiguity) } == The D solution is to use the keyword "alias" : == // I am the my_min.d file module my_min; import std.algorithm.comparison; int min(int a,int b) { return (aalias std.algorithm.comparison.min min; // Here is the magic that imports // std min declaration into my_min module // and allows "global" overload resolution // to take place here, in my_min module == // I am the app.d file import my_min; // <- ATTENTION DO NOT RE-import std.algorithm.comparison HERE! void main() { int a=1,b=2; auto c=min(a,b); // OK! -> use my_min.min auto dc=min(4.5,b); // OK! -> use std.algorithm.comparison.min } == But now I have the following problem: if I use a parametrized function min: == module my_min; import std.algorithm.comparison; T min(T)(T a,T b) { return (aalias std.algorithm.comparison.min min; // <- Does NOT compile anymore // error: alias my_min.min conflicts with template my_min.min(T)(T a, T b) == ---> What am I missing? What is the right way to do that? Thank you :)
How to list aggregate members in order of declaration at compile time?
Hi. I want to somehow list members of a class in the order of their declaration. The immediate goal is to generate a few functions, like the "default" constructor for structs but only with all the fields, or the "reader" function, but I'm interested in the general question as well. I can go the hard way and wrap every declaration into something that will collect them and then list in the right order. The problem is, the declarations won't look normal then. The documentation for __traits (allMembers, ...), __traits (derivedMembers, ...) and the like [1] explicitly says that the order is not defined. Still, I've looked at Atila Neves' Cerealed serializer library, and it does use them [2]. Does it mean the order is unlikely to change at this point? The documentation for std.traits' RepresentationTypeTuple [3] says things are listed in topological order, which formally does not restrict the order for flat structures again. And the underlying implementation [4] of Fields uses .tupleof class property which, in turn, does not list any guarantees on the order [5]. So I'm confused. What is considered the right way to list members when I care about their linear order? Ivan Kazmenko. [1] https://dlang.org/spec/traits.html#derivedMembers [2] https://github.com/atilaneves/cerealed/blob/master/src/cerealed/cereal.d#L467 [3] https://dlang.org/phobos/std_traits.html#RepresentationTypeTuple [4] https://github.com/dlang/phobos/blob/10cd84a/std/traits.d#L2279 [5] https://dlang.org/spec/class.html#class_properties