Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dealias type when type parameters inference occurs #23242

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

Open
Alex1005a wants to merge 2 commits into scala:main
base: main
Choose a base branch
Loading
from Alex1005a:dealias-in-typeparams

Conversation

Copy link
Contributor

@Alex1005a Alex1005a commented May 22, 2025

The code below displays the type parameter [Res] =>> TypeAl[F, String, Res] for the toFunctorOps implicit conversion.

import cats.Functor
import cats.syntax.all.*
type Result[F[_], Err, Res] = F[Either[Err, Res]]
def test[F[_]: Functor](x: Result[F, String, Int]) = {
 x.map(identity) // error here
}

To fix this, you can do dealias if the type before dealias does not have the same arity as the constructor with which unification occurs, but the type after dealias does have the same arity. Then the type parameter F will be inferred.

@Alex1005a Alex1005a marked this pull request as ready for review May 23, 2025 19:05
@odersky odersky requested a review from smarter June 5, 2025 11:26
Copy link
Contributor

odersky commented Jun 5, 2025

I think @smarter would be the best person to review this. Guillaume, can you take a look? You have thought most about dealiasing and type inference.

@odersky odersky assigned smarter and unassigned odersky Jun 5, 2025
Copy link
Contributor

@smarter @som-snytt
Have you maybe managed to take a look?

Copy link
Contributor

som-snytt commented Aug 27, 2025
edited
Loading

I'll learn something by taking a look. I recently removed dealiasing from Scala 2's typedNew and also contended in Scala 3 with the type of new A.C for C[T] where the unused check sees type [T] =>> this.A.C[T] at #23699

Edit: apparently I already have a branch review/23242...

Gedochao reacted with thumbs up emoji

Copy link
Member

smarter commented Aug 27, 2025

Sorry for the late reply! So having type aliases influence type inference here was a conscious choice so that users could customize higher-order unification by defining their own type aliases. The logic proposed in this PR is sound, but I worry it's making the rules around type inference more complex. An alternative here would be to use a curried type alias to get unification to do exactly what you want:

//> using dep org.typelevel::cats-core:2.13.0
import cats.Functor
import cats.syntax.all.*
type Result[Err, Res] = [F[_]] =>> F[Either[Err, Res]]
def test[F[_]: Functor](x: Result[String, Int][F]) =
 x.map(identity) // ok

Copy link
Contributor

Thanks for the timely comment (just back from grocery shopping). This morning I got up to speed on the code and suspected there might be a user solution, but I'm not used to writing the funny arrow. I thought the point was that "unification", as a convention, already does what I would expect.

I saw that the PR flips canInstantiate in a way that seems obvious. I'll see if I have an opinion about the tweak. Rules for type inference only matter when it seems to infer the wrong thing (as in this case).

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

@som-snytt som-snytt Awaiting requested review from som-snytt

@odersky odersky Awaiting requested review from odersky

@smarter smarter Awaiting requested review from smarter

At least 1 approving review is required to merge this pull request.

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /