/safe

Scala type-safety helpers for Equality and working with Strings

Primary LanguageScalaOtherNOASSERTION

diff

Join the chat at https://gitter.im/xdotai/diff

Helpers to write type-safe Scala code using equality and Strings.

(Usage below.)

Build setup

SBT

"ai.x" %% "diff" % "1.2.0"

CBT

ScalaDependency( "ai.x", "diff", "1.2.0" )

imports

import ai.x.safe._

Feedback welcome

Are ~ or === conflicting with other libraries you use?

For now do

import ai.x.safe.{ SafeEquals => _, SafeStringAdd => _, _ }

But open a ticket if anything is getting in your way or you have ideas how to improve things, etc.

Contributions welcome

Found any other type-safety holes in the Scala Standard Library and know a workaround? Why not submit a PR :)?

Usage / Problems this library solves

import ai.x.safe._

== is not type-safe

Problem
val guests = List( chris, ... )
chris == guests.find( _ == chris )
// false
Solution: ===
val guests = List( chris, ... )
chris === guests.find( _ == chris )
// type error: Guest != Option[Guest]

.contains is not type-safe

Problem
val guests = List( chris, ... )
guests.contains( chrisId )
// false
Solution
val guests = List( chris, ... )
guests.safeContains( chrisId )
// error expected Guest, found GuestId

+ is not type-safe

Problem
"Hi " + guest + ", ..."
// Hi Guest(1,Some(Chris)), ...
Solution
"Hi " ~ guestName ~ ", ..."
// error expected Guest, found Option[Guest]

.mkString is not type-safe

"Hi " + names.mkString(" ") + ", ..."
// Hi Some(Chris) Some(Vogt), ...
Solution
"Hi " + names.safeMkString(" ") + ", ..."
// safeMkString is not a member of List[Option[String]]

s"..." is not type-safe

Problem
s"Hi $guestName, ..."
// Hi Some(Chris), ...
Solution
safe"Hi $guestName, ..."
// expected String, found Option[String]