Questions tagged [single-abstract-method]

11 questions
4
votes
2 answers

Difference between SAM and anonymous functions

I have recently stumbled upon SAM (single abstract method) concept in Java. I program in Scala and to me, they look the same as anonymous functions (or lambdas), however my IntelliJ suggests me to either use SAM or lambdas in different…
Shibalicious
  • 288
  • 2
  • 4
  • 14
4
votes
2 answers

Ambiguous overload even one is more specific

I have two definition of foo, and one of them is more supposedly specific def foo(f: java.util.function.ToIntFunction[String]) = println("foo1") def foo[T](f: String=>T) = println("foo2") //def foo[T](f: String=>T)(implicit d: DummyImplicit) =…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
3
votes
3 answers

Does scala cache conversions to functional interfaces

Scala 2.12 can automatically convert a lambda expression to an interface. E.g, I'm using: import org.apache.kafka.common.serialization.{Deserializer, Serde, Serializer} import scalapb.GeneratedMessageCompanion class ProtoSerde[A <:…
3
votes
1 answer

Prevent a Scala trait from qualifying as a SAM type

I have a trait Modifier[-A] extends (A => Unit). However, Scala 2.12 magic SAM syntax silently converts any lambda literal like el => foo into a Modifier because Modifier qualifies as a SAM type (rules for applicability of this syntax are here). I…
Nikita
  • 2,924
  • 1
  • 19
  • 25
2
votes
1 answer

Why scalac gets confused my SAM type not implementing Function when it overloads 'andThen'?

Tested with 2.13, but I presume it's been like that since 2.12 and I just had not encountered this type of problem before: trait Extractor[-X, +Y] { def optional :X => Option[Y] = apply def apply(x :X) :Option[Y] def…
Turin
  • 2,208
  • 15
  • 23
1
vote
2 answers

Why does this Scala code pass type checking?

Consider the following Scala code. object Q{ trait C{ def f(x: Int) : Int } def applyTo3(c: C) = c.f(3) def main(args: Array[String]) = println(applyTo3(x => x+1)) } This looks like it shouldn't compile: the function applyTo3 expects…
Gavin Lowe
  • 121
  • 1
  • 4
1
vote
1 answer

Why do I need to put parenthesis after the name of the interface when creating an instance of a SAM interface in Kotlin?

According to Kotlin's documentation on SAM Conversions, the syntax for creating instances of SAM interfaces goes like this: val runnable = Runnable { println("This runs in a runnable") } It can be seen that there are no parenthesis after Runnable,…
1
vote
1 answer

Scala: single abstract methods without a parameter

With SAM types, we can have: trait Sam { def foo(i: Int): String } val sam : Sam = _.toString What if my abstract method doesn't have a parameter?
Maths noob
  • 1,684
  • 20
  • 42
1
vote
1 answer

Strange SAM rules with functions

What are exact rules for a function to be considered for Single Abstract Method conversion? In the following code there are two very similar cases, one is compiled, second is not, when compiled with Scala 2.12.4: trait SAM { def apply(a: Int, b:…
Suma
  • 33,181
  • 16
  • 123
  • 191
0
votes
0 answers

Scala Single Abstract Method & Intellij

I'm just getting use to Scala Single Abstract Method which I did not know well, until I went through several blogs on Type Class. I am able after few minutes to figure out what's going on when I see it. I wonder if there is a way in Intellij to…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
0
votes
1 answer

Array SAM Scala not allowed

It seems you can not initialize an Array with a SAM syntax. When I try the following... trait A { def num(): Int } trait B extends A trait C extends A val nums: Array[A] = Array(() => 5) I get the following error... :12: error: type…
uh_big_mike_boi
  • 3,350
  • 4
  • 33
  • 64