jeudi 11 juin 2020

How to compare the return type of a method to a Scala native or TypeTag?

So, I'm using the Scala reflections library, and I'm trying to check if a method conforms to a given type. To simplify, I'm trying to check only its output.

What I have now is:

val returnType = methodSymbol.returnType
// returnType: reflect.runtime.universe.Type = java.lang.String

So, I can read it is a String, but it has this terrible type reflect.runtime.universe.Type. How on Earth can I compare check if this return type is a simple String? I even tried using TypeTags, which are simple enough, but to convert a Type to a TypeTag is such a monumental effort that I fail to believe such a simple task cannot be achieved with more simply.

So, how can I compare this to a String and simply get a boolean back? I thought of simply calling a toString() and trying to parse that back to a normal type, but that would be really disgusting to do on the code, IMO. Also, I cannot simply specify the method name, because I'm working on a list of methods, and more will come later.

I've seen some questions, and even this (in my opinion, absurdly complex) answer on how to convert a Type to a TypeTag, but, again, I'm baffled by the level of complexity for such a trivial task. I'm already thinking of pulling my scarce hair out. Help appreciated.





Aucun commentaire:

Enregistrer un commentaire