score:8
With a little push and pull
trait Foo {
def bar(a:String): String
def bar(a:String)(implicit di: DummyImplicit): Int
}
class F extends Foo {
def bar(a: String): String = "Hello"
def bar(a: String)(implicit di: DummyImplicit): Int = 1
}
object Demo extends App {
val f = new F()
val s: String = f.bar("x")
val i: Int = f.bar("x")
println(s)
println(i)
}
you can tweak it, using a DummyImplicit
(to get around "Method is defined twice") and explicit typing (to pick one of the methods).
score:2
From wiki: http://en.wikipedia.org/wiki/Function_overloading
Rules in function overloading
* The overloaded function must differ either by the arity or data types.
* The same function name is used for various instances of function call.
Only having different return types does not count as function overloading, and is not allowed.
score:3
I can't really tell why it would be useful, but you could do this:
scala> object Foo {
| trait BarImpl[T] { def apply(str: String): T }
| implicit object barInt extends BarImpl[Int] { def apply(str: String) = 1 }
| implicit object barBoolean extends BarImpl[Boolean] { def apply(str: String) = true }
| def bar[T](str: String)(implicit impl: BarImpl[T]) = impl(str)
| }
defined module Foo
scala> Foo.bar[Int]("asdf")
res8: Int = 1
scala> Foo.bar[Boolean]("asdf")
res9: Boolean = true
score:6
On the JVM the return type of a method is not a part of a method signature. You have to give different method names or parameters. From Oracle Docs:
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
What you are tryng to do is called Method overloading and Oracle says the following:
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Cause Scala also compiles to JVM byte code, rules are the same
Source: stackoverflow.com
Related Query
- Scala trait same method and argument with different return types
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- Scala Ambiguous reference with same signature and argument types
- Consuming and API with Scala Dispatch Same method two different JSON
- In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?
- Cannot specialize a Scala method with specializable trait as return type
- How to combine the same function for different types of argument in scala
- Scala Case Class with Different Argument Types
- Scala and Hive: best way to write a generic method that works with all types of Writable
- F-bounded types and methods with type parameters at argument and return sites
- scala return types with traits and concrete classes
- Scala method which accepts multiple different objects, all of which extend the same trait
- Mixing trait and base class with same method
- scala pattern matching on object type and method accepting argument of many types
- Scala design pattern - sealed trait with two generics, children method having same type object in arguments list
- Scala return types with foreach and map
- Scala map with keys of same type but with irregular types for values and subtypes of values themselves being irregular
- Generate derived trait with same methods but without first parameter in each method in Scala
- How to return different future types in scala for a method
- How can we mock scala method with generic return type and implicit parameters?
- How to have child classes override a parent method but with collection of different element types as parameter in Scala
- Can Scala method that returns a trait returns method with the same type as trait?
- Defining a trait with a method that return a value of different numeric type
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- Difference between using App trait and main method in scala
- Functions are contravariant in their argument types and co-variant in their return types
- How to stub a method call with an implicit matcher in Mockito and Scala
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- Why can a method returning Unit be overridden with method returning String when return types are not explicitly given?
- Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
More Query from same tag
- Saving a Pipeline with DecisionTreeModel Spark ML
- Scala - case class with 100 fields (StackOverflowError)
- How to get active socket connection count in Play Framework?
- Does Jackson use stable binary serialization?
- Futures executed in order although there is no dependency
- Race condition detected when compiling Scala file programmatically
- Scala parameter type explanation
- Play Scala - Native access
- Calculations before extending class
- How to get business days between two dates in scala
- Java/Scala best way to get difference between two DateTime objects as List of DateTime
- Error when performing an inner join on Spark 2.0.1 DataFrame
- Using Class Methods in Spark RDD Operations Returns Task not serializable Exception
- scala "Illegal start of simple expression" in for comprehension with if
- Akka: testing monitoring\death watch
- mapping a Map object to an Option[Iterable[Row]] to derive an Option[List[Address]]
- Instantiate Actor System for my code design
- Spark Data frame select nothing
- How to set options for the sbt idea plugin in Play2 framework?
- uPickle and ScalaJS: sealed trait serialisation
- Enriching object with mixin trait
- How to decrypt AWS KMS cipher with AWS Encryption SDK with Java or AWSKmsClient
- Use generic type to turn off logic in datapath (Chisel)
- Android/Scala project in IntelliJ 14 compiles, but crashes when launched not finding Scala class
- Maximum amount of tasks running in parallel?
- HiveContext - unable to access hbase table mapped in hive as external table
- How to test a fire-and-forget WSClient request in Play
- Is there a way to know the context of an Akka actor?
- Trying to integrate mongoDB and spark, keep having errors related to "could not find or load class"
- Function composition dynamically