13

In the following code (from Functional Programming in Scala):

trait Functor[F[_]] {
  def map[A,B](fa: F[A])(f: A => B): F[B]
}

trait Monad[F[_]] {
  def unit[A](a: => A): F[A]
  def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
  def apply[A](a: => A): F[A]
}

I see the following warning:

[warn] C:\...\Monad.scala:3: higher-kinded type should be enabled
[warn] by making the implicit value scala.language.higherKinds visible.
[warn] This can be achieved by adding the import clause 'import scala.language.higherKinds'
[warn] or by setting the compiler option -language:higherKinds.
[warn] See the Scala docs for value scala.language.higherKinds for a discussion
[warn] why the feature should be explicitly enabled.
[warn] trait Functor[F[_]] {
[warn]               ^
[warn] C:\...\Monad.scala:7: higher-kinded type should be enabled
[warn] by making the implicit value scala.language.higherKinds visible.
[warn] trait Monad[F[_]] {

What's going on here? Note that I read this post, but didn't understand it.

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • See [SIP-18 - Modularizing Language Features](http://docs.scala-lang.org/sips/completed/modularizing-language-features.html). – senia Feb 08 '14 at 05:36

2 Answers2

12

See the doc for higherKinds.

Only where this flag is enabled, higher-kinded types can be written.

The level of abstraction implied by these design patterns is often a barrier to understanding for newcomers to a Scala codebase.

But note that the import is no longer required for recent 2.13. See the doc. The git commit links to an explanation that they weren't previously sure whether language support was stable.

Apparently, that means, "Some things that were previously inexpressible now just work."

The following stale joke is not relevant, but the missing hyphen for "higher-kinded" seems to indicate a certain lack of facility:

For some reason, no one has joked about:

Higher kinded types in Scala lead to a Turing-complete type system, where compiler termination is no longer guaranteed.

...though often it will just terminate early with a crash.

That's just a joke.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Thanks, @som-snytt. Is the `compiler termination is no longer guaranteed` a joke? – Kevin Meredith Feb 19 '14 at 01:13
  • Interesting: the [Qi](https://en.wikipedia.org/wiki/Qi_(programming_language)) language (and some of its [spinoffs](https://en.wikipedia.org/wiki/Shen_(programming_language))) is known to have a Turing complete (and thus most powerful possible by definition) type system—does Scala then offer an equivalently powerful (but perhaps not as tersely expressive) type system? – Erik Kaplun Mar 12 '14 at 21:39
  • 1
    doc leads to dead link. – WestCoastProjects Sep 04 '14 at 14:08
  • @javadba inline links ignore trailing dollar, fixed. – som-snytt Sep 04 '14 at 15:07
7

If you wish to suppress this warning, just add into your import section:

import scala.language.higherKinds
Dmitry Bespalov
  • 5,179
  • 3
  • 26
  • 33