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

fix: Ambiguous given instance inside RefinedTypeOps #180

Merged
merged 1 commit into from
Oct 8, 2023

Conversation

Iltotore
Copy link
Owner

@Iltotore Iltotore commented Oct 6, 2023

Closes #178

@ajaychandran can you check if this PR solves your problem?

@ajaychandran
Copy link
Contributor

This should work (waiting for the nightly version) but perhaps it would be better to grant access to the given instance (to avoid re-synthesis)?

@Iltotore
Copy link
Owner Author

Iltotore commented Oct 7, 2023

perhaps it would be better to grant access to the given instance

If we do that, the given instance would leak. For example, this code would compile:

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.Positive

opaque type Temperature = Int :| Positive
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]
//No explicit iron import
import foo.Temperature

summon[RuntimeConstraint[Int, Positive]] //Works

which is not the expected behaviour. Also I'm afraid this would cause other ambiguous given errors:

opaque type Temperature = Int :| Positive
object Temperature extends RefinedTypeOps[Int, Positive, Temperature]

opaque type Moisture= Int :| Positive
object Moisture extends RefinedTypeOps[Int, Positive, Moisture]
import foo.{Moisture, Temperature}

summon[RuntimeConstraint[Int, Positive]] //Ambiguous given errors: both Moisture.rtc and Temperature.rtc are eligible

(to avoid re-synthesis)

the inline def does not re-synthetize nor add any overhead on top of the protected given RuntimeConstraint. The method call is erased after compilation.

@ajaychandran
Copy link
Contributor

(to avoid re-synthesis)

I am using RuntimeConstraint to define typeclass instances for constrained opaque types. These instances need to be defined explicitly to work with Scala 3 auto derivation (for types using the constrained type). Having access to the RuntimeConstraint within the companion object would avoid re-synthesis.

I think protected access will support mine and the other use cases. Right?

@ajaychandran
Copy link
Contributor

From the workaround in the ticket,

type IsWhole = GreaterEqual[0]
opaque type Whole <: Int = Int :| IsWhole
object Whole extends RefinedTypeOps[Int, IsWhole, Whole]:

  given Parse[Whole] =
    summon[Parse[Int]].refineParse(using rtc)

I would like to avoid having to specify the given instance (using rtc) and reuse the one from RefinedTypeOps.

  given Parse[Whole] =
    summon[Parse[Int]].refineParse

@Iltotore
Copy link
Owner Author

Iltotore commented Oct 7, 2023

You shouldn't have to. That's why the protected is given

@Iltotore
Copy link
Owner Author

Iltotore commented Oct 7, 2023

This code compiles with this PR:

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.GreaterEqual

trait Parse[A]:

  def refineParse[C](using contraint: RuntimeConstraint[A, C]): Parse[A :| C] = ???

given Parse[Int] = new Parse[Int]{}

type IsWhole = GreaterEqual[0]
opaque type Whole <: Int = Int :| IsWhole
object Whole extends RefinedTypeOps[Int, IsWhole, Whole]:

  given Parse[Whole] = summon[Parse[Int]].refineParse

@ajaychandran
Copy link
Contributor

Thanks for checking.

I was getting compiler errors (something about deferred inline) with inline instances but that could have been my code.

@Iltotore
Copy link
Owner Author

Iltotore commented Oct 8, 2023

No problem. Feel free to open a new discussion or issue if you have hard time dealing with this error.

@Iltotore Iltotore merged commit 5822995 into main Oct 8, 2023
2 checks passed
@Iltotore Iltotore deleted the fix/ambiguous-given branch October 30, 2023 08:01
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.

Ambiguous RuntimeConstraint given instances in RefinedTypeOps
2 participants