www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A predicate with different argument types

reply psycha0s <box mail.com> writes:
Is it possible to make a comparison predicate with different 
argument types? For instance, suppose I have a struct like this:

struct Attribute {
     string name;
     variant value;
}

Also, suppose I have a sorted vector of such values. So I can 
quickly find one by its name field and I have index-based random 
access to elements at the same time. Therefore I have something 
like this:

Attribute[] attributes;

Now, imagine I try to find an insertion position for a new 
element:

--------
string name; // input data

auto sorted = object_.assumeSorted!((a, b) => a.name < b.name);
Attribute dummy;
dummy.name = name;
auto pos = sorted.lowerBound(dummy);
--------

It works fine but is there a way to get rid of the 'dummy' 
object? I can easily achieve that in C++ since STL allows to have 
a predicate with different argument types. Ideally, I'd like 
something like this:

--------
string name; // input data

auto sorted = object_.assumeSorted!((a, b) => a.name < name);
auto pos = sorted.lowerBound(name);
--------

Thanks.
Feb 10 2019
parent psycha0s <box mail.com> writes:
 auto sorted = object_.assumeSorted!((a, b) => a.name < b.name);
Sorry for the copy-paste. It should be "attributes" in place of "object_" here, of course: auto sorted = attributes.assumeSorted!((a, b) => a.name < b.name);
Feb 10 2019