Skip to content

Conversation

@sjrd
Copy link
Owner

@sjrd sjrd commented May 9, 2019

This design removes one case in USome.apply().

However, it does require the public object UNone to extend the private[uoption] class WrappedNone, a design I'm not really fond of.

This design removes one case in `USome.apply()`.
@dwijnand
Copy link

However, it does require the public object UNone to extend the private[uoption] class WrappedNone, a design I'm not really fond of.

How about:

diff --git src/main/scala/uoption/package.scala src/main/scala/uoption/package.scala
index a153621..02b5ccf 100644
--- src/main/scala/uoption/package.scala
+++ src/main/scala/uoption/package.scala
@@ -14,11 +14,12 @@ package object uoption {
     override def toString(): String = stringRepr
   }

-  object UNone extends WrappedNone(0, null) {
+  type UNone <: AnyRef
+  val UNone: UNone = new WrappedNone(0, null) {
     override def toString(): String = "UNone"
-  }
+  }.asInstanceOf[UNone]

-  type UOption[+A] >: UNone.type <: AnyRef
+  type UOption[+A] >: UNone <: AnyRef

   object UOption {
     @inline // only for Scala.js?

?

private[uoption] val wrap: WrappedNone = new WrappedNone(1, this)

object UNone extends WrappedNone(0, null) {
override def toString(): String = "UNone"

This comment was marked as resolved.

@sjrd
Copy link
Owner Author

sjrd commented May 21, 2019

val UNone: UNone

Unfortunately that increases the amount of instructions necessary to load the instance. It's a module-load (i.e., a static field read) plus a field access, instead of just a module-load.

Moreover, it changes the type inferred for UNone, as it will be UNone instead of UNone.type. I'm afraid of the consequences of such a change.

@dwijnand
Copy link

I see what you mean. UNone's .type is UNone, but the UNone type isn't a singleton. But I'm not sure what consequence you're afraid of.

@sjrd
Copy link
Owner Author

sjrd commented May 21, 2019

But I'm not sure what consequence you're afraid of.

Me neither, which is why I'm afraid of them. ;)

private[uoption] val unwrap: WrappedNone // null when this is UNone
) {
lazy val wrap: WrappedNone =
private[uoption] lazy val wrap: WrappedNone =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, are these required with the class being private[uoption]?

Is this in case you forget to hide these if you make the class non-private?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants