Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually creating a type tag

I have some Scala code with type tags that I need to make available for Java users. My problem boils down to solving the following:

Given two Scala type tags, how can I manually create a type tag for Tuple2[ A , B ]?

If it makes any difference my specific case is a bit simpler: I have one type tag for A and I need to create a type tag for Tuple2[ String , A ]

like image 797
harel Avatar asked Feb 18 '26 10:02

harel


1 Answers

Type tags are usually used as implicits, so the implicit system is very familiar with them. Just tell Scala what type you want and ask for it implicitly.

def tupleTag[A : TypeTag, B: TypeTag]: TypeTag[Tuple2[A, B]] = implicitly

If it can be instantiated, Scala will do so. If it can't, it's a compiler error. In this case, since A and B are already concrete (in that they have tags), Tuple2[A, B] will always be concrete as well.

like image 193
Silvio Mayolo Avatar answered Feb 20 '26 03:02

Silvio Mayolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!