Scala: function with a common return type

I have the following code, which I hope to have a common return type for my function:

object myUtility {

  def myFunction(input1:String, input2:String, returnType: T): T = {

  :
  :
}

What should be the correct syntax and what should I import to achieve this? Thank you very much!

+4
source share
2 answers

You do not declare what Tis a type parameter in a method. You need to do this by adding [T]values ​​in front of the parameter list:

def myFunction[T](input1:String, input2:String, returnType: T): T = ...

(Also, what for the parameter returnTypefor? You do not need this if your only intent was to declare a return type).

+5
source

, , . - ...

  def fxn[T](x : T) : T = {
    x
  }
+1

Source: https://habr.com/ru/post/1609701/


All Articles