Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

provide ability to write a NULL #28

Open
thurstonsand opened this issue Oct 24, 2016 · 6 comments
Open

provide ability to write a NULL #28

thurstonsand opened this issue Oct 24, 2016 · 6 comments

Comments

@thurstonsand
Copy link
Owner

None will always remove the write. provide a way to write down a NULL

@thurstonsand
Copy link
Owner Author

what about providing alternative to Option (with easy conversion)

sealed trait Nullable[+A] {
  def toOption: Option[A]
}
final case class NotNull[+A](x: A) extends Nullable[A] {
  def toOption: Option[A] = Some(x)
}
case object IsNull extends Nullable[Nothing] {
  def toOption: Option[Nothing] = None
}

object Nullable {
  implicit class NullableOption[+A](val opt: Option[A]) extends AnyVal {
    def toNullable: Nullable[A] = opt.fold[Nullable[A]](IsNull)(NotNull.apply)
  }
}

@thurstonsand
Copy link
Owner Author

usage could be

case class SomeTable(couldBeNull: Nullable[String])

val str: Option[String] = Some("a string")
SomeTable(str.toNullable)

or

case class SomeTable(couldBeNull: Nullable[String])
object SomeTable {
  def apply(couldBeNull: Option[String]) = new SomeTable(couldBeNull.toNullable)
}

SomeTable(Some("a string")) // just works

in the latter case, maybe it makes more sense to have a

implicit def option2nullable[A](opt: Option[A]): Nullable[A]) = opt.toNullable

case class SomeTable(couldBeNull: Nullable[String])
SomeTable(Some("a string")) // just works

@thurstonsand
Copy link
Owner Author

this is implemented. no docs yet, so leaving this here until then

@holinov
Copy link

holinov commented Sep 28, 2019

i feel like this going to loose type constraints on fields of data record
If i get a clean way to say "write down it an null" using None (why else am i trying TO UPDATE record in cassandra) - i'll need this possibility only in queues

@thurstonsand
Copy link
Owner Author

thurstonsand commented Sep 28, 2019

there were some cases in my project where "absence of data" and "data set to null" were semantically different, so I wanted to enable this. if you don't care about that difference, then just don't use Nullable

@thurstonsand
Copy link
Owner Author

also, thanks for reminding me about this. it shouldn't take much to add this documentation, so i should just do it

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants