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

Feature Request: toLabelledProduct #853

Open
schlichtanders opened this issue Aug 21, 2018 · 10 comments
Open

Feature Request: toLabelledProduct #853

schlichtanders opened this issue Aug 21, 2018 · 10 comments

Comments

@schlichtanders
Copy link

Dear shapeless team,

having toProduct support for unnamed Hlist, it seems a natural request to have method like toLabelledProduct for named Hlist of records (like automatically derived by LabelledGeneric )

any plans on implementing this?

@milessabin
Copy link
Owner

Are you referring to the toProduct syntax for function types that's defined in shapeless/syntax/std/functions.scala?

@schlichtanders
Copy link
Author

schlichtanders commented Aug 21, 2018

yes, thanks for clarification.

I am coming from a Python background and there I have the star * and doublestar ** tools for passing lists and dictionaries as function arguments respectively.

With shapeless it seems we can already support * via myfunction.toProduct(hlist) and using plain HLists
It would be great to support named arguments like in ** similarly by something like myfunction.toLabelledProduct(labelledHList)

@milessabin
Copy link
Owner

Gotcha.

Scala function types don't encode the names of their arguments, so unfortunately what you ask isn't possible. Scala methods do, but they have the limitation that they're not first class. If that isn't a problem for you, then you might be able to make some headway with RecordArgs.

@schlichtanders
Copy link
Author

I see. Yeah that is unfortunate, but if something like this could work with methods, I would love to have it

how could the syntax look like?

@milessabin
Copy link
Owner

It does work with methods ... see the tests for RecordArgs and FromRecordArgs, or look at some of the uses in other projects :-)

@schlichtanders
Copy link
Author

thanks a lot for the pointers!
so it seems you have to extend RecordArgs if you want to build you own custom Records HList methods, but offer "normal" interfaces. Perfect

However for interfacing already defined normal functions with FromRecordArgs it seems we have to hope that the class is inheritable, or the methods defined on a trait, so that we can create our custom wrapper class Wrapper extends Original with FromRecordArgs

Is there also support for interfacing "normal" methods which are not inheritable in such a manner?

@milessabin
Copy link
Owner

Is there also support for interfacing "normal" methods which are not inheritable in such a manner?

No there isn't.

@schlichtanders
Copy link
Author

thanks a lot

this sounds like a class FromRecordArgsProxy[A](a: A) extends FromRecordArgs would be awesome, which is supposed to wrap a custom object of kind of arbitrary type. With such you could easily access myMethodRecord for any "normal" method.

However to the best of my knowledge such proxies are not well supported currently (haven't found a single satisfiable implementation out there for Scala). If you know about a proxy which could accomplish this, please drop me a line.

@joroKr21
Copy link
Collaborator

One could imagine having a toLabelledProduct where you have to pass the labels explicitly.
But I'm not sure how useful that would be.

@joroKr21
Copy link
Collaborator

It could also work as a macro when passed a reference to a method or a literal function.

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

3 participants