Re: Grokking std.container and Ranges
On Tue, 29 Jun 2010 07:16:13 +0200, BCS n...@anon.com wrote: Hello Mike, I want to do the following: foreach(obj; list) { if(obj.pleaseKillMe) somehow_remove_the_object_from_the_list(); } That isn't legal for normal arrays or AAs. IIRC the docs even say that you can't change what a foreach is iterating over during the foreach. I think you will have to convert to a manual for loop to make it work. That said, I've not worked with range at all. Perhaps keep the foreach and make a list of to be deleted objects and then delete them after the foreach.
@property and interfaces
Just wonder how to translate this C# snippet into D.. //C# public interface IBindingList { bool AllowEdit { get; } } //D2 interface IBindingList { @property bool allowEdit(); } Is this correct ? I think in C# AllowEdit() takes tare that you don't implement a setter. Seems to be impossible in D. Thanks Bjoern
Re: How do I make an extern function?
BCS n...@anon.com wrote: The issue is that the function called from module a is _D1a3fooFZv where the function defined in module b is _D1b3fooFZv ('a' - 'b') so they aren't the same function. extern(C) works because C doesn't mangle names so the function is foo in both cases. I know. I just react to 'extern void foo();' being treated as a function that must be in a. I would expect extern to indicate it lies elsewhere in the program. You can resolve this by having a a.di file with the extern foo(); in it (DMD has a flag to generate such a file for you). OTOH without knowing what you are doing, I can't tell if this is the correct solution. I'm trying to create a framework in which the user may provide his own foo( ), so the name of module b is impossible to know. torhu n...@spam.invalid wrote: You could always create a b.di file, if that doesn't defeat the purpose. Jonathan M Davis jmdavisp...@gmail.com wrote: Um, extern isn't needed in D. All you need to do is make the function public and then import the module. If only life were easy, eh? Module b is user-supplied, so I can't import it. -- Simen
Re: @property and interfaces
sorry for making so much noise.. figured it out by myse4lf. interface IBindingList { @property bool AllowEdit(); //@property bool AllowEdit(bool enable); //remove // to enable setter } class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } //bool AllowEdit(bool enable) { return _allowEdit = enable; } } } bjoern On 29/06/2010 12:11, BLS wrote: Just wonder how to translate this C# snippet into D.. //C# public interface IBindingList { bool AllowEdit { get; } } //D2 interface IBindingList { @property bool allowEdit(); } Is this correct ? I think in C# AllowEdit() takes tare that you don't implement a setter. Seems to be impossible in D. Thanks Bjoern
Re: @property and interfaces
On Tuesday 29 June 2010 03:11:34 BLS wrote: Just wonder how to translate this C# snippet into D.. //C# public interface IBindingList { bool AllowEdit { get; } } //D2 interface IBindingList { @property bool allowEdit(); } Is this correct ? I think in C# AllowEdit() takes tare that you don't implement a setter. Seems to be impossible in D. Thanks Bjoern Well, with the way that properties are implemented in D, I don't think that the getters and setters have any real relation with one another. If a getter is declared, you can use the property as an rvalue. If a setter is declared, you can use it as an lvalue. AFAIK, they don't really have any effect on each other and aren't really related. So, there certainly won't be any restriction on having a getter or setter in a class if an interface it implements declared only one. It's just that you'll only be able to use the one that's part of the interface if you're using a reference of the interface type rather than the implementing class. And really, I see no point in it being more restrictive. The interface itself should be properly restrictive if you use it since it only declares one of the two. And if you're using the class directly, then what does what the interface does and doesn't have matter? You're using the class at that point, not the interface. - Jonathan M Davis
Re: @property and interfaces
On 29/06/2010 12:32, Jonathan M Davis wrote: So, there certainly won't be any restriction on having a getter or setter in a class if an interface it implements declared only one. It's just that you'll only be able to use the one that's part of the interface if you're using a reference of the interface type rather than the implementing class. Hi Jonathan, interesting : this snippet compiles. interface IBindingList { @property bool AllowEdit(); class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } bool AllowEdit(bool enable) { return _allowEdit = enable; } } } But this one NOT. interface IBindingList { @property bool AllowEdit(); @property bool AllowEdit(bool enable); } class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } } } IMO this is bad design. bjoern
Re: How do I make an extern function?
On Tue, 29 Jun 2010 06:24:42 -0400, Simen kjaeraas simen.kja...@gmail.com wrote: BCS n...@anon.com wrote: The issue is that the function called from module a is _D1a3fooFZv where the function defined in module b is _D1b3fooFZv ('a' - 'b') so they aren't the same function. extern(C) works because C doesn't mangle names so the function is foo in both cases. I know. I just react to 'extern void foo();' being treated as a function that must be in a. I would expect extern to indicate it lies elsewhere in the program. D symbol names are mangled with their modules, so it is impossible for the compiler to know what module it will be in. The fact that it assumes the current module is probably very counterintuitive. Use extern(C) if you do not want mangled names. You can resolve this by having a a.di file with the extern foo(); in it (DMD has a flag to generate such a file for you). OTOH without knowing what you are doing, I can't tell if this is the correct solution. I'm trying to create a framework in which the user may provide his own foo( ), so the name of module b is impossible to know. Then the symbol is impossible to know. D symbols *must* mangle with their module names. Use extern(C) to avoid this. extern(C) functions work just as good as D functions, and for all intents and purposes, are equivalent. Just their names are not mangled, so you cannot have overloads. torhu n...@spam.invalid wrote: You could always create a b.di file, if that doesn't defeat the purpose. Jonathan M Davis jmdavisp...@gmail.com wrote: Um, extern isn't needed in D. All you need to do is make the function public and then import the module. If only life were easy, eh? Module b is user-supplied, so I can't import it. Have you thought of using interfaces? This is exactly why interfaces exist (to call functions without knowing who implemented them). What I would do is something like this: interface Foo { void foo1(int x); int foo1(string y); } extern extern(C) Foo getImplementation(); getImplementation().foo1(1); getImplementation().foo1(hi); Then your client lib has to define their implementation of Foo, and must define one extern (C) function called getImplementation. Or you could make getImplementation a @property: extern extern(C) @property Foo implementation(); implementation.foo1(1); implementation.foo1(hi); -Steve
Re: Grokking std.container and Ranges
On Mon, 28 Jun 2010 23:01:42 -0400, Mike Parker aldac...@gmail.com wrote: I thought I understood ranges until I actually started trying to use them. Now I'm having difficulties with the new range-based containers. So I've got two issues right now, grokking ranges and understanding the container interfaces. Given an SList, I want to do the following: foreach(obj; list) { if(obj.pleaseKillMe) somehow_remove_the_object_from_the_list(); } Part of my problem is I'm not entirely clear what's going on with the foreach. I know it's iterating a range, but is it the SList itself being iterated, or is it a range returned by SList.opSlice? I assume the latter, which at one point led me to try this (thinking of Java's iterator.remove()): auto r = list[]; foreach(obj; r) { if(obj.pleaseKillMe) r.popFront(); } Which, of course, didn't work. I see that popFront doesn't actually 'pop' anything off of, or out of, the range as it would in a traditional stack or a queue. The foreach doc says about range.popFront: move the left edge of the range right one Meaning, it's more like a next() than the pop() I'm familiar with (recalibrating all those years of C and Java terminology is not easy). And even if it did, changes to the range do not propagate to the underlying container. I understand that, at least (now). So apparently I want something like the list.stableRemove*() variants, which the docs say remove an item from a container without invalidating any ranges currently iterating the container. Great! Only, there's no variant that accepts a single item. SList has removeFront and removeAny, and ranges can be removed via linearRemove. I can insert individual items fine. But how do I remove them? linearRemove is it for SList. Sucks, but that's what's available, until Andrei can figure out a better way to remove a range. With a singly linked list, you can't remove the front of a range, because you have to alter the element *before* the front element. What SList needs is a function to allow removing all but the *first* element in the range. Something like removeTail. I think it would be an SList-specific function, not much use outside there. Anything you use will have to avoid foreach, removing elements from anything while using foreach on that thing is not a good idea (unless the foreach is doing it for you, see below). You have to manually handle the ranges. You may want to give dcollections a try. They support purging (the operation you are trying to write) natively with foreach via opApply. The linked list is dual-linked, so there are no issues with removing arbitrary elements (removal of a single element is an O(1) operation): LinkList!T list; ... foreach(ref doPurge, obj; list.purge) { doPurge = obj.pleaseKillMe; } http://www.dsource.org/projects/dcollections Please note, the online docs are D1 only, the D2 docs are included in the distribution, but aren't as pretty. -Steve
Re: @property and interfaces
On Tue, 29 Jun 2010 06:58:54 -0400, BLS windev...@hotmail.de wrote: On 29/06/2010 12:32, Jonathan M Davis wrote: So, there certainly won't be any restriction on having a getter or setter in a class if an interface it implements declared only one. It's just that you'll only be able to use the one that's part of the interface if you're using a reference of the interface type rather than the implementing class. Hi Jonathan, interesting : this snippet compiles. interface IBindingList { @property bool AllowEdit(); class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } bool AllowEdit(bool enable) { return _allowEdit = enable; } } } But this one NOT. interface IBindingList { @property bool AllowEdit(); @property bool AllowEdit(bool enable); } class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } } } IMO this is bad design. Classes are always able to add functionality beyond what the interface declares. IMO, it's actually bad design of C# not to allow you to declare setters even if the interface declares only a getter. If you access an A instance through the IBindingList, you only have access to a getter, so it is properly implementing the interface. I don't see why adding a setter detracts from it. What if you had this in C#? interface I1 { int x { get; } } interface I2 { int x {get; set;} } class C : I1, I2 // my C# is a bit rusty, I can't remember if this is how you implement interfaces { ??? } Besides, try to do this in C#: @property int value() {return _x;} @property int value(int x) { return _x = x;} @property int value(string s) { return _x = to!int(s);} :) D's properties are so much better... -Steve
Re: @property and interfaces
BLS: But this one NOT. interface IBindingList { @property bool AllowEdit(); @property bool AllowEdit(bool enable); } class A : IBindingList { private bool _allowEdit; @property { bool AllowEdit() { return _allowEdit; } } } IMO this is bad design. bjoern Is this good for you? interface IBindingList { @property bool AllowEdit(); @property bool AllowEdit(bool); } class A : IBindingList { bool _allowEdit; @property bool AllowEdit() { return _allowEdit; } @disable @property bool AllowEdit(bool) { return true; } } void main() {} Bye, bearophile
Re: @property and interfaces
On 29/06/2010 14:08, Steven Schveighoffer wrote: Besides, try to do this in C#: @property int value() {return _x;} @property int value(int x) { return _x = x;} @property int value(string s) { return _x = to!int(s);} :) D's properties are so much better... -Steve Ok, convinced ;)
Re: @property and interfaces
Hi bearophile, sorry for my ignorance, but what is the difference between @disable and simply deleting the line ? where can I read more about @disable ? thanks, bjoern
C# Indexers. how to implement them in D.. also property related.
Hi, in C# you can do some thing like this. public interface IDataErrorInfo { // INDEXER string this[string columnName] { get; } } } how to translate this into D2 ? thanks in advance, bjoern
Re: C# Indexers. how to implement them in D.. also property related.
On Tue, 29 Jun 2010 09:21:34 -0400, BLS windev...@hotmail.de wrote: Hi, in C# you can do some thing like this. public interface IDataErrorInfo { // INDEXER string this[string columnName] { get; } } } how to translate this into D2 ? thanks in advance, bjoern string opIndex(string columnName);
Re: C# Indexers. how to implement them in D.. also property related.
On 29/06/2010 15:27, Steven Schveighoffer wrote: string opIndex(string columnName); yeah this is what I did, too.. However defined as ; interface I1 { string opIndex(string columnName); } is a no go. So can we say operator overloading within interfaces is not allowed in D2 ? thanks again Steve.. try to learn the interface/property stuff.
Re: C# Indexers. how to implement them in D.. also property related.
BLS windev...@hotmail.de wrote: On 29/06/2010 15:27, Steven Schveighoffer wrote: string opIndex(string columnName); yeah this is what I did, too.. However defined as ; interface I1 { string opIndex(string columnName); } is a no go. Hm. That should have worked. So can we say operator overloading within interfaces is not allowed in D2 ? The new operator overloading scheme has problems with interfaces. A damned shame if you ask me. That said, opIndex does not use templates, and should thus work no problem. -- Simen
How to call receiveTimout? (std.concurrency)
Hello and Good morning! I'm trying to use receiveTimeout: // import std.stdio, std.concurrency; int main(string[] args){ receiveTimeout( 1000L, (int i){writefln(Received: %d,i);} ) ; return 0; } // (I removed all the surrounding code above that spawned threads etc.) Compiler gives me: /Library/Compilers/dmd2/osx/bin/../../src/phobos/std/concurrency.d(335): Error: mismatched tuple lengths, 2 and 1 I can't see what's wrong? Help! A look in concurrency.d shows: // - - 8 - - bool receiveTimeout(T...)( long ms, T ops ) { static enum long TICKS_PER_MILLI = 10_000; return mbox.get( ms * TICKS_PER_MILLI, ops ); } // - - 8 - - final void get(T...)( T ops ) { static assert( T.length ); static if( isImplicitlyConvertible!(T[0], long) ) { alias TypeTuple!(T[1 .. $]) Ops; assert( ops[0] = 0 ); long period = ops[0]; ops = ops[1 .. $]; // === line 335 } // - - 8 - - (DMD v2.047, OSX 10.6.4) BR /soul PS. Sorry if this is a dupe. Mailman doesn't seem to like my emails? Sending this via the web- interface..
Re: C# Indexers. how to implement them in D.. also property related.
interface I1 { string opIndex(string columnName); } is a no go. So can we say operator overloading within interfaces is not allowed in D2 ? It should work. Only opBinary etc doesn't work yet cause there are problems with template functions in interfaces.
Re: How to call receiveTimout? (std.concurrency)
Heywood Floyd soul...@gmail.com wrote: ops = ops[1 .. $]; // === line 335 Well, this looks like a bug to me. Should be Ops = ops[1 .. $]; -- Simen
Re: C# Indexers. how to implement them in D.. also property related.
On 29/06/2010 15:35, BLS wrote: On 29/06/2010 15:27, Steven Schveighoffer wrote: string opIndex(string columnName); yeah this is what I did, too.. However defined as ; interface I1 { string opIndex(string columnName); } is a no go. So can we say operator overloading within interfaces is not allowed in D2 ? thanks again Steve.. try to learn the interface/property stuff. Jeez, my mistake.. forget to create the implementation. sorry!
Re: How to call receiveTimout? (std.concurrency)
Simen kjaeraas simen.kja...@gmail.com wrote: Heywood Floyd soul...@gmail.com wrote: ops = ops[1 .. $]; // === line 335 Well, this looks like a bug to me. Should be Ops = ops[1 .. $]; Oh, and you could probably make this change yourself. -- Simen
Re: How do I make an extern function?
Hello Simen, BCS n...@anon.com wrote: You can resolve this by having a a.di file with the extern foo(); in it (DMD has a flag to generate such a file for you). OTOH without knowing what you are doing, I can't tell if this is the correct solution. I'm trying to create a framework in which the user may provide his own foo( ), so the name of module b is impossible to know. Several approaches: In addition to interfaces as Steven pointed out, if you don't need overloading you can use a function pointer. Also for either option, you could have a global variable that the users code sets from a static this: module a; void function(int) foo; // use foo -- moduel b; import a; void theFoo(int i) { ... } static this() { foo = theFoo; } If you want more encapsulation, you could switch to a registration function rather than having people muck around in the dirt. Also, if you have some appropriate object, you can put it there and avoid a global and all it entails. -- ... IXOYE
Re: C# Indexers. how to implement them in D.. also property related.
On Tue, 29 Jun 2010 09:55:50 -0400, BLS windev...@hotmail.de wrote: On 29/06/2010 15:35, BLS wrote: On 29/06/2010 15:27, Steven Schveighoffer wrote: string opIndex(string columnName); yeah this is what I did, too.. However defined as ; interface I1 { string opIndex(string columnName); } is a no go. So can we say operator overloading within interfaces is not allowed in D2 ? thanks again Steve.. try to learn the interface/property stuff. Jeez, my mistake.. forget to create the implementation. sorry! LOL! :) As others have said, templates do not yet work in interfaces, and there is another subtle problem: templates in interfaces are not virtual so they cannot enjoy covariance. This is a problem for dcollections which uses operators in interfaces. The only solution right now is to reimplement the template in the derived classes. Not a fun situation... -Steve
Re: How to call receiveTimout? (std.concurrency)
On Tue, 29 Jun 2010 09:53:25 -0400, Simen kjaeraas simen.kja...@gmail.com wrote: Heywood Floyd soul...@gmail.com wrote: ops = ops[1 .. $]; // === line 335 Well, this looks like a bug to me. Should be Ops = ops[1 .. $]; Ops is a type, isn't it? Don't you need a variable there? I agree it's a bug in the original, but I don't think that's the fix. -Steve
How to call receiveTimout? (std.concurrency)
Hello! I'm trying to use receiveTimeout: // import std.stdio, std.concurrency; int main(string[] args){ receiveTimeout( 1000L, (int i){writefln(Received: %d,i);} ) ; return 0; } // (I removed all the surrounding code above that spawned threads etc.) Compiler gives me: /Library/Compilers/dmd2/osx/bin/../../src/phobos/std/concurrency.d(335): Error: mismatched tuple lengths, 2 and 1 I can't see what's wrong? A look in concurrency.d shows: // - - 8 - - bool receiveTimeout(T...)( long ms, T ops ) { static enum long TICKS_PER_MILLI = 10_000; return mbox.get( ms * TICKS_PER_MILLI, ops ); } // - - 8 - - final void get(T...)( T ops ) { static assert( T.length ); static if( isImplicitlyConvertible!(T[0], long) ) { alias TypeTuple!(T[1 .. $]) Ops; assert( ops[0] = 0 ); long period = ops[0]; ops = ops[1 .. $]; // === line 335 } // - - 8 - - (DMD v2.047, OSX 10.6.4) BR /soul
Re: @property and interfaces
On Tue, 29 Jun 2010 14:42:33 +0200, BLS windev...@hotmail.de wrote: Hi bearophile, sorry for my ignorance, but what is the difference between @disable and simply deleting the line ? where can I read more about @disable ? thanks, bjoern @disable propagates throughout the objects hierarchy (all children). you can use it to disable builtins as well such as opEquals
Re: How to call receiveTimout? (std.concurrency)
Ok, thanks! How does the chain of command/responsibility work here? Should I file this to bugzilla? (I'm not able to fix it myself as I haven't built dmd locally. I'm just not quite there yet... : ) /heywood On Jun 29, 2010, at 16:31 , Steven Schveighoffer wrote: On Tue, 29 Jun 2010 09:53:25 -0400, Simen kjaeraas simen.kja...@gmail.com wrote: Heywood Floyd soul...@gmail.com wrote: ops = ops[1 .. $]; // === line 335 Well, this looks like a bug to me. Should be Ops = ops[1 .. $]; Ops is a type, isn't it? Don't you need a variable there? I agree it's a bug in the original, but I don't think that's the fix. -Steve
Re: How to call receiveTimout? (std.concurrency)
On Tue, 29 Jun 2010 13:05:50 -0400, Heywood Floyd soul...@gmail.com wrote: Ok, thanks! How does the chain of command/responsibility work here? Should I file this to bugzilla? (I'm not able to fix it myself as I haven't built dmd locally. I'm just not quite there yet... : ) /heywood Simen actually already filed it. See here: http://d.puremagic.com/issues/show_bug.cgi?id=4406. In the future, just go to http://d.puremagic.com/issues and you can file the bug directly. It's good to ask on d.learn if you aren't sure. And this bug isn't in dmd, it's in phobos :) They are compiled separately (in case you are interested in trying your hand at phobos patches). All the releases come with the complete source code, just cd to the src directory and type make -f blah.mak where blah is your platform (posix, windows, etc.). -Steve
dcollections problem
Hi - probably Steve :) I have problem in compiling a little programm using dcollection.LinkList. (similar problem appears when I use dcollections.ArrayList) D2.047 Linker error.. Error 1 Error 42: Symbol Undefined _D12dcollections8LinkList7__arrayZ Error 2 Error 42: Symbol Undefined _D12dcollections8LinkList12__ModuleInfoZ // LinkList use... import dcollections.LinkList; final class LoadBalancer { alias LinkList!Server ServerList; private ServerList sl; static this() { synchronized lb = new LoadBalancer; } static LoadBalancer opCall() { return lb; } private this() { sl = new ServerList; sl.add( new Server() ); ... } Thanks, Bjoern
Re: dcollections problem
On Tue, 29 Jun 2010 14:13:57 -0400, BLS windev...@hotmail.de wrote: Hi - probably Steve :) I have problem in compiling a little programm using dcollection.LinkList. (similar problem appears when I use dcollections.ArrayList) D2.047 Linker error.. Error 1 Error 42: Symbol Undefined _D12dcollections8LinkList7__arrayZ Error 2 Error 42: Symbol Undefined _D12dcollections8LinkList12__ModuleInfoZ // LinkList use... import dcollections.LinkList; final class LoadBalancer { alias LinkList!Server ServerList; private ServerList sl; static this() { synchronized lb = new LoadBalancer; } static LoadBalancer opCall() { return lb; } private this() { sl = new ServerList; sl.add( new Server() ); ... } Thanks, Bjoern Are you linking against dcollections? It looks like you are not... BTW, you can use dcollections' ticket tracking system for things like this instead of sending to the newsgroup :) http://www.dsource.org/projects/dcollections/newticket -Steve
Re: dcollections problem
On 29/06/2010 20:19, Steven Schveighoffer wrote: Are you linking against dcollections? It looks like you are not... No. BTW, you can use dcollections' ticket tracking system for things like this instead of sending to the newsgroup :) Will do. Thanks Steve bjoern
dcollections how to LinkList // port c# code
Hi, in C# this is common. private ListServer _servers; _servers = new ListServer { new Server{ Name = ServerI, IP = 120.14.220.18 }, new Server{ Name = ServerII, IP = 120.14.220.19 }, new Server{ Name = ServerIII, IP = 120.14.220.20 }, new Server{ Name = ServerIV, IP = 120.14.220.21 }, new Server{ Name = ServerV, IP = 120.14.220.22 }, }; D2 so far.. import dcollections.LinkList; class LoadBalancer { alias LinkList!Server ServerList; private ServerList sl; this() { sl = new ServerList; sl.add( new Server() ); ... } Do I really have to create something like this auto x = new Server(); x.Name = Blah; x.IP = 120.14.220.22; s1.add(x) (Name and IP are Server properties.) thanks bjoern
Re: dcollections how to LinkList // port c# code
BLS: D2 so far.. import dcollections.LinkList; In D use dynamic arrays unless you really need to remove or add a lot of items from the start or middle of the sequence. On modern CPUs linked lists are usually the wrong data structure to use. Bye, bearophile
Re: dcollections how to LinkList // port c# code
On Tue, 29 Jun 2010 15:27:41 -0400, bearophile wrote: In D use dynamic arrays unless you really need to remove or add a lot of items from the start or middle of the sequence. On modern CPUs linked lists are usually the wrong data structure to use. Bye, bearophile D's dynamic arrays are great! Also you should create a constructor for a common build operation. import std.stdio; class Server { string name; string ip; this( string _name, string _ip ) { name = _name; ip = _ip; } string toString() { return name ~ - ~ ip; } } void main() { Server[] serverList = [ new Server( a, 164.76.0.1 ), new Server( b, 164.76.0.2 ) ]; foreach( server; serverList ) { writeln( server ); } } - a - 164.76.0.1 b - 164.76.0.2 -B
Re: dcollections how to LinkList // port c# code
On Tue, 29 Jun 2010 15:22:30 -0400, BLS windev...@hotmail.de wrote: Hi, in C# this is common. private ListServer _servers; _servers = new ListServer { new Server{ Name = ServerI, IP = 120.14.220.18 }, new Server{ Name = ServerII, IP = 120.14.220.19 }, new Server{ Name = ServerIII, IP = 120.14.220.20 }, new Server{ Name = ServerIV, IP = 120.14.220.21 }, new Server{ Name = ServerV, IP = 120.14.220.22 }, }; D2 so far.. import dcollections.LinkList; class LoadBalancer { alias LinkList!Server ServerList; private ServerList sl; this() { sl = new ServerList; sl.add( new Server() ); ... } Do I really have to create something like this auto x = new Server(); x.Name = Blah; x.IP = 120.14.220.22; s1.add(x) (Name and IP are Server properties.) I think I need to add some constructors that accept data. std.container has some cool construction methods. For now, can you do something like this? sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(...) ... ]); The new constructor would probably do something like this: sl = new ServerList( new Server(...), new Server(...), ... ); Does that work for you? If you need to build servers by naming fields, I'm not sure that's really a dcollections issue, D doesn't support constructing object by specifing individual field names. Alternatively, you could define an external constructor: Server create(string name, string ip) { auto retval = new Server(); retval.Name = name; retval.IP = ip; return retval; } BTW, I don't think I've ever constructed a list that way in C#, it's cool :) -Steve
Re: dcollections how to LinkList // port c# code
On 29/06/2010 22:12, Steven Schveighoffer wrote: For now, can you do something like this? sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(...) ... ]); Hi Steve, I think this should work, however I got very strange err. msg. in file ArrayList.d Have to stop now..need some sleep. BTW the new constructor stuff would be nice to have. //current code. import std.stdio; import std.random; import dcollections.ArrayList; import dcollections.LinkList; void main() { auto b1 = LoadBalancer(); auto b2 = LoadBalancer(); auto b3 = LoadBalancer(); // Confirm these are the same instance if (b1 == b2 b2 == b3 ) { writeln(Same instance\n); } // Next, load 15 requests for a server for (int i = 0; i 15; i++) { string serverName = b1.nextServer.servername; writeln(Dispatch request to: ~ serverName); } } // D2 singleton final class LoadBalancer { private static LoadBalancer lb; alias ArrayList!Server ServerList; private ServerList sl; static this() { synchronized lb = new LoadBalancer; } static LoadBalancer opCall() { return lb; } private this() { sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(ServerII, 121.14.220.18) ]); } @property { Server nextServer() { return sl[uniform(0, sl.length)]; } } private class Server { private string _name, _id; this(string name, string id) { this._name = _name; this._id = id; } string servername() { return _name; } /* OLD PROPERTY STUFF @property { string servername(string sn) { return _name = sn; } string servername() { return _name; } string id(string id) { return _id = id; } string id() { return _id; } } */ } } cheers,bjoern
Re: dcollections how to LinkList // port c# code
On Tue, 29 Jun 2010 17:33:13 -0400, BLS windev...@hotmail.de wrote: On 29/06/2010 22:12, Steven Schveighoffer wrote: For now, can you do something like this? sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(...) ... ]); Hi Steve, I think this should work, however I got very strange err. msg. in file ArrayList.d Have to stop now..need some sleep. BTW the new constructor stuff would be nice to have. //current code. import std.stdio; import std.random; import dcollections.ArrayList; import dcollections.LinkList; void main() { auto b1 = LoadBalancer(); auto b2 = LoadBalancer(); auto b3 = LoadBalancer(); // Confirm these are the same instance if (b1 == b2 b2 == b3 ) { writeln(Same instance\n); } // Next, load 15 requests for a server for (int i = 0; i 15; i++) { string serverName = b1.nextServer.servername; writeln(Dispatch request to: ~ serverName); } } // D2 singleton final class LoadBalancer { private static LoadBalancer lb; alias ArrayList!Server ServerList; private ServerList sl; static this() { synchronized lb = new LoadBalancer; } static LoadBalancer opCall() { return lb; } private this() { sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(ServerII, 121.14.220.18) ]); } @property { Server nextServer() { return sl[uniform(0, sl.length)]; } } private class Server { private string _name, _id; this(string name, string id) { this._name = _name; this._id = id; } string servername() { return _name; } /* OLD PROPERTY STUFF @property { string servername(string sn) { return _name = sn; } string servername() { return _name; } string id(string id) { return _id = id; } string id() { return _id; } } */ } } cheers,bjoern One thing to note, ArrayList *does* accept an array as a constructor, and it will actually use that array as its storage. This is so you can wrap an array as a ArrayList and get the full dcollections functionality from it. The other containers do not accept an array for construction... yet :) -Steve
Re: dcollections how to LinkList // port c# code
On 29/06/2010 23:49, Steven Schveighoffer wrote: One thing to note, ArrayList *does* accept an array as a constructor, and it will actually use that array as its storage. This is so you can wrap an array as a ArrayList and get the full dcollections functionality from it. Hi Steve This is why I've switched from LinkList to ArrayList. (ArrayList is simply cool) also : Server nextServer() { return sl[uniform(0, sl.length)]; } is impossible with LinkList. ( sl is LinkList) yeah opIndex on linked lists is simply slow. However the snippet in the previous msg. does not compile.. will see later. bjoern
Class knowing its own Class
What is the pretty way to do something like this? Class C { private const char[] _name = C;// demangling this.mangleof didn't work void makeNew() { mixin(`new `~_name~`();`); // the not so pretty part } }
Re: Class knowing its own Class
On Tue, 29 Jun 2010 17:59:37 -0400, strtr st...@sp.am wrote: What is the pretty way to do something like this? Class C { void makeNew() { new typeof(this); } } As edited... -Steve
Re: Class knowing its own Class
== Quote from Steven Schveighoffer (schvei...@yahoo.com)'s article On Tue, 29 Jun 2010 17:59:37 -0400, strtr st...@sp.am wrote: What is the pretty way to do something like this? Class C { void makeNew() { new typeof(this); } } As edited... -Steve Whahaha! Thanks, I knew I was missing something here.
Mixing operations with signed and unsigned types
I was surprised by the behavior of division. The resulting type of division in example below is uint and the value is incorrect. I would expect that when one of operands is signed, then the result is signed type. int a = -6; uint b = 2; auto c = a / b; // c is type of uint, and has value 2147483645 int d = a / b; // int, 2147483645 auto e = a / cast(int)b; // e, -3 (ok) I have longer time problems with mixing int and uint, so I tested some expression now and here is the result. auto f = a - b // uint, 4294967288 auto g = a + b // uint, 4294967292 auto h = a b // bool, false auto i = a b // bool, true Recently while I was hunting some bug in templated code, I created a templated function for operator , which requires both arguments to be either signed or unsigned. Fortunately D such function was quite easy to do, if it wasn't possible I don't know if I would ever find form where the ints and uints come from... bool sameSign (A, B) () { return isUnsigned!(A) isUnsigned!(B)) || (isSigned!(A) isSigned! (B); } bool lt (A, B) (A a, B b) { static assert (sameSign!(A, B) ()); return a b; } Could somebody please tell me why is this behavior, when mixing signed and unsigned, preferred over one that computes correct result. If this cannot be changed, is it possible to just make compiler error/warning when such incorrect calculation could occur. If it is possible in D code to require same-signed types for function, it is definitely possible for compiler to require explicit cast in such cases.
Re: Mixing operations with signed and unsigned types
Stewart Gordon: http://d.puremagic.com/issues/show_bug.cgi?id=259 I have added my vote there a lot of time ago. I think Andrei says that fixing this is unworkable, but I don't know why. If you make this an error and at the same time turn array indexes/lengths into signed values, you don't have that many unsigned values in normal D programs, so you need very few casts and it becomes workable. Bye, bearophile
Re: Mixing operations with signed and unsigned types
On Tue, 29 Jun 2010 19:42:45 -0400, bearophile wrote: Stewart Gordon: http://d.puremagic.com/issues/show_bug.cgi?id=259 I have added my vote there a lot of time ago. I think Andrei says that fixing this is unworkable, but I don't know why. If you make this an error and at the same time turn array indexes/lengths into signed values, you don't have that many unsigned values in normal D programs, so you need very few casts and it becomes workable. Bye, bearophile Why on the earth should array indexes and lengths be signed !!! My brain just explodes when I think of something like that.
Re: Mixing operations with signed and unsigned types
On Tue, 29 Jun 2010 19:42:45 -0400, bearophile wrote: Stewart Gordon: http://d.puremagic.com/issues/show_bug.cgi?id=259 I have added my vote there a lot of time ago. I think Andrei says that fixing this is unworkable, but I don't know why. If you make this an error and at the same time turn array indexes/lengths into signed values, you don't have that many unsigned values in normal D programs, so you need very few casts and it becomes workable. Bye, bearophile I voted for the bug, but IMO it should be fixed by other means as making array indexes and lengths signed. It makes no sense for me, and would probably affect lot of code.
Re: Mixing operations with signed and unsigned types
On Wed, 30 Jun 2010 00:30:19 +0100, Stewart Gordon wrote: Michal Minich wrote: I was surprised by the behavior of division. The resulting type of division in example below is uint and the value is incorrect. I would expect that when one of operands is signed, then the result is signed type. Going by the spec http://www.digitalmars.com/d/1.0/type.html Usual Arithmetic Conversions the compiler is behaving correctly. point 4.4 in docs - The signed type is converted to the unsigned type. this is just not good for most common binary operators, it might be useful for , | and maybe shift, but they are quite less common
Re: dcollections how to LinkList // port c# code
Just a few things that may cause you some bugs/errors On Tue, 29 Jun 2010 23:33:13 +0200, BLS wrote: On 29/06/2010 22:12, Steven Schveighoffer wrote: // Confirm these are the same instance if (b1 == b2 b2 == b3 ) { writeln(Same instance\n); } I think you mean to use is here if( b1 is b2 b2 is b3 ) // == compares value, not pointer/references // D2 singleton final class LoadBalancer { private static LoadBalancer lb; alias ArrayList!Server ServerList; private ServerList sl; static this() { synchronized lb = new LoadBalancer; } Might want the declare the class as synchronized, or make lb shared private this() { sl = new ServerList; sl.add([ new Server(ServerI, 120.14.220.18), new Server(ServerII, 121.14.220.18) ]); } try sl = new ServerList([ new Server(ServerI, 120.14.220.18), new Server(ServerII, 121.14.220.18) ]); this(string name, string id) { this._name = _name; this._id = id; } this._name = name; // you had _name -B
Re: Mixing operations with signed and unsigned types
There is very long discussion on digitamars.D ng Is there ANY chance we can fix the bitwise operator precedence rules? which I should probably read first...but was there some conclusion ?
Re: Mixing operations with signed and unsigned types
Michal Minich wrote: I was surprised by the behavior of division. The resulting type of division in example below is uint and the value is incorrect. I would expect that when one of operands is signed, then the result is signed type. Going by the spec http://www.digitalmars.com/d/1.0/type.html Usual Arithmetic Conversions the compiler is behaving correctly. But see below snip auto f = a - b // uint, 4294967288 auto g = a + b // uint, 4294967292 auto h = a b // bool, false auto i = a b // bool, true Recently while I was hunting some bug in templated code, I created a templated function for operator , which requires both arguments to be either signed or unsigned. It is in fact a bug that DMD accepts it. http://www.digitalmars.com/d/1.0/expression.html#RelExpression http://d.puremagic.com/issues/show_bug.cgi?id=259 Fortunately D such function was quite easy to do, if it wasn't possible I don't know if I would ever find form where the ints and uints come from... bool sameSign (A, B) () { return isUnsigned!(A) isUnsigned!(B)) || (isSigned!(A) isSigned! (B); } bool lt (A, B) (A a, B b) { static assert (sameSign!(A, B) ()); return a b; } Could somebody please tell me why is this behavior, when mixing signed and unsigned, preferred over one that computes correct result. It would appear to be Walter's idea of C compatibility taking control again. If this cannot be changed, is it possible to just make compiler error/warning when such incorrect calculation could occur. If it is possible in D code to require same-signed types for function, it is definitely possible for compiler to require explicit cast in such cases. I agree. Either behave sensibly or generate an error. Stewart.
Re: dcollections how to LinkList // port c# code
Byron Heads: this(string name, string id) { this._name = _name; this._id = id; } this._name = name; // you had _name I have just filed a bug report on this (it's a lot of time I want to write it): http://d.puremagic.com/issues/show_bug.cgi?id=4407 Bye, bearophile
Re: How to call receiveTimout? (std.concurrency)
Thanks! I will! /heywood PS. I like D. On Jun 29, 2010, at 19:37 , Steven Schveighoffer wrote: On Tue, 29 Jun 2010 13:05:50 -0400, Heywood Floyd soul...@gmail.com wrote: Ok, thanks! How does the chain of command/responsibility work here? Should I file this to bugzilla? (I'm not able to fix it myself as I haven't built dmd locally. I'm just not quite there yet... : ) /heywood Simen actually already filed it. See here: http://d.puremagic.com/issues/show_bug.cgi?id=4406. In the future, just go to http://d.puremagic.com/issues and you can file the bug directly. It's good to ask on d.learn if you aren't sure. And this bug isn't in dmd, it's in phobos :) They are compiled separately (in case you are interested in trying your hand at phobos patches). All the releases come with the complete source code, just cd to the src directory and type make -f blah.mak where blah is your platform (posix, windows, etc.). -Steve