I don’t get this factory method stuff :(
# announcements
d
I don’t get this factory method stuff :(
d
Simple example:
Copy code
class CSVParser<T>(val factory: (String) -> T, val file: String)
// inside parse method:
val tInstance: T = factory(line)

class MyClass(line: String)

// create CSVParser instance:
val parser = CSVParser<MyClass>(::MyClass, myFile)
In this example calling
factory
will call the
MyClass
constructor.
d
Great !!
This is really awesome
Thx a lot
🙇‍♂️ 1
Hmmm now compiler says : factory: “ constructor Kotlin reflection not available
I have added reflection lib now it works
c
You can avoid reflection completely, just pass
{ -> MyClass() }
as the factory
d
Just
::MyClass
and calling it should work without reflection library, too.