All Projects → tabdulradi → nullable

tabdulradi / nullable

Licence: Apache-2.0 license
Makes `A | Null` work with for-comprehensions

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to nullable

why-roguelike
A multiplayer ASCII roguelike
Stars: ✭ 17 (-5.56%)
Mutual labels:  dotty
Petals
A project to help restaurants in post covid world made with Flutter, VueJs and NodeJs.
Stars: ✭ 17 (-5.56%)
Mutual labels:  null-safety
atom-ide-scala
Scala & Dotty support for Atom IDE (🧟‍♂️ zombie repo)
Stars: ✭ 47 (+161.11%)
Mutual labels:  dotty
NDScala
N-dimensional arrays in Scala 3. Think NumPy ndarray, but type-safe over shapes, array/axis labels & numeric data types
Stars: ✭ 37 (+105.56%)
Mutual labels:  dotty
tf-dotty
Shape-safe TensorFlow in Dotty
Stars: ✭ 42 (+133.33%)
Mutual labels:  dotty
effpi
Verified message-passing programs in Dotty
Stars: ✭ 42 (+133.33%)
Mutual labels:  dotty
Inkuire
Hoogle-like searches for Scala 3 and Kotlin
Stars: ✭ 70 (+288.89%)
Mutual labels:  dotty
aestheticDialogs
📱 Flutter Plugin for 💫fluid, 😍beautiful, 🎨custom Dialogs
Stars: ✭ 19 (+5.56%)
Mutual labels:  null-safety
dart sealed
Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
Stars: ✭ 16 (-11.11%)
Mutual labels:  null-safety
web portfolio
My web portfolio built with Flutter
Stars: ✭ 18 (+0%)
Mutual labels:  null-safety
scala-3-crash-course
Scala 3 workshop presenting the top new features of the language.
Stars: ✭ 34 (+88.89%)
Mutual labels:  dotty
snq
A utility function to avoid type errors when traversing over arrays and object properties.
Stars: ✭ 30 (+66.67%)
Mutual labels:  null-safety
intent
Test framework for Dotty
Stars: ✭ 14 (-22.22%)
Mutual labels:  dotty
either
Elm Either
Stars: ✭ 24 (+33.33%)
Mutual labels:  union-types
Dotty
The Scala 3 compiler, also known as Dotty.
Stars: ✭ 4,836 (+26766.67%)
Mutual labels:  dotty
scala3-cross.g8
No description or website provided.
Stars: ✭ 12 (-33.33%)
Mutual labels:  dotty
dart meta types
a code gen solution for defining sealed classes, data classes, and enum classes for dart.
Stars: ✭ 25 (+38.89%)
Mutual labels:  union-types
dart-emoji
A light-weight Emoji 📦 for Dart & Flutter with all up-to-date emojis written in pure Dart 😄 . Made from 💯% ☕ with ❤️!
Stars: ✭ 16 (-11.11%)
Mutual labels:  null-safety
Home-Fi
Home Automation App using Flutter, Adafruit IO & Esp32 dev board.
Stars: ✭ 60 (+233.33%)
Mutual labels:  null-safety

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.3.0"

0.3.0 is published for Scala 3 RC2 only ( If you are still in RC1, please use 0.2.0 instead)

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].