(Scala initial question)
I am trying to write a program in Scala that performs a series of checks of source files in C ++, and I want to implement optional audit logging.
The following works fine for one check:
val headerFiles = files.filter(_.matches(".*?\\.h$")) val headerGuardChecker = if(options.contains('verbose)) { new HeaderGuard with LoggingFileCheckerTrait } else { new HeaderGuard } headerFiles.foreach(h => if (! headerGuardChecker.check(new File(h))) println(h + " doesn't have a header guard"))
however, when I try to generalize this with generics:
def checker[T] = if(options.contains('verbose)) { new T with LoggingFileCheckerTrait } else { new T } val headerFiles = files.filter(_.matches(".*?\\.h$")) headerFiles.foreach(h => if (! checker[HeaderGuard].check(new File(h))) println(h + " doesn't have a header guard"))
I get compilation errors for two new statements, claiming that T is not a type. I believe this is caused by type erasure, but I have not found a way around this. Is there a way to do what I want to do?
Anthony berent
source share