Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a staticIndexOf overload taking a pred #9767

Open
dlangBugzillaToGithub opened this issue Dec 11, 2018 · 1 comment
Open

add a staticIndexOf overload taking a pred #9767

dlangBugzillaToGithub opened this issue Dec 11, 2018 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

b2.temp reported this on 2018-12-11T09:58:48Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=19475

CC List

  • simen.kjaras

Description

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
@dlangBugzillaToGithub
Copy link
Author

simen.kjaras commented on 2018-12-11T13:52:18Z

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);
}

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants