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 associated type to link request and response types? #6

Open
icefoxen opened this issue Dec 5, 2017 · 2 comments
Open

Add associated type to link request and response types? #6

icefoxen opened this issue Dec 5, 2017 · 2 comments

Comments

@icefoxen
Copy link
Contributor

icefoxen commented Dec 5, 2017

Just a thought that I had while fixing a bug I'd made...

We have the request::ApiRequest type which specifies some data for the request type. It should be possible to make an associated type request::ApiRequest::Response to link the response type to a particular request type. Then the type of client::IpfsClient::request<Req, Res>() -> AsyncResponse<Res> could just become client::IpfsClient::request<Req>() -> AsyncResponse<Req::Response>.

That wouldn't have prevented the bug that I'd made, I would have just put the typo in a different spot, so I'm not sure if this will actually help rather than just bikeshedding. But maybe it could make life a little simpler?

...actually, now that I think about it, we might be able to make the ApiRequest::path() method be an associated const now. Though that might be more restrictive than we want? I dunno.

@ferristseng
Copy link
Owner

Yeah, I like the idea of the associated type. I was planning on making a macro to implement the ApiRequest trait, since a large portion of it is just boiler plate. Since I was pretty consistent with the type naming, I think it might be possible to make a macro where you just pass in the request name, and it would automatically link the response as an associated type. I think a combination of the associated type, and the macro would make it really difficult to ever make a naming issue.

Also I'm okay with making path an associated const. I didn't know about associated consts, but the path method is pretty much acting like one right now.

@icefoxen
Copy link
Contributor Author

icefoxen commented Dec 5, 2017

I think it might be possible to make a macro where you just pass in the request name, and it would automatically link the response as an associated type

Let me know if you work this out, but afaik synthesizing new identifiers in macros is not currently feasible... :-(

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