question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guide for implementing a custom refined type (ITT: Some cool FiniteDuration refining cheats)

See original GitHub issue

I love this library, thanks for your work on it

I wanted a way to say a FiniteDuration must be > 5 seconds earlier today and set about looking into writing my own refined type, but it looks tricky - is this really simple, have I missed something?

A guide on extending the library with your own types might be nice (which is why i made this ticket)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
henricookcommented, May 1, 2019

I’ve done some shameless thievery from the size validate instance you linked, and i haven’t yet tested this but:

final case class LengthInSeconds[P](lengthInSeconds: P)

object LengthInSeconds {
  implicit def lengthInSecondsValidate[P, RP](
    implicit v: Validate.Aux[Long, P, RP]
  ): Validate.Aux[FiniteDuration, LengthInSeconds[P], LengthInSeconds[v.Res]] =
    // Convert FiniteDuration to seconds and use v to validate that value
    new Validate[FiniteDuration, LengthInSeconds[P]] {
      override type R = LengthInSeconds[v.Res]

      override def validate(duration: FiniteDuration): Res = {
        val r = v.validate(duration.toSeconds)
        r.as(LengthInSeconds(r))
      }

      override def showExpr(duration: FiniteDuration): String =
        v.showExpr(duration.toSeconds)

      override def showResult(duration: FiniteDuration, r: Res): String = {
        val lengthInSeconds = duration.toSeconds
        val nested          = v.showResult(lengthInSeconds, r.detail.lengthInSeconds)
        Resources.predicateTakingResultDetail(s"lengthInSeconds({$duration.toSeconds}) = $lengthInSeconds", r, nested)
      }
    }
}
1reaction
fthomascommented, May 3, 2019

You could define a companion object for MySpecialType:

type MySpecialType = FiniteDuration Refined LengthInSeconds[Greater[W.`5`.T]]

object MySpecialType extends RefinedTypeOps[MySpecialType, FiniteDuration]

MySpecialType.from is then the same function as refineV[LengthInSeconds[Greater[W.`5`.T]]](_: FiniteDuration).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Safe, Expressive Code with Refinement Types - OVO Tech Blog
In this blog post I will explore refinement types and their implementation in Scala's refined library. Refinement types are a powerful tool ...
Read more >
Refined types, what are they good for? - Beyond the lines
Type refinement is all about making the types more precise. But why would do that? Because using the correct types makes your program...
Read more >
fthomas/refined: Refinement types for Scala - GitHub
refined -scopt allows to read command line options with refined types using scopt; refined-shapeless. External modules. Below is an incomplete list of third- ......
Read more >
How we used Refined to improve type safety and error ...
Today, we are going to talk about a way to refine our types to make them more expressive and increase confidence in our...
Read more >
Build your own refinement types in Scala 3 - Michał Sitko blog
I wondered what it would take to implement a simple refinement types library. ... new version can offer some improvements in the process....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found