typelevel / typelevel/cats

Scala 3 type inference related issue

Open
#4,043 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

Hi I don't know how to explain it in short and I'm also not sure whether it's a Scala3 issue. I reproduced it with the scala-cli. If you have it installed have a look at this gist. There are 2 flies and each has a Main method in different packages. You can run them with either

scala-cli https://gist.github.com/agilesteel/05482df2e1f420f762dec7696088552d -M good.Main

or

scala-cli https://gist.github.com/agilesteel/05482df2e1f420f762dec7696088552d -M bad.Main

The Good.scala file in there has no dependencies on cats-core. I defined my own trait Semigroup and my own instance for Option. I also defined a Maybe ADT with the new enum keyword and an instance for it as well.

enum Maybe[+A]:
  case Just(a: A)
  case Nothing

I noticed 2 things:

  1. Some(1) combine Some(2) compiles and works even though my instance is for Option. I don't have an instance for Some, which is surprising. So we don't need .some anymore?!
  2. Maybe.Just(1) combine Maybe.Just(2) compiles and works as expected since the enum keyword generates factories which return the Maybe type instead of the Maybe.Just type (as opposed to manual ADTs in Scala 2 for instance).

The Bad.scala file in there has a dependency on cats-core (2.6.1 but I doubt that the version matters much) and therefore I used the Semigroup from cats. I found the following:

Maybe.Just(1) combine Maybe.Just(2) // does not compile BUT IT SHOULD!
[error] ./Bad.scala:24:11: value combine is not a member of bad.Maybe.Just[Int]
[error]   println(Maybe.Just(1) combine Maybe.Just(2)) // does NOT compile
[error]

however

val maybe = Maybe.Just(1) // notice that there is no type ascription or any other type information
maybe combine Maybe.Just(2) // compiles!!!

I used Scala 3.1.0 but other Scala 3.x versions behaved the same.

My expectation is that at least the Maybe examples should compile, right? Why the Option examples work in Good.scala is a mystery to me, but I guess this one is not a cats issue.

I also created this issue https://github.com/lampepfl/dotty/issues/13948

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two scala-cli commands against the referenced gist and compare Good.scala with Bad.scala, focusing on the Maybe.Just and Option combine examples. Read the linked Dotty issue to determine whether the behavior belongs to Scala 3 or cats; done requires a confirmed scope and a reproducible resolution for the reported type-inference behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.