2

Please view this classes :

class MainClass(firstName:String) extends GenericClass[(Int,String,Long)](firstName,"David")
class GenericClass[T](str1: String, str2: String)    

How scala can implement and map multiple type to single generic type ?
I mean GenericClass only have a Single type named GenericClass[T] but MainClass extends this and implement multiple type GenericClass[(Int,String,Long)]

I learn this code on slick orm :

class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") { ... }

can do like this in java language ?

2
  • 2
    (Int, String, Long) is just tuple syntax. Java has not build-in special syntax for tuples, but you can use special classes, which serve a similar purpose to tuples, like Pair<A,B> from javatuples. Nothing then stops you from doing things like GenericClass<Pair<String, String>> in Java. Commented May 24, 2019 at 9:25
  • If you want something similar to Slick in Java, look at jOOQ. And yes, because Java does not have built-in tuple types, it uses things like Record3<Integer, String, Long>. Commented May 24, 2019 at 9:41

1 Answer 1

5

GenericClass[T] has type parameter T. This parameter can be replaced by any type. In your case GenericClass[T] is parameterized by tuple (Int,String,Long) which is just a syntax sugar for Tuple3[Int,String,Long], so you can replace one with another and nothing changes:

class MainClass(firstName:String) extends GenericClass[Tuple3[Int,String,Long]](firstName,"David")

Note: Tuple3 is one type with three type parameters. Not multiple types.

As far as I know there is no equivalent of tuple class in java, but you can create it by your own or just use some librairies like javatuples

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.