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

HList with trait bounded items #207

Open
damooo opened this issue Nov 3, 2022 · 5 comments
Open

HList with trait bounded items #207

damooo opened this issue Nov 3, 2022 · 5 comments

Comments

@damooo
Copy link

damooo commented Nov 3, 2022

Thanks for frunk!

Sorry if this is obvious, but i cannot figure out how to spell out an hlist with all items satisfying a trait bound. Could you help?

Thanks again for your work.

@BGR360
Copy link
Contributor

BGR360 commented Nov 3, 2022

Could you clarify a bit more what you're trying to do?

@damooo
Copy link
Author

damooo commented Nov 3, 2022

I want to construct a hlist of tokens, all satisfying certain trait called AdjectiveToken. So that i can keep that as a valu to list of adjective tokens. I want it to be HList, instead of Vec<Box<dyn AdjectiveToken>> as i should be able to specify adjective tokens as trait bounds whrere the struct is expected.

@BGR360
Copy link
Contributor

BGR360 commented Nov 4, 2022

Does this do what you want?

impl<Head, Tail> AdjectiveToken for HCons<Head, Tail>
where
    Head: AdjectiveToken,
    Tail: AdjectiveToken,
{ ... }

impl AdjectiveToken for HNil { ... }

fn function_which_needs_all_hlist_elements_to_be_tokens<Head, Tail>(hlist: HCons<Head, Tail>)
where
    HCons<Head, Tail>: AdjectiveToken
{ ... }

@damooo
Copy link
Author

damooo commented Nov 4, 2022

I assume, you meant following:

trait AdjectiveTokenList {}

impl<Head, Tail> AdjectiveTokenList for HCons<Head, Tail>
where
    Head: AdjectiveToken,
    Tail: AdjectiveTokenList,
{ ... }

impl AdjectiveTokenList for HNil { ... }

fn function_which_needs_all_hlist_elements_to_be_tokens<Head, Tail>(hlist: HCons<Head, Tail>)
where
    HCons<Head, Tail>: AdjectiveTokenList
{...}

If so, indeed that is what i mean. But issue is, such an AdjectiveTokenList cannot implement trait HList. As HList requires implementation to allow prepend any unconstrained item in it's trait methods.

@BGR360
Copy link
Contributor

BGR360 commented Nov 4, 2022

Please sketch out the pseudocode of what you're hoping to do in the end, I'm really having a hard time understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants