Issue 19475 - add a staticIndexOf overload taking a pred
Summary: add a staticIndexOf overload taking a pred
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-11 09:58 UTC by basile-z
Modified: 2024-12-01 16:34 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2018-12-11 09:58:48 UTC
Overloads of std.meta.staticIndexOf allow only to find an exact needle but in some case it more useful to get the index of something based on a predicate, for example to get the index of a compile time value that partially matches (e.g member value of a struct used as UDA).

It even may be better to name them firstStaticIndexOf lastStaticIndexOf
Comment 1 Simen Kjaeraas 2018-12-11 13:52:18 UTC
This can be trivially done as a combination of existing templates:

import std.meta;

enum predStaticIndexOf(alias Fn, T...) = staticIndexOf!(true, staticMap!(Fn, T));

template MoreThan2(T...) {
    enum MoreThan2 = T[0] > 2;
}

unittest {
    static assert(predStaticIndexOf!(MoreThan2, 1,2,3) == 2);
}
Comment 2 dlangBugzillaToGithub 2024-12-01 16:34:36 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9767

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB