Type information in the Scala REPL

后端 未结 2 1351
离开以前
离开以前 2021-01-25 00:54

If I\'m using the F# interpreter, I can define a simple function like this:

> // Function to check if x is an integer multiple of y
> let multipleOf x y =          


        
相关标签:
2条回答
  • 2021-01-25 01:31

    Yuck! This is one of those occasions where the solution to a question occurs to you just as you're about to submit the question to StackOverflow. Hopefully someone will find it useful if I answer it here myself.

    It turns out that Scala will play ball on providing type information for functions as long as you tell it to evaluate the function as a partially evaluated function! In other words, the following does the trick:

    scala> multipleOf _
    res0: (Int, Int) => Boolean = <function2>
    

    In other words, the REPL gives you information about the type of a function only if you reevaluate the function as a partially evaluated version of itself. This seems significantly less than optimal. ;-)

    Perhaps somebody can mention in the comments why it's sane for Scala to approach things this way?

    0 讨论(0)
  • 2021-01-25 01:47
    scala> val f = (i: Int, j: Int) => i % j == 0
    f: (Int, Int) => Boolean = <function2>
    
    scala> f
    res2: (Int, Int) => Boolean = <function2>
    
    scala> def multipleOf(x: Int, y: Int) = x % y == 0
    multipleOf: (x: Int, y: Int)Boolean
    
    scala> :type multipleOf(_, _)
    (Int, Int) => Boolean
    
    0 讨论(0)
提交回复
热议问题