I have this JAVA-class:
public class PostgreSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
}
The only way I’m able to instantiate it from Scala is having a dummy-class:
class Fisk extends PostgreSQLContainer[Fisk]
val container = new PostgreSQLContainer[Fisk]("postgres:12.1")
Is the a better way?
Doesn’t new PostgreSQLContainer[PostgreSQLContainer]
work?
bmaso
March 3, 2020, 6:32pm
3
Looks right to me. The class is designed to be instantiated in a subclass, not in the base class.
Brian Maso
1 Like
If this is related to the TestContainers lib, you should have a look at
It makes using test containers from Scala very convenient, but it might
or might not match your use case.
No:
Error:(44, 58) class PostgreSQLContainer takes type parameters
final val postgreSQLContainer = new PostgreSQLContainer[PostgreSQLContainer]("postgres:12.1")
1 Like
In Scala 3 you could do:
object container extends PostgreSQLContainer[container.type]
Which is still more heavyweight than simple instantiation though. But it’s a bit weird to have an F-bounded concrete class.
BTW; I had to change the PostgreSQLContainerType
to:
class PostgreSQLContainerType[SELF](dockerImageName: String) extends PostgreSQLContainer[PostgreSQLContainerType[SELF]](dockerImageName)
The use-case in JAVA is to allow builder-pattern (having the with*()-methods implemented in abstract super-class), and return the concrete class:
new PostgreSQLContainerType[PostgreSQLContainerType[_]]("postgres:12.1")
.withDatabaseName("XXX").withUsername("XXX").withPassword("XXX")