GithubHelp home page GithubHelp logo

tabdulradi / nullable Goto Github PK

View Code? Open in Web Editor NEW
22.0 5.0 1.0 42 KB

Makes `A | Null` work with for-comprehensions

License: Apache License 2.0

Scala 100.00%
scala allocation-cost union-types null-safety dotty

nullable's Introduction

Nullable

Enriches union types A | Null with an interface similar scala.Option making it usable inside for comprehensions

Usage

Add the following to your build.sbt

scalacOptions += "-Yexplicit-nulls"
libraryDependencies += "com.abdulradi" %% "nullable-core" % "0.4.0"

Features

Plays nice with for comprehensions

import com.abdulradi.nullable.syntax.*
val maybeA: Int | Null = 42
val maybeB: String | Null = "foo"

// Compiles fine
for 
  a <- maybeA 
yield 5

// Also compiles, no null pointer exceptions
for 
  a <- maybeA 
  b <- null
yield ()

// Compiles, and evaluates to null (because if condition doesn't match)
for 
  a <- maybeA 
  if (a < 0)
yield a

Familiar Option-like experience

maybeA.map(_ => 5)
maybeA.flatMap(_ => null)
maybeA.flatMap(_ => maybeB)
maybeA.fold(0)(_ + 1)
maybeA.getOrElse(0)
maybeA.contains(0)
maybeA.exists(_ == 0)
maybeA.toRight("Value was null")

Convert from/to Option

val optA = Some(42)
maybeA.toOption == optA
maybeA == optA.orNull

Prevents auto flattening

While Option[Option[A]] is a valid type, the equivalent A | Null | Null is indistinguishable from A | Null. So, the library ensures that map and flatMap are used correctly used at compile time.

The following examples don't compile

for a <- maybeA yield maybeB // Shows a compile time error suggesting to use flatMap instead
maybeA.flatMap(_ => 5) // Shows a compile time error suggesting message to use map instead
maybeA.map(_ => null) // Shows a compile time error suggesting to use flatMap instead
maybeA.map(_ => maybeB) // Shows a compile time error suggesting to use flatMap instead

Working with Generics

The following doesn't compile, as we can't prove A won't be Null

def useFlatMapWithNullableInScope[A](f: Int => A): A | Null = 
  maybeA.flatMap(f)

Solution: Propagate a Nullable instance and let the library check at usage site

def useFlatMapWithNullableInScope[A: Nullable](f: Int => A): A | Null = 
  maybeA.flatMap(f)

def useMapWithNotNullInScope[A: NotNull](f: Int => A): A | Null = 
  maybeA.map(f)

Lightweight version

If you only care about for-comprehension features, but not the rest of Option-like methods, we also offer a lightweight syntax

import com.abdulradi.nullable.forSyntax.*

for 
  a <- maybeA
  b <- maybeB
  res = a + b
  if (res % 2) == 0
yield res

The rest of the methods like fold, getOrElse, etc won't be in scope.

License & Acknowledgements

Since this library mimics the scala.Option behavior, it made sense to copy and adapt the documentation of it's methods and sometimes the implementation too. Those bits are copyrighted by EPFL and Lightbend under Apache License 2.0, which is the same license as this library.

nullable's People

Contributors

lolgab avatar tabdulradi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

lolgab

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.