Skip to content

Instantly share code, notes, and snippets.

@halotukozak
Last active June 25, 2025 12:50
Show Gist options
  • Select an option

  • Save halotukozak/bc2b9a65bcb03bfb03bf1737054e72b3 to your computer and use it in GitHub Desktop.

Select an option

Save halotukozak/bc2b9a65bcb03bfb03bf1737054e72b3 to your computer and use it in GitHub Desktop.
package com.avsystem.commons
package analyzer
import org.scalatest.funsuite.AnyFunSuite
final class ImplicitFunctionParamsTest extends AnyFunSuite with AnalyzerTest {
test("regular parameter with function type should pass") {
assertNoErrors(scala"def goodMethod1(f: Int => String): Unit = ???")
}
test("regular parameter with partial function type should pass") {
assertNoErrors(scala"def goodMethod2(pf: PartialFunction[Int, String]): Unit = ???")
}
test("implicit parameter with non-function type should pass") {
assertNoErrors(scala"def goodMethod3(implicit s: String): Unit = ???")
}
test("implicit parameter with function type should fail") {
assertErrors(1, scala"def badMethod1(implicit f: Int => String): Unit = ???")
}
test("implicit parameter with function type in second parameter list should fail") {
assertErrors(1, scala"def badMethod2(x: Int)(implicit f: Int => String): Unit = ???")
}
test("implicit parameter with partial function type should fail") {
assertErrors(1, scala"def badMethod3(implicit pf: PartialFunction[Int, String]): Unit = ???")
}
test("implicit parameter with partial function type in second parameter list should fail") {
assertErrors(1, scala"def badMethod4(x: Int)(implicit pf: PartialFunction[Int, String]): Unit = ???")
}
test("regular class parameter with function type should pass") {
assertNoErrors(scala"class GoodClass1(f: Int => String)")
}
test("regular class parameter with partial function type should pass") {
assertNoErrors(scala"class GoodClass2(pf: PartialFunction[Int, String])")
}
test("implicit class parameter with non-function type should pass") {
assertNoErrors(scala"class GoodClass3(implicit s: String)")
}
test("implicit class parameter with function type should fail") {
assertErrors(1, scala"class BadClass1(implicit f: Int => String)")
}
test("implicit class parameter with function type in second parameter list should fail") {
assertErrors(1, scala"class BadClass2(x: Int)(implicit f: Int => String)")
}
test("implicit class parameter with partial function type should fail") {
assertErrors(1, scala"class BadClass3(implicit pf: PartialFunction[Int, String])")
}
test("implicit class parameter with partial function type in second parameter list should fail") {
assertErrors(1, scala"class BadClass4(x: Int)(implicit pf: PartialFunction[Int, String])")
}
// Define a SAM type (Single Abstract Method) for all SAM type tests
private val SamDefinition = {
//language=Scala
"""
|trait IntToString {
| def apply(i: Int): String
|}
|""".stripMargin
}
test("regular parameter with SAM type should pass") {
assertNoErrors(scala"$SamDefinition def goodMethod1(f: IntToString): Unit = ???")
}
test("implicit parameter with SAM type should pass") {
assertNoErrors(scala"$SamDefinition def goodMethod2(implicit f: IntToString): Unit = ???")
}
test("implicit parameter with SAM type in second parameter list should pass") {
assertNoErrors(scala"$SamDefinition def goodMethod3(x: Int)(implicit f: IntToString): Unit = ???")
}
test("regular class parameter with SAM type should pass") {
assertNoErrors(scala"$SamDefinition class GoodClass1(f: IntToString)")
}
test("implicit class parameter with SAM type should pass") {
assertNoErrors(scala"$SamDefinition class GoodClass2(implicit f: IntToString)")
}
test("implicit class parameter with SAM type in second parameter list should pass") {
assertNoErrors(scala"$SamDefinition class GoodClass3(x: Int)(implicit f: IntToString)")
}
test("regular parameter with Function2 type should pass") {
assertNoErrors(
scala"def goodMethod1(f: (Int, String) => Boolean): Unit = ???")
}
test("regular parameter with Function3 type should pass") {
assertNoErrors(
scala"def goodMethod2(f: (Int, String, Double) => Boolean): Unit = ???")
}
test("implicit parameter with non-function type should pass (multiple params context)") {
assertNoErrors(
scala"def goodMethod3(implicit s: String): Unit = ???")
}
test("implicit parameter with Function2 type should fail") {
assertErrors(1,
scala"def badMethod1(implicit f: (Int, String) => Boolean): Unit = ???")
}
test("implicit parameter with Function2 type in second parameter list should fail") {
assertErrors(1, scala"def badMethod2(x: Int)(implicit f: (Int, String) => Boolean): Unit = ???")
}
test("implicit parameter with Function3 type should fail") {
assertErrors(1, scala"def badMethod3(implicit f: (Int, String, Double) => Boolean): Unit = ???")
}
test("implicit parameter with Function3 type in second parameter list should fail") {
assertErrors(1, scala"def badMethod4(x: Int)(implicit f: (Int, String, Double) => Boolean): Unit = ???")
}
test("regular class parameter with Function2 type should pass") {
assertNoErrors(scala"class GoodClass1(f: (Int, String) => Boolean)")
}
test("regular class parameter with Function3 type should pass") {
assertNoErrors(scala"class GoodClass2(f: (Int, String, Double) => Boolean)")
}
test("implicit class parameter with non-function type should pass (multiple params context)") {
assertNoErrors(scala"class GoodClass3(implicit s: String)")
}
test("implicit class parameter with Function2 type should fail") {
assertErrors(1, scala"class BadClass1(implicit f: (Int, String) => Boolean)")
}
test("implicit class parameter with Function2 type in second parameter list should fail") {
assertErrors(1, scala"class BadClass2(x: Int)(implicit f: (Int, String) => Boolean)")
}
test("implicit class parameter with Function3 type should fail") {
assertErrors(1, scala"class BadClass3(implicit f: (Int, String, Double) => Boolean)")
}
test("implicit class parameter with Function3 type in second parameter list should fail") {
assertErrors(1, scala"class BadClass4(x: Int)(implicit f: (Int, String, Double) => Boolean)")
}
}
package com.avsystem.commons
package analyzer
import org.scalatest.funsuite.AnyFunSuite
final class ImplicitFunctionParamsTest extends AnyFunSuite with AnalyzerTest {
test("regular parameter with function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod1(f: Int => String): Unit = ???
|}
""".stripMargin)
}
test("regular parameter with partial function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod2(pf: PartialFunction[Int, String]): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with non-function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod3(implicit s: String): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with function type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod1(implicit f: Int => String): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with function type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod2(x: Int)(implicit f: Int => String): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with partial function type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod3(implicit pf: PartialFunction[Int, String]): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with partial function type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod4(x: Int)(implicit pf: PartialFunction[Int, String]): Unit = ???
|}
""".stripMargin)
}
test("regular class parameter with function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| class GoodClass1(f: Int => String)
|}
""".stripMargin)
}
test("regular class parameter with partial function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| class GoodClass2(pf: PartialFunction[Int, String])
|}
""".stripMargin)
}
test("implicit class parameter with non-function type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| class GoodClass3(implicit s: String)
|}
""".stripMargin)
}
test("implicit class parameter with function type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| class BadClass1(implicit f: Int => String)
|}
""".stripMargin)
}
test("implicit class parameter with function type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| class BadClass2(x: Int)(implicit f: Int => String)
|}
""".stripMargin)
}
test("implicit class parameter with partial function type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| class BadClass3(implicit pf: PartialFunction[Int, String])
|}
""".stripMargin)
}
test("implicit class parameter with partial function type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| class BadClass4(x: Int)(implicit pf: PartialFunction[Int, String])
|}
""".stripMargin)
}
// Define a SAM type (Single Abstract Method) for all SAM type tests
private val SamDefinition = {
//language=Scala
"""
|trait IntToString {
| def apply(i: Int): String
|}
|""".stripMargin
}
test("regular parameter with SAM type should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| def goodMethod1(f: IntToString): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with SAM type should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| def goodMethod2(implicit f: IntToString): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with SAM type in second parameter list should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| def goodMethod3(x: Int)(implicit f: IntToString): Unit = ???
|}
""".stripMargin)
}
test("regular class parameter with SAM type should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| class GoodClass1(f: IntToString)
|}
""".stripMargin)
}
test("implicit class parameter with SAM type should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| class GoodClass2(implicit f: IntToString)
|}
""".stripMargin)
}
test("implicit class parameter with SAM type in second parameter list should pass") {
assertNoErrors(SamDefinition +
//language=Scala
"""
|object whatever {
| class GoodClass3(x: Int)(implicit f: IntToString)
|}
""".stripMargin)
}
test("regular parameter with Function2 type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod1(f: (Int, String) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("regular parameter with Function3 type should pass") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod2(f: (Int, String, Double) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with non-function type should pass (multiple params context)") {
assertNoErrors(
//language=Scala
"""
|object whatever {
| def goodMethod3(implicit s: String): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with Function2 type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod1(implicit f: (Int, String) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with Function2 type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod2(x: Int)(implicit f: (Int, String) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with Function3 type should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod3(implicit f: (Int, String, Double) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("implicit parameter with Function3 type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|object whatever {
| def badMethod4(x: Int)(implicit f: (Int, String, Double) => Boolean): Unit = ???
|}
""".stripMargin)
}
test("regular class parameter with Function2 type should pass") {
assertNoErrors(
//language=Scala
"""
|class GoodClass1(f: (Int, String) => Boolean)
""".stripMargin)
}
test("regular class parameter with Function3 type should pass") {
assertNoErrors(
//language=Scala
"""
|class GoodClass2(f: (Int, String, Double) => Boolean)
""".stripMargin)
}
test("implicit class parameter with non-function type should pass (multiple params context)") {
assertNoErrors(
//language=Scala
"""
|class GoodClass3(implicit s: String)
""".stripMargin)
}
test("implicit class parameter with Function2 type should fail") {
assertErrors(1,
//language=Scala
"""
|class BadClass1(implicit f: (Int, String) => Boolean)
""".stripMargin)
}
test("implicit class parameter with Function2 type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|class BadClass2(x: Int)(implicit f: (Int, String) => Boolean)
""".stripMargin)
}
test("implicit class parameter with Function3 type should fail") {
assertErrors(1,
//language=Scala
"""
|class BadClass3(implicit f: (Int, String, Double) => Boolean)
""".stripMargin)
}
test("implicit class parameter with Function3 type in second parameter list should fail") {
assertErrors(1,
//language=Scala
"""
|class BadClass4(x: Int)(implicit f: (Int, String, Double) => Boolean)
""".stripMargin)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment