vendredi 24 février 2017

Compile time structural typing of close method

I've got the following helper method in my project:

def close(c: Closeable) {
  Option(c).foreach(s => Try(c.close))
}

I've got some classes that have a close method but do not implement Closeable. If I change the helper method to use structural types I can still use it on these classes:

def close(c: {def close()}) {
  Option(c).foreach(s => Try(c.close))
}

However this introduces use of reflection which is something that I'd like to avoid in runtime.

Is there a way to use something similar to structural typing without inducing runtime reflection?

I.e in the same way Shapeless allows generic access to fields, maybe implicit parameters + macros could be used to access methods in the same way?





Aucun commentaire:

Enregistrer un commentaire