digitalmars.D.learn - std.socket.Address not allowed in tuples
- Steven O (15/15) Jan 23 2019 Can anyone please explain to me what's going on here?
- Neia Neutuladh (24/25) Jan 23 2019 As always, it helps a lot to post the error message the compiler gave yo...
- Steven O (8/14) Jan 23 2019 Sorry about that.
- Neia Neutuladh (20/22) Jan 23 2019 You define a function implementing the "less" operation for a pair of
Can anyone please explain to me what's going on here? import std.container; import std.socket; import std.typecons; void main() { /* Doesn't work alias Rec_type = Tuple!(Address, "x", int, "y", int, "z"); RedBlackTree!Rec_type[] records; */ // Works alias Rec_type = Tuple!(int, "y", int, "z"); RedBlackTree!Rec_type[] records; } Why am I not allowed to put Address types in tuples?
Jan 23 2019
On Wed, 23 Jan 2019 15:56:15 +0000, Steven O wrote:Why am I not allowed to put Address types in tuples?As always, it helps a lot to post the error message the compiler gave you. The message is: /usr/include/dmd/phobos/std/functional.d-mixin-215(215): Error: template std.typecons.Tuple!(Address, int).Tuple.opCmp cannot deduce function from argument types !()(Tuple!(Address, int)) inout, candidates are: /usr/include/dmd/phobos/std/typecons.d(806): std.typecons.Tuple! (Address, int).Tuple.opCmp(R)(R rhs) if (areCompatibleTuples! (typeof(this), R, "<")) /usr/include/dmd/phobos/std/typecons.d(820): std.typecons.Tuple! (Address, int).Tuple.opCmp(R)(R rhs) if (areCompatibleTuples! (typeof(this), R, "<")) /usr/include/dmd/phobos/std/container/rbtree.d(866): Error: template instance `std.functional.binaryFun!("a < b", "a", "b").binaryFun! (inout(Tuple!(Address, int)), Tuple!(Address, int))` error instantiating scratch.d(13): instantiated from here: RedBlackTree!(Tuple! (Address, int), "a < b", false) The last line says that the error came when trying to instantiate the RedBlackTree template. At that point, Tuple was instantiated. There is no issue putting an address in a Tuple. The issue is that Address doesn't have a comparison operator defined, so the resulting tuple type can't be compared with the standard operators. You need to define your own function for comparing the tuples in question and pass that to RedBlackTree.
Jan 23 2019
On Wednesday, 23 January 2019 at 16:30:33 UTC, Neia Neutuladh wrote:As always, it helps a lot to post the error message the compiler gave you.Sorry about that.The issue is that Address doesn't have a comparison operator defined, so the resulting tuple type can't be compared with the standard operators. You need to define your own function for comparing the tuples in question and pass that to RedBlackTree.Is there any documentation or examples of how to do that? The RedBlackTree documentation gives trivial examples like auto maxTree = redBlackTree!"a > b"(iota(5)); but, that doesn't really help me figure out how to do something more complex.
Jan 23 2019
On Thu, 24 Jan 2019 01:35:57 +0000, Steven O wrote:Is there any documentation or examples of how to do that? The RedBlackTree documentation gives trivial examples likeYou define a function implementing the "less" operation for a pair of Tuple!(Address, int) (or whatever you have). --- alias V = Tuple!(Address, int); bool less(V a, V b) { if (a[0].classinfo != b[0].classinfo) { return a[0].classinfo < b[0].classinfo; } // do something for InternetAddress, Internet6Address, etc return 0; } --- Then pass that to RedBlackTree: --- alias VTree = RedBlackTree!(V, less); VTree tree = new VTree(); ---
Jan 23 2019