Re: Trying Multiple Aggregate reduce
On Monday, 31 March 2014 at 22:25:40 UTC, Nordlöw wrote: You can't use reduce with a const seed. This, surely, must be a compiler bug right? /Per Arguably, it's a user bug ;) The user should have provided a non-const seed. *But*, there have been many cases of reduce being made to accept const seeds before, via unqualified copy. This case must have just been a missed one. In any case, I had submitted a re-write for reduce, and it just so happens to support this. So I added your code to the test cases: https://github.com/D-Programming-Language/phobos/pull/2060
Re: Sockets between D and C(++) app
My bad; It returns immutable(char)*. Still won't work with send(); Am I right to supposed the receiving client must handle a ubyte[] as well (C++) ?
Re: Sockets between D and C(++) app
On Wednesday, 2 April 2014 at 00:34:08 UTC, bearophile wrote: char[] rep = "regan\0".dup; s.send(cast(ubyte[])rep); casts are dangerous, because they silently assume you know what you are doing. As first try I suggest you to remove every cast() from your D program, and replace them with to!() or other functions like toStringz. Sometimes this is not the most efficient thing to do, but it's safer, so it's better when you start to learn D. Bye, bearophile Thanks for your reply. As for cast, I seems not to have any option, because to! doesn't work as I would expect it. It would return me an array of numbers, instead of a string; So I kept the cast here, since I certainly know what it's doing -for now-. I generally use immutables, you caught me, here :-). As of main and return, I was not aware we could just ignore them if we didn't need them. I love explicit programming. However, I'm still stuck with toStringz(). Since it returns an immutable(char[]), I can't find how to pass it to Socket.send(), and I do not seem to be able to cast away the immutable :-s Alexandre
Re: Sockets between D and C(++) app
Alexandre L.: Some comments on your code: Here's my 'server' code: int main(string[] args) { If you don't need args, then I suggest to not put it as main argument. So probably this is better (note no int nor return, in D they are not needed): void main() { ... } int count = s.receiveFrom(recv_buf); It's better to use immutable here, and infer the type (use const/immutable everywhere you don't really need to mutate a variable/argument/field): immutable count = s.receiveFrom(recv_buf); char[] test = cast(char[])recv_buf[0..count-1]; // -1 for C string comp. writefln("Received: %s\n", test); char[] rep = "regan\0".dup; s.send(cast(ubyte[])rep); casts are dangerous, because they silently assume you know what you are doing. As first try I suggest you to remove every cast() from your D program, and replace them with to!() or other functions like toStringz. Sometimes this is not the most efficient thing to do, but it's safer, so it's better when you start to learn D. Bye, bearophile
Sockets between D and C(++) app
Hello, I lately did a minimal udp socket server-client application with C++, and I've been trying to recreate it with D, after. I'm able to get the client's request (C++ client) without too much trouble (after I figured I needed to get it in ubyte[]). Then I've tried to send the client an answer through send() (then tried sendTo() ) but none of them work. Here's my 'server' code: int main(string[] args) { auto s = new UdpSocket(); auto addr = new InternetAddress("127.0.0.1", ); s.setOption(SocketOptionLevel.IP, SocketOption.REUSEADDR, true); s.bind(addr); while (true) { ubyte[2048] recv_buf; int count = s.receiveFrom(recv_buf); char[] test = cast(char[])recv_buf[0..count-1]; // -1 for C string comp. writefln("Received: %s\n", test); char[] rep = "regan\0".dup; s.send(cast(ubyte[])rep); } writeln("End!"); return 0; } Then the client tries to get the string in a std::string, hence why I did input the \0 literal. I'm also fairly new to D, so any guidance is sure welcomed! Alexandre
Re: Why is SysTime.init invalid?
On Tuesday, 1 April 2014 at 10:54:41 UTC, Jonathan M Davis wrote: On Tuesday, April 01, 2014 05:35:28 ed wrote: OK, lazy me just read the std.satetime article again. It appears the design is for no invalid values and it is currently a known limitation due to CTFE. --- d_time_nan There is no equivalent. SysTime.init, which has a null TimeZone object, would be the closest, but once CTFE advances to the point that you can new up class objects with it, SysTime.init's timezone will be LocalTime, so don't rely on SysTime.init being invalid. std.datetime in general tries to avoid having any invalid states for any of its types. It's intended that creating such values be impossible. --- I would still like to know if there is a way around this so I can have my struct default init. You can certainly have a struct with a SysTime member, but there's no way for it to use that SysTime without first assigning a valid value to it - at least, not without segfaulting when it tries to use the SysTime in any context which it would use its timezone member. If you really want to have the SysTime be useable in the init value of your struct, then you can make it so that each of its member functions which use the SysTime member check it for validity first (or have a separate bool which indicates whether the SysTime has been assigned to or not), in which case, it could assign something like SysTime(0) to it, which would be the valid equivalent of SysTime.init. Unfortunately, that does add extra overhead, but there isn't any way around it if you want to have the SysTime be used without having the user of the struct assign to it first. It's pretty much the same boat that SysTime itself is in. I could make it so that it checks the timezone for null every time it's used and assign LocalTime to it if it's null, but that adds constant overhead. My decision was that it was better to just live with the fact that SysTime.init is invalid. It's debatable though, as it's a trade-off. - Jonathan M Davis I have changed my code slightly to what you suggest, assigning SysTime(0) if not initialised. It wasn't a big change and works fine. Thanks, ed
Re: How to dynamically call class virtual method dynamically
On Tuesday, 1 April 2014 at 19:52:47 UTC, Steven Schveighoffer wrote: On Tue, 01 Apr 2014 15:00:17 -0400, Frustrated wrote: On Tuesday, 1 April 2014 at 12:20:06 UTC, Steven Schveighoffer wrote: On Tue, 01 Apr 2014 03:31:41 -0400, Frustrated wrote: Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. Detecting whether bar exists can only happen at compile time, since an instance of A has no mechanism to detect whether it has bar. D does not have very good runtime introspection, that would have to be built into the TypeInfo struct (the mechanism exists to do it, but it has never been used for that). You could use this templates, but that would only work if the type of the derived class is known at compile time. This problem could easily be solved by virtual methods with default implementation. -Steve It seems logical to me that I should be able to achieve what I what. Suppose I have an object cast to it's interface: A a = new B; when I call a.This() it will call the method in the interface. Either This is a virtual method or a final method. Suppose it is a Final method since if it is virtual there is no problem. Now suppose B implements That as a virtual method that doesn't exist in A. Since a IS a B, That exists in it's vtable. I should be able to call it: a.That(); // Calls B's That(). There is no definition for B's vtable according to A. It just looks like an array of void pointers. In other words, there's no possible way, without knowing B's type structure, to know which entry in the vtable is 'That'. Of course this doesn't work directly because That() is not part of the interface. Regardless though, it still exists: (cast(B)a).That(); // Works Because you have (at runtime) determined that a actually IS a 'B'. But the only problem is that the cast(B) is required and is a trick to get the compiler to do what I want. It's not a "trick", it's a runtime check. It basically is saying "if a is actually a B, then call B.That, otherwise segfault" But we know that a is of type B. The compiler/runtime does not know that. e.g., typeof(cast(Object)a) returns B, right? You are thinking of typeid. But the runtime information does not contain any way to figure out which location 'That' is at. What you are really looking for is runtime introspection, similar to Java or C#. D has the capability, but it has not been implemented. We only have compile-time introspection. I could be mistaken but isn't `A a = new B` just a facade and a really is of type B? a is of type A, and it points at an instance of B. If so, isn't there a way to get the true type of a at runtime? If so, then can't we cast a to its true type at runtime and access its members properly? You can get at B's typeinfo, but that doesn't contain a way to call arbitrarily named functions. e.g., suppose truecast(a) returns a as the actual object that a was created as(in this case B, not A). Then truecast(a).That() would work. With the correct implementation of RTInfo inside object.di, you could possibly make this work. It would be kind of cool. It would be a TON of work to make this a reality. -Steve Here is a basic outline of a possible approach. I do not believe it is the best way import std.stdio, std.cstream; interface A { public final void opDispatch(string m)() { opDispatchImpl(m); } protected bool opDispatchImpl(string m); // void bar() { writeln("hello"); } // possible but name conflict } class B : A { void bar() { writeln("asdfasdf"); } override bool opDispatchImpl(string m) { if (m == "bar") bar(); return false; } } void main() { A a = new B; B b = new B; a.bar(); // Wow, A doesn't have a bar yet we call it?!?! b.bar(); din.getc(); } Note that a does not have a
Re: template interface and delegates
On Tue, 01 Apr 2014 15:47:42 -0400, anonymous wrote: Is this bug allready reported? or can somebody who has a deeper insight to this report it? I don't know. I think you should report it. If it's already reported, someone will close it as a "duplicate" -Steve
Re: How to dynamically call class virtual method dynamically
On Tue, 01 Apr 2014 15:00:17 -0400, Frustrated wrote: On Tuesday, 1 April 2014 at 12:20:06 UTC, Steven Schveighoffer wrote: On Tue, 01 Apr 2014 03:31:41 -0400, Frustrated wrote: Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. Detecting whether bar exists can only happen at compile time, since an instance of A has no mechanism to detect whether it has bar. D does not have very good runtime introspection, that would have to be built into the TypeInfo struct (the mechanism exists to do it, but it has never been used for that). You could use this templates, but that would only work if the type of the derived class is known at compile time. This problem could easily be solved by virtual methods with default implementation. -Steve It seems logical to me that I should be able to achieve what I what. Suppose I have an object cast to it's interface: A a = new B; when I call a.This() it will call the method in the interface. Either This is a virtual method or a final method. Suppose it is a Final method since if it is virtual there is no problem. Now suppose B implements That as a virtual method that doesn't exist in A. Since a IS a B, That exists in it's vtable. I should be able to call it: a.That(); // Calls B's That(). There is no definition for B's vtable according to A. It just looks like an array of void pointers. In other words, there's no possible way, without knowing B's type structure, to know which entry in the vtable is 'That'. Of course this doesn't work directly because That() is not part of the interface. Regardless though, it still exists: (cast(B)a).That(); // Works Because you have (at runtime) determined that a actually IS a 'B'. But the only problem is that the cast(B) is required and is a trick to get the compiler to do what I want. It's not a "trick", it's a runtime check. It basically is saying "if a is actually a B, then call B.That, otherwise segfault" But we know that a is of type B. The compiler/runtime does not know that. e.g., typeof(cast(Object)a) returns B, right? You are thinking of typeid. But the runtime information does not contain any way to figure out which location 'That' is at. What you are really looking for is runtime introspection, similar to Java or C#. D has the capability, but it has not been implemented. We only have compile-time introspection. I could be mistaken but isn't `A a = new B` just a facade and a really is of type B? a is of type A, and it points at an instance of B. If so, isn't there a way to get the true type of a at runtime? If so, then can't we cast a to its true type at runtime and access its members properly? You can get at B's typeinfo, but that doesn't contain a way to call arbitrarily named functions. e.g., suppose truecast(a) returns a as the actual object that a was created as(in this case B, not A). Then truecast(a).That() would work. With the correct implementation of RTInfo inside object.di, you could possibly make this work. It would be kind of cool. It would be a TON of work to make this a reality. -Steve
Re: template interface and delegates
Is this bug allready reported? or can somebody who has a deeper insight to this report it? On Tuesday, 1 April 2014 at 05:51:46 UTC, anonymous wrote: Ok, thought i did something wrong or got some wrong idea how it should work.
Re: How to dynamically call class virtual method dynamically
On Tuesday, 1 April 2014 at 19:00:18 UTC, Frustrated wrote: A a = new B; [...] Now suppose B implements That as a virtual method that doesn't exist in A. [...] e.g., typeof(cast(Object)a) returns B, right? No, it's Object. You're looking for typeid which returns a TypeInfo [1] which is runtime information about the type. For classes that's for the most derived one (here: B). I could be mistaken but isn't `A a = new B` just a facade and a really is of type B? If so, isn't there a way to get the true type of a at runtime? If so, then can't we cast a to its true type at runtime and access its members properly? Yes, all true. But you need the target type at compile time to cast. e.g., suppose truecast(a) returns a as the actual object that a was created as(in this case B, not A). Then truecast(a).That() would work. When different implementations of A have different Thats, they are not related in any way. And the compiler doesn't know about (possibly future) implementations when working on code that uses just the interface A. So, as far as I can see, truecast(a).That() would have to be a string lookup at runtime. And then the members' types would be dynamic, too, of course. I have no idea how one would go about that. My best guess is that all of D's type system would have to be duplicated at runtime. [1] http://dlang.org/phobos/object.html#TypeInfo
Re: How to dynamically call class virtual method dynamically
On Tuesday, 1 April 2014 at 12:20:06 UTC, Steven Schveighoffer wrote: On Tue, 01 Apr 2014 03:31:41 -0400, Frustrated wrote: Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. Detecting whether bar exists can only happen at compile time, since an instance of A has no mechanism to detect whether it has bar. D does not have very good runtime introspection, that would have to be built into the TypeInfo struct (the mechanism exists to do it, but it has never been used for that). You could use this templates, but that would only work if the type of the derived class is known at compile time. This problem could easily be solved by virtual methods with default implementation. -Steve It seems logical to me that I should be able to achieve what I what. Suppose I have an object cast to it's interface: A a = new B; when I call a.This() it will call the method in the interface. Either This is a virtual method or a final method. Suppose it is a Final method since if it is virtual there is no problem. Now suppose B implements That as a virtual method that doesn't exist in A. Since a IS a B, That exists in it's vtable. I should be able to call it: a.That(); // Calls B's That(). Of course this doesn't work directly because That() is not part of the interface. Regardless though, it still exists: (cast(B)a).That(); // Works But the only problem is that the cast(B) is required and is a trick to get the compiler to do what I want. But we know that a is of type B. e.g., typeof(cast(Object)a) returns B, right? Basically, it should be very easy for the compiler to internally cast a object to it's actual type(since that is contained in the object information, even if it's cast to something else) and call members on it. Of course, I'm not asking for an internal way. I could be mistaken but isn't `A a = new B` just a facade and a really is of type B? If so, isn't there a way to get the true type of a at runtime? If so, then can't we cast a to its true type at runtime and access its members properly? e.g., suppose truecast(a) returns a as the actual object that a was created as(in this case B, not A). Then truecast(a).That() would work. I'm sort of looking for the truecast function/template. Of course, if D doesn't store information about the actual type an object is inside it(which it doesn't AFAIK) then you can't truecast.
Re: How to foreach over a DList?
Just for reference, this is the compiling code: https://gist.github.com/Binero/f30e56351baf05f1a2ec
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 15:16:41 UTC, Meta wrote: On Tuesday, 1 April 2014 at 13:55:05 UTC, monarch_dodra wrote: On Tuesday, 1 April 2014 at 13:54:00 UTC, monarch_dodra wrote: In contrast, if you *pass* "1" to the DList, you lose that info, and the DList will complain that you are trying to assign an int to a ubyte. EDIT: The issue is actually one of template constraint, but it's essentially equivalent. Without context you can't assign an int to a ubyte. I thought that maybe VRP might work here (as you detailed in your other post), as it works elsewhere. Is is possible to extend VRP to cover this situation? *Maybe*, but it would require getting rid of the constraints. However, those template constraints are necessary to resolve the overloads to begin with ("insertBack(value)" vs "insertBack(range)"). So VRP wouldn't even get a chance to trigger :/
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 13:55:05 UTC, monarch_dodra wrote: On Tuesday, 1 April 2014 at 13:54:00 UTC, monarch_dodra wrote: In contrast, if you *pass* "1" to the DList, you lose that info, and the DList will complain that you are trying to assign an int to a ubyte. EDIT: The issue is actually one of template constraint, but it's essentially equivalent. Without context you can't assign an int to a ubyte. I thought that maybe VRP might work here (as you detailed in your other post), as it works elsewhere. Is is possible to extend VRP to cover this situation?
Re: How to foreach over a DList?
On Tue, Apr 01, 2014 at 06:04:48AM +, monarch_dodra wrote: > On Tuesday, 1 April 2014 at 04:43:44 UTC, H. S. Teoh wrote: > >On Mon, Mar 31, 2014 at 09:55:01PM +, monarch_dodra wrote: > >>On Monday, 31 March 2014 at 21:41:16 UTC, H. S. Teoh wrote: > >>>Argh, why is opSlice non-const? :-( Please file a bug. > >> > >>If opSlice was const, then you'd get a const slice, with const > >>reference. You wouldn't even be able to iterate on it. > >[...] > > > >Um... wat? > > I'm pointing out the fix is not as trivial as slapping "const" onto > the signature. It requires a very real investment in terms of > development. [...] Well, yes... I don't expect just adding 'const' to an existing function is going to magically make it work with const types. :-P Though I suppose the way I said it was ambiguous. My bad. T -- Those who don't understand Unix are condemned to reinvent it, poorly.
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 13:54:00 UTC, monarch_dodra wrote: In contrast, if you *pass* "1" to the DList, you lose that info, and the DList will complain that you are trying to assign an int to a ubyte. EDIT: The issue is actually one of template constraint, but it's essentially equivalent. Without context you can't assign an int to a ubyte.
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 13:30:21 UTC, Meta wrote: On Tuesday, 1 April 2014 at 12:30:03 UTC, monarch_dodra wrote: I fixed this not too long ago. Long story short, the "~=" implementations were made of fail. Just change those "~=" for "insertBack" and you should be fine. That aside, why is it necessary to cast 1, 2 and 3 to ubyte when adding them to the DList? Is this a problem with DList or with D? D has a "feature", where you can't assign something big into something small. EG, you can't assign an int to a byte. This avoids dangerous overflow. That said, this is *usually* transparent, because D *also* has value range analysis. So what this means is that when you write: "ubyte b = 1;" Even though "1" is actually an int, the compiler knows it is int he 0-255 range, so the assignment is fine. In contrast, if you *pass* "1" to the DList, you lose that info, and the DList will complain that you are trying to assign an int to a ubyte. For what it's worth, 2.066 introduces uniform initialization, so this will become valid, and is somewhat less intrusive: list1.insertBack(ubyte(1)); list1.insertBack(ubyte(2)); list1.insertBack(ubyte(3));
Re: How to foreach over a DList?
On Tue, 01 Apr 2014 09:30:18 -0400, Meta wrote: On Tuesday, 1 April 2014 at 12:30:03 UTC, monarch_dodra wrote: I fixed this not too long ago. Long story short, the "~=" implementations were made of fail. Just change those "~=" for "insertBack" and you should be fine. That aside, why is it necessary to cast 1, 2 and 3 to ubyte when adding them to the DList? Is this a problem with DList or with D? A limitation with D. The issue is that IFTI interprets numeric literals as type int. There simply is no way to say "if you get a numeric literal that fits within a ubyte, use ubyte as the type for T" See this bug report: http://d.puremagic.com/issues/show_bug.cgi?id=4998 -Steve
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 12:30:03 UTC, monarch_dodra wrote: I fixed this not too long ago. Long story short, the "~=" implementations were made of fail. Just change those "~=" for "insertBack" and you should be fine. That aside, why is it necessary to cast 1, 2 and 3 to ubyte when adding them to the DList? Is this a problem with DList or with D?
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 10:56:40 UTC, Ali Çehreli wrote: On 03/31/2014 10:32 PM, Jeroen Bollen wrote: Still no luck: import std.container; import std.stdio; void main() { DList!ubyte list1 = DList!ubyte(); list1 ~= cast(ubyte) 1; list1 ~= cast(ubyte) 2; list1 ~= cast(ubyte) 3; foreach(ubyte item; list1[]) { writeln(item); } } /usr/include/dlang/dmd/std/container.d(1874): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: undefined identifier rhs_, did you mean variable rhs? source/app.d(7): Error: template instance std.container.DList!ubyte.DList.opOpAssign!("~", ubyte) error instantiating source/app.d(7): Error: cannot append type ubyte to type DList!ubyte I think your problem is fixed in a more recent compiler. The code works with git head dmd but fails with e.g. 2.064. (Not tested with 2.065.) Ali I fixed this not too long ago. Long story short, the "~=" implementations were made of fail. Just change those "~=" for "insertBack" and you should be fine.
Re: How to dynamically call class virtual method dynamically
On Tue, 01 Apr 2014 03:31:41 -0400, Frustrated wrote: Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. Detecting whether bar exists can only happen at compile time, since an instance of A has no mechanism to detect whether it has bar. D does not have very good runtime introspection, that would have to be built into the TypeInfo struct (the mechanism exists to do it, but it has never been used for that). You could use this templates, but that would only work if the type of the derived class is known at compile time. This problem could easily be solved by virtual methods with default implementation. -Steve
Re: How to foreach over a DList?
John Colvin: doesn't work on 2.065 But it works in the latest alpha. Bye, bearophile
Re: How to foreach over a DList?
On Tuesday, 1 April 2014 at 10:56:40 UTC, Ali Çehreli wrote: On 03/31/2014 10:32 PM, Jeroen Bollen wrote: Still no luck: import std.container; import std.stdio; void main() { DList!ubyte list1 = DList!ubyte(); list1 ~= cast(ubyte) 1; list1 ~= cast(ubyte) 2; list1 ~= cast(ubyte) 3; foreach(ubyte item; list1[]) { writeln(item); } } /usr/include/dlang/dmd/std/container.d(1874): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: undefined identifier rhs_, did you mean variable rhs? source/app.d(7): Error: template instance std.container.DList!ubyte.DList.opOpAssign!("~", ubyte) error instantiating source/app.d(7): Error: cannot append type ubyte to type DList!ubyte I think your problem is fixed in a more recent compiler. The code works with git head dmd but fails with e.g. 2.064. (Not tested with 2.065.) Ali doesn't work on 2.065
Re: How to foreach over a DList?
On 03/31/2014 10:32 PM, Jeroen Bollen wrote: Still no luck: import std.container; import std.stdio; void main() { DList!ubyte list1 = DList!ubyte(); list1 ~= cast(ubyte) 1; list1 ~= cast(ubyte) 2; list1 ~= cast(ubyte) 3; foreach(ubyte item; list1[]) { writeln(item); } } /usr/include/dlang/dmd/std/container.d(1874): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: no property '_first' for type 'ubyte' /usr/include/dlang/dmd/std/container.d(1875): Error: undefined identifier rhs_, did you mean variable rhs? source/app.d(7): Error: template instance std.container.DList!ubyte.DList.opOpAssign!("~", ubyte) error instantiating source/app.d(7): Error: cannot append type ubyte to type DList!ubyte I think your problem is fixed in a more recent compiler. The code works with git head dmd but fails with e.g. 2.064. (Not tested with 2.065.) Ali
Re: Why is SysTime.init invalid?
On Tuesday, April 01, 2014 05:35:28 ed wrote: > OK, lazy me just read the std.satetime article again. It appears > the design is for no invalid values and it is currently a known > limitation due to CTFE. > > --- > d_time_nanThere is no equivalent. SysTime.init, which has a null > TimeZone object, would be the closest, but once CTFE advances to > the point that you can new up class objects with it, > SysTime.init's timezone will be LocalTime, so don't rely on > SysTime.init being invalid. std.datetime in general tries to > avoid having any invalid states for any of its types. It's > intended that creating such values be impossible. > --- > > I would still like to know if there is a way around this so I can > have my struct default init. You can certainly have a struct with a SysTime member, but there's no way for it to use that SysTime without first assigning a valid value to it - at least, not without segfaulting when it tries to use the SysTime in any context which it would use its timezone member. If you really want to have the SysTime be useable in the init value of your struct, then you can make it so that each of its member functions which use the SysTime member check it for validity first (or have a separate bool which indicates whether the SysTime has been assigned to or not), in which case, it could assign something like SysTime(0) to it, which would be the valid equivalent of SysTime.init. Unfortunately, that does add extra overhead, but there isn't any way around it if you want to have the SysTime be used without having the user of the struct assign to it first. It's pretty much the same boat that SysTime itself is in. I could make it so that it checks the timezone for null every time it's used and assign LocalTime to it if it's null, but that adds constant overhead. My decision was that it was better to just live with the fact that SysTime.init is invalid. It's debatable though, as it's a trade-off. - Jonathan M Davis
Re: How to dynamically call class virtual method dynamically
On Tuesday, 1 April 2014 at 09:12:41 UTC, John Colvin wrote: Also, bear in mind that polymorphism is one-way: you can call base class methods through an inherited class reference, but not the other way around. Sorry, I should clarify that: Given an inheritance tree with the base class class at the bottom, calls can always be made down the tree (inheriting class reference calling method in inherited class), but you can only call up the tree via overriden virtual functions (inherited class reference calling inheriting class functions). A new method that isn't present in the base class is by definition not overriding anything in the base class. Therefore you cannot call it from a base class reference, that would mean moving up the inheritance tree.
Re: How to dynamically call class virtual method dynamically
On Tuesday, 1 April 2014 at 07:31:43 UTC, Frustrated wrote: Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. I've tried using hasMember but since my object is cast to a type of Object it never works. The traditional OO approach would probably be this: import std.stdio; class A { static final void foo() { writeln("A.foo"); } void bar() { foo(); } } class B : A { override void bar() { writeln("B.bar"); } // optional } class C : A {} void main() { A a0 = new B; // or new C; A a1 = new C; // or new C; a0.bar(); //B.bar a1.bar(); //A.foo } Also, bear in mind that polymorphism is one-way: you can call base class methods through an inherited class reference, but not the other way around. interface A {} class B : A { void bar() {} } void main() { A a = new B; a.bar(); // Error: no property 'bar' for type 'A' }
How to dynamically call class virtual method dynamically
Basically in programming to interfaces I need to decide to call a virtual method of an object if it exists else call a final method in the interface: interface A { static final void foo() { ... } } class B : A { void bar() { ... } // optional } class C : B { void bar() { ... } // optional } void main() { A a = new B; // or new C; // if a.bar exists call it, else call foo // code should work independent of the classes. (there might be more) } The point of the code is simply to allow the class to implement bar optionally but provide default behavior with foo. I need a way to dynamically determine if bar exists and fall back on foo. This should be possible. e.g., suppose class B : A { } then I would like to b.bar() to actually call A.foo() (since bar doesn't exist in b). I guess the exist way would be to create an opDispatch and have it call foo if bar is passed. This works great and does everything I need it to except requires adding the code in the class which I can't have. Also I'm not sure how it would work with virtual methods. I've tried using hasMember but since my object is cast to a type of Object it never works.