Single variablecan have multiple types of data

I want to create a function that takes one variable but the variable can be either a string or a seq[Ints] how would I go about this?

How would you use the input if it can be any of the two things?

Anyways, there are a couple of options.

  • Overloading
  • Asking for an Either[String, Seq[Int]]
  • If you are in Scala 3 ask for a union type String | Seq[Int].
  • Create your own little ADT for both cases.
  • Using a typeclass
  • Using the magnet pattern.
  • Using structural types.

Overloading is the most common solution.

Often you have a “main” overload and the others invoke it to do the actual work, but not always.

Example of overloading:

scala 2.13.6> object O {
            |   def fn(s: String) = "it's a string"
            |   def fn(xs: Seq[Int]) = "it's some ints"
            | }
object O

scala 2.13.6> O.fn("")
val res0: String = it's a string

scala 2.13.6> O.fn(List(1, 2, 3))
val res1: String = it's some ints

so basically initiate the function twice once for strings and one for seq[double] also it’s used with an if statement thanks for the help!!