softwaremill / softwaremill/quicklens
implement extractors
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 852
- Forks
- 53
- PR merge metrics
- No merged PRs in 30d
Description
As we can't use:
scala> def extractor(p: Person) = p.address.street.name
extractor: (p: Person)String
scala> val p2 = person.modify(extractor).using(_.toUpperCase)
<console>:21: error: Path must have shape: _.field1.field2.each.field3.(...)
as it's hard to analyze such complex AST, it would be great to have something like:
person.modify(_.address.street.name).get
Which will return a List of values. This will allow to reuse a path for both read and write.
My practical need is to actually check a path at one instance of case class, but modify the same path in another instance of the same case class:
val accessor = (_: Person).modify(_.address.street.name) //"access" alias for "modify" would be also great
if(accessor(person1).get == "Functional Road") accessor(person2).setTo("Imperative Road")
Currently, I've got to hack it with vars (or alternate the model), which is not very functional...
def getter[T, U](pm: T => PathModify[T, U])(in: T): List[U] = {
var values: List[U] = Nil //list is just in case of "each"
pm(in).using{x =>
values ++= x
x
}
values
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the existing modify and PathModify APIs, including using and setTo, to understand how paths are represented and evaluated. Define the accessor behavior described in the issue, including get returning values and reusing the same path for read and write; done means the Person example works without mutable temporary state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100