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

Boolean literal types based filtering #275

Open
alexarchambault opened this issue Dec 5, 2014 · 1 comment
Open

Boolean literal types based filtering #275

alexarchambault opened this issue Dec 5, 2014 · 1 comment

Comments

@alexarchambault
Copy link
Collaborator

Currently, the filter methods on HLists and Coproducts filter elements of a given type only.
Wouldn't it make more sense to have a filtering based on value types - Boolean literals - returned by a Poly? This way it would

  • look closer to the filter method of standard collections (like map is),
  • allow more complex filtering (filtering more than one type, or filtering based on value types)
  • be closer to a filterKeys method that could be added for records and unions - as keys have value types, it doesn't make really sense to filter them only on their types (if it was, only two types of filtering would be allowed then: all-or-nothing, or filtering one or all but one key).

For example, it allows to write things like

object intOrString extends Poly1 {
  implicit def caseBoolean = at[Boolean](_ => False)
  implicit def caseInt = at[Int](_ => True)
  implicit def caseString = at[String](_ => True)
}

val l = 1 :: true :: "foo" :: 2 :: HNil
val filtered = l.filterNot(intOrString)

The problem with this new filter is that it would use boolean literal types, and in particular some macro to manipulate these value types. But these macro cannot be called during their own build (the one of the shapeless-core project). So they have to be built separately, in a bootstrap project say. So the new filtering would require a bootstrap project for some macro and definitions that they use.

It would also require renaming the former filter methods to something else, like filterType, so that would break compatibility with former shapeless versions.

I gave a try to all of this here:
https://github.com/alexarchambault/shapeless/tree/refactor/bootstrap (bootstrap project),
https://github.com/alexarchambault/shapeless/tree/refactor/filtertype (renaming the former filter to filterType), and
https://github.com/alexarchambault/shapeless/tree/feature/filter (new filter method).

So all of this is feasible, but requires big changes. I let you decide whether you think they are worth it. (I think they are as far as I'm concerned :-)

@alexarchambault alexarchambault changed the title Boolean literal based filtering Boolean literal types based filtering Dec 5, 2014
@milessabin
Copy link
Owner

I'm going to punt this to 3.0 ... or has it been subsumed by Refined?

@milessabin milessabin added this to the shapeless-3.0.0 milestone Feb 8, 2016
@joroKr21 joroKr21 removed this from the shapeless-3.0.0 milestone Sep 12, 2022
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