Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

RestoreTo[F[_], G[_]] extends Lift[G[_], F[_]]. #122

Merged
merged 6 commits into from
Jan 30, 2020

Conversation

oskin1
Copy link
Collaborator

@oskin1 oskin1 commented Jan 22, 2020

Close #117

private[this] implicit val containsInstanceAny: Contains[Nothing, Any] =
new Contains[Nothing, Any] {
def set(s: Nothing, b: Any): Nothing = s
def extract(s: Nothing): Any = s.asInstanceOf[Any]
Copy link
Contributor

Choose a reason for hiding this comment

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

asInstanceOf is redundant
Also, may be this instance should be somewhere in optics module?

Copy link
Collaborator Author

@oskin1 oskin1 Jan 22, 2020

Choose a reason for hiding this comment

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

Maybe, but I haven't found more suitable place for such dummy instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think such instance can be abstracted to smth like:

def subtype[A, B >: A]: Contains[A, B] = PContains[A, B](identity)((x, _) => x)

and moved to tofu.optics.functions.
@Odomontois, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with moving, but I like the current non-allocating version more.

Copy link
Member

@Odomontois Odomontois Jan 23, 2020

Choose a reason for hiding this comment

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

Also, I believe such an instance would break Lens laws in general.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, sorry, my mistake.
Counterexample: for Contains[Boolean, Boolean] such instance, we have: extract(set(true, false)) = true

@@ -78,10 +79,12 @@ object ZioTofuInstance {
}
}

class ZioTofuErrorsToInstance[R, E, E1] extends ErrorsTo[ZIO[R, E, *], ZIO[R, E1, *], E] {
class ZioTofuErrorsToInstance[R, E, E1](implicit lens: Contains[E1, E])
Copy link
Member

Choose a reason for hiding this comment

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

It looks we need only Extract instance here

def extract(s: Nothing): Any = s
}

private[this] def zioErrorsToInstanceAny: ZioTofuErrorsToInstance[Any, Any, Nothing] = new ZioTofuErrorsToInstance[Any, Any, Nothing]
final def zioTofuErrorsToInstance[R, E, E1]: ZioTofuErrorsToInstance[R, E, E1] =
Copy link
Member

Choose a reason for hiding this comment

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

This instance is not sound anymore.
You can not transform ZIO[R, E1, A] to ZIO[R, E, A] without any prior knowledge about how to map E1 to E
I'm afraid trying to cast arbitrary E1 to Nothing would bring ClassCastException

Copy link
Member

Choose a reason for hiding this comment

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

I suppose best we can do here is a change E1 to Nothing explicitly for that instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Odomontois, you mean final def zioTofuErrorsToInstance[R, E]: ZioTofuErrorsToInstance[R, E, Nothing]?

@oskin1 oskin1 requested a review from Odomontois January 23, 2020 13:23
@@ -78,10 +79,12 @@ object ZioTofuInstance {
}
}

class ZioTofuErrorsToInstance[R, E, E1] extends ErrorsTo[ZIO[R, E, *], ZIO[R, E1, *], E] {
class ZioTofuErrorsToInstance[R, E, E1](implicit lens: Extract[E1, E])
Copy link
Member

Choose a reason for hiding this comment

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

So now the only use of the lens parameter is to Extract something from Nothing
This is perfectly legit and could not touch performance (since mapError is never applied).
But it looks like we need at least one additional instance , e.g.

final def zioTofuExtractErrorsInstance[R, E, E1: * Extract E1]: ZioTofuErrorsToInstance[R, E, E1] = 
  new ZioTofuErrorsToInstance

To justify this encoding.

@@ -56,4 +56,6 @@ object functions {
def some[A]: Subset[Option[A], A] = Subset[Option[A]](identity)(Some(_))

def none[A]: Subset[Option[A], Unit] = Subset[Option[A]](_ => Some(()))(_ => None)

def extractAnyFromNothing: Extract[Nothing, Any] = (s: Nothing) => s
Copy link
Member

Choose a reason for hiding this comment

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

Well, Extract[A, B] unlike Lens[A, B] is perfectly legal for any A <: B, so it would be better to see more general case here.

@oskin1 oskin1 requested a review from Odomontois January 27, 2020 15:12
@Odomontois Odomontois merged commit bf36c87 into tofu-tf:master Jan 30, 2020
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.

Function allowing to get back into exceptional context in RestoreTo
3 participants