GithubHelp home page GithubHelp logo

0pg / arrow-exact Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arrow-kt/arrow-exact

0.0 0.0 0.0 113 KB

Arrow Exact exposes a variation of exact types to work with in Arrow. It includes some boilerplate for commonly defined value class wrappers.

License: Apache License 2.0

Kotlin 100.00%

arrow-exact's Introduction

Module Arrow Exact

Arrow Exact allows you to use Kotlin's type system to enforce exactness of data structures.

Introduction

Exact allows automatically projecting smart-constructors on a Companion Object. We can for example easily create a NotBlankString type that is a String that is not blank, leveraging the Arrow's Raise DSL to ensure the value is not blank.

import arrow.core.raise.Raise
import arrow.exact.Exact
import arrow.exact.ExactError
import arrow.exact.ensure
import kotlin.jvm.JvmInline

@JvmInline
value class NotBlankString private constructor(val value: String) { 
  companion object : Exact<String, NotBlankString> {
    override fun Raise<ExactError>.spec(raw: String): NotBlankString { 
      ensure(raw.isNotBlank())
      return NotBlankString(raw)
    }
  }
}

We can then easily create values of NotBlankString from a String, which returns us a Either with the ExactError or the NotBlankString. We can also use fromOrNull to get a nullable value, or fromOrThrow to throw an ExactException.

note: Make sure to define your constructor as private to prevent creating invalid values.

fun example() {
  println(NotBlankString.from("Hello"))
  println(NotBlankString.from(""))
}

The output of the above program is:

Either.Right(NotBlankString(value=Hello))
Either.Left(ExactError(message=Failed condition.))

You can also define Exact by using Kotlin delegation.

@JvmInline
value class NotBlankString private constructor(val value: String) {
   companion object : Exact<String, NotBlankString> by Exact({
     ensure(it.isNotBlank())
     NotBlankString(it)
   })
}

You can define a second type NotBlankTrimmedString that is a NotBlankString that is also trimmed. ensureExact allows us to compose Exact instances and easily reuse the NotBlankString type.

@JvmInline
value class NotBlankTrimmedString private constructor(val value: String) { 
  companion object : Exact<String, NotBlankTrimmedString> { 
    override fun Raise<ExactError>.spec(raw: String): NotBlankTrimmedString { 
      ensure(raw, NotBlankString)
      return NotBlankTrimmedString(raw.trim())
    }
  }
}

arrow-exact's People

Contributors

nomisrev avatar renovate[bot] avatar ustitc avatar iliyangermanov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.