Questions tagged [refined]

A refinement type is a type endowed with a predicate which is assumed to hold for any element of the refined type. "Refined" are libraries in Scala and Haskell for refining types.

In type theory, a refinement type is a type endowed with a predicate which is assumed to hold for any element of the refined type. Connected with dependent types.

In Scala refined types are types like type A { type T = ... }. Connected with compound types and structural types.

refined is a Scala library for refining types with type-level predicates which constrain the set of values described by the refined type. It started as a port of the refined Haskell library.

23 questions
16
votes
2 answers

Refined and existential types for runtime values

Suppose I want to map between some strings and integer identifiers, and I want my types to make it impossible to get a runtime failure because someone tried to look up an id that was out of range. Here's one straightforward API: trait Vocab { def…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
5
votes
0 answers

Scala refined: concatenating NonEmptyStrings

I'm using the NonEmptyString type from the refined library. When concatenating two strings, at least one of which is non-empty, the result is obviously another non-empty string. But is there a way to convince the Scala compiler of this fact without…
Matthias Berndt
  • 4,387
  • 1
  • 11
  • 25
5
votes
0 answers

How can I define A and B depending from each other typed class using refined library?

Problem: I have a case class Passenger, that starts from point A and goes to point B. A valid passenger means that point A doesn't equal to point B. Passenger( a: Int, b: Int ) Question: How can I design Passenger class using refind library to…
mkUltra
  • 2,828
  • 1
  • 22
  • 47
4
votes
2 answers

Scala: how to force converting a statement to literal?

I'm experimenting the refined type feature of scala provided in one of its library: https://github.com/fthomas/refined The following code represents a simple case: import eu.timepit.refined.auto._ import shapeless.{Witness => W} type Vec5 =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
4
votes
1 answer

Using PureConfig with Refined?

I have the below conf file: connection.port = 8080 connection.interface = "127.0.0.1" I am trying to use refined and refined-pureconfig when reading this file. I have the below class: import com.api.models.{Config, Connection} import…
Nespony
  • 1,253
  • 4
  • 24
  • 42
3
votes
1 answer

How to convert into refined type?

I am using the library https://github.com/fthomas/refined and would like to convert java.util.UUID to refined's Uuid. How to convert java.util.UUID to refined's Uuid? Update I have the following http routes: private val httpRoutes: HttpRoutes[F] =…
softshipper
  • 32,463
  • 51
  • 192
  • 400
2
votes
1 answer

How to ensure type safety with Scala's Refined library when using the same predicate for refinement

I am new to scala and the refined library but I am trying to create two refined types based on UUIDs. In order to do so, I did this (Note: Uuid in this case comes from eu.timepit.refined.string.Uuid): type UuidPredicate = Uuid type UuidA = String…
stigward
  • 127
  • 13
2
votes
1 answer

decoder for refined type when using circe with Http4s

I am trying to use refined types for a case class but couldn't figure out how the encoder will actually work. For json parsing circe is used with https4s library. type AgeT = Int Refined Interval.ClosedOpen[0,100] type NameT = String Refined…
Abhi
  • 130
  • 11
2
votes
1 answer

Type refinements in Scala but without using refined

I am trying to create a HexString type based on String which should fulfill the condition "that it contains only hexadecimal digits" and I would like to have the compiler typecheck it for me, if possible. One obvious solution would be to use refined…
2
votes
1 answer

Not able to generate Circe Decoder when using with Cats and Refined Types

I wrote this code import io.circe._ import io.circe.refined._ import cats.data._ import cats.implicits._ import eu.timepit.refined.auto._ final case class Translation(lang: LanguageCode, name: ProductName) final case class Product(id: ProductId,…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
1
vote
0 answers

How to handle complex transformations in circe decode

Let's say that I have JSON {"some": "123"} And I want to create a custom decoder that would decode it to case class Test(some: Long Refined NonNegative = 0) So first I would need to decode JSON to String. Then turn String to Long which may throw…
Danil
  • 103
  • 7
1
vote
1 answer

Apply type projection to a refined type

Consider the following example: trait T3 trait T2{ type TT4 type TT3 <: T3 } trait T1{ type TT2 <: T2 } now I want to write a function the roughly speaking looks as def test[T <: T1](t: T#TT2{type TT4 = Int}#TT3) = println(t) //invalid…
Some Name
  • 8,555
  • 5
  • 27
  • 77
1
vote
3 answers

Scala circe deriveUnwrapped value class doesn't work for missing member

I am trying to decode a String value class in which if the string is empty I need to get a None otherwise a Some. I have the following ammonite script example: import $ivy.`io.circe::circe-generic:0.13.0`, io.circe._, io.circe.generic.auto._,…
Mihai Soloi
  • 373
  • 1
  • 12
1
vote
0 answers

Override typesafe config logger for using in my logback

There is a problem I have in my typesafe config + logback config + refined types + pure config When my config is not validated I get a raw exception without logback appender formatting use for example, my logback looks like How can I solve my…
1
vote
1 answer

Trying to keep types unwrapped when using refined

I am trying to use refined to create smart constructors based on primitives and avoid wrapping since same types might be used in large collections. Am I doing this right? Seems to work but a bit boilerplaty type ONE_Pred = = MatchesRegex[W.... …
DavedD
  • 58
  • 5
1
2