Skip to content

Conversation

@Decel
Copy link
Contributor

@Decel Decel commented Jun 11, 2023

Currently, if we write:

def add(x: Int, y: Int) = x + y

val a = List(1, 2, 3)
val b = a.map(add)

We get a pretty confusing error message:

-- [E086] Syntax Error: ----------------------------------------------------
  |val b = a.map(add)
  |              ^^^
  |              Wrong number of parameters, expected: 1

Now, with -explained enabled it's:

-- [E086] Syntax Error----------------------------------------------------------
  |val b = a.map(add)
  |              ^^^
  |              Wrong number of parameters, expected 1, but found 2
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |
  | Expected pattern: Int => Int
  | Found pattern   : (x: Int, y: Int) => add(x, y)
   -----------------------------------------------------------------------------

@Decel Decel marked this pull request as draft June 11, 2023 17:28
@som-snytt
Copy link
Contributor

For comparison,

scala> add(1,2,3)
-- Error: --------------------------------------------------------------------------------------------------------------
1 |add(1,2,3)
  |        ^
  |        too many arguments for method add: (x: Int, y: Int): Int
1 error found

and

scala> val b = a.map(add)
                     ^
       error: type mismatch;
        found   : (Int, Int) => Int
        required: Int => ?

scala> add(1,2,3)
               ^
       error: too many arguments (found 3, expected 2) for method add: (x: Int, y: Int): Int

scala>

There are weird applications where it's helpful to count the args for the user.

@Decel
Copy link
Contributor Author

Decel commented Jun 12, 2023

scala> val b = a.map(add)
                     ^
       error: type mismatch;
        found   : (Int, Int) => Int
        required: Int => ?

The error message has changed (I think for the worse):

3.3.0:
https://scastie.scala-lang.org/hEmQZ9nLQyyIvuhBDUeDjQ
3.3.1-RC1:
https://scastie.scala-lang.org/noU1kBxLRdKch6n2CgyCig

But thanks for the feedback, I'll apply the changes a bit later.

@som-snytt
Copy link
Contributor

The main gripe has progressed:

Welcome to Scala 3.4.0-RC1-bin-SNAPSHOT-git-d96e9e4 (21, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> def add(x: Int, y: Int) = x + y
def add(x: Int, y: Int): Int

scala> val a = List(1, 2, 3)
val a: List[Int] = List(1, 2, 3)

scala> val b = a.map(add)
-- [E007] Type Mismatch Error: -----------------------------------------------------------------------------------------
1 |val b = a.map(add)
  |              ^^^
  |              Found:    (Int, Int) => Int
  |              Required: Int => Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

scala> add(1,2,3)
-- Error: --------------------------------------------------------------------------------------------------------------
1 |add(1,2,3)
  |        ^
  |        too many arguments for method add: (x: Int, y: Int): Int
1 error found

Required: Int => Int is too narrow and Scala 2 is more correct, but maybe not more helpful.

@som-snytt
Copy link
Contributor

Actually, it gives you a count if it tries to check the type of the formal (hence, not if you supply param types):

scala> List(1,2,3).map((x, y) => x+y)
-- [E086] Syntax Error: ------------------------------------------------------------------------------------------------
1 |List(1,2,3).map((x, y) => x+y)
  |                ^^^^^^^^^^^^^
  |                Wrong number of parameters, expected: 1
  |---------------------------------------------------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The function literal
  |
  |     (x, y) => x + y
  |
  | has 2 parameters. But the expected type
  |
  |     Int => Any
  |
  | requires a function with 1 parameters.
   ---------------------------------------------------------------------------------------------------------------------
1 error found

scala> List(1,2,3).map((x: Int, y: Int) => x+y)
-- [E007] Type Mismatch Error: -----------------------------------------------------------------------------------------
1 |List(1,2,3).map((x: Int, y: Int) => x+y)
  |                ^^^^^^^^^^^^^^^^^^^^^^^
  |                Found:    (Int, Int) => Int
  |                Required: Int => Int
  |---------------------------------------------------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |
  | Tree: {
  |   def $anonfun(x: Int, y: Int): Int = x.+(y)
  |   closure($anonfun)
  | }
  | I tried to show that
  |   (Int, Int) => Int
  | conforms to
  |   Int => Int
  | but none of the attempts shown below succeeded:
  |
  |   ==> (Int, Int) => Int  <:  Int => Int  = false
  |
  | The tests were made under a constraint with:
  |  uninstantiated variables:
  |  constrained types: [B](f: Int => B): List[B], [B](f: Int => B): List[B]
  |  bounds:
  |      B
  |      B := Int
  |  ordering:
  |  co-deps:
  |  contra-deps:
   ---------------------------------------------------------------------------------------------------------------------
1 error found

Obviously, the first message is more helpful.

@som-snytt
Copy link
Contributor

Usual pluralism. 1 parameters

@som-snytt
Copy link
Contributor

Superseded by #18665

@som-snytt som-snytt closed this Dec 7, 2023
@som-snytt som-snytt removed their assignment Dec 7, 2023
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.

2 participants