-
Notifications
You must be signed in to change notification settings - Fork 21
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
Generalising many
and some
#141
Labels
Comments
Whatever we come up with here should ideally be applied to stuff like |
Turns out, following the Scala 2.13+ way of using |
Draft
40 tasks
Merged
j-mie6
added a commit
that referenced
this issue
Feb 16, 2025
This PR, which closes #141, introduces `Factory`-forms for all iterative combinators. These allow them to return any arbitrary type `C` (not limited to sequences, `String` is possible, as are `Set`s and `Map`s). This is done by including a `Factory[A, C]` into these combinators. Such factories can be obtained by implicit conversion from `IterableFactory` etc, which are the base types for the companion objects of various collections. For instance `many(p)` is equivalent to `many(p, List)`. In Scala 2, the inference for these can be a bit shaky, and full type ascription may be required (unfortunately). In Scala 3 this is not an issue. To improve the situation on Scala 2, additional overloads could be added to take explicitly `IterableFactory` (this is done for some combinators anyway, as they would otherwise always need the ascription even in Scala 3); however, this would increase API noise and doesn't account for other special cases like `EvidenceIterableFactory`, which allows for the construction of `Array` and `SortedSet`s, say. One fix to this problem would be to bite the bullet and move the combinators to suffix-form: ```scala p.many, p.many(List) p.sepBy(sep), p.sepBy(sep, List) ... ``` The question would be where to draw the line. This would have the advantage of improving the autocompletion of combinators!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Is your feature request related to a problem? Please describe.
As of parsley
4.0.0
,many
andsome
can be used to parse zero/one or more values into aList
. This works pretty well, but occasionally, it is desirable to parse it into a different type,Vector
orArray
, say. In this sense,many
andsome
are a bit wasteful.Describe the solution you'd like
It might be nice to explore generalising these combinators to take in an implicit instance of a typeclass that helps build various collections, and use this internally. The low-priority implicits pattern could be used to ensure that
List
is chosen above other alternatives, say.The cleanest way of doing this would be to support HKTs, so
many[A, F[_]](p: Parsley[A])(...): Parsley[F[A]])
, allowing for something likemany[Char, Vector](p): Parsley[Vector[Char]]
, but this rules out three useful use cases: chars into strings, ints into strings, and (k, v) pairs intoMap
s. Annoyingly this means we'd be looking atmany[A, C](p: Parsley[A])(...): Parsley[C]
, which places extra burden on implicit resolution to tie the knot, and more verbose type ascription when it goes wrong (i.e.many[A, Vector[A]]
).In any case, it's a bit annoying to have two parameters there, the
A
and theC
/C[_]
. Perhaps it's time to movemany
andsome
to be methods on Parsley itself? This would allow forp.many[Vector]
/p.many[Vector[A]]
which feels like an improvement. That said, perhaps it could be done via extension methods, and have two different kinds, one for HKTs and the other for everything more general?Describe alternatives you've considered
Obviously, constructing stuff like
Vector
orMap
, can be be using.foldRight
or.foldLeft
, but this doesn't leverage mutable builders. The use of mutable builders makes this pattern of use far more clumsy than it needs to be. The existence ofstringOfMany
andstringOfSome
further points to this, and their implementations are non-trivial without the secret internal combinators within parsley: they are desirable though!Additional Context
If we go down this route, we should be careful to explore the performance implications of "de-optimising" the existing
many
implementation.The text was updated successfully, but these errors were encountered: