Is it possible to do something like this? fun foo...
# getting-started
a
Is it possible to do something like this? fun foo(bar: (Int) -> String, block: () -> Unit) { // Some code } fun test() { foo { bar(5) // This is currently not valid but I need it to be } }
p
What is it you're trying to achieve? Do you want a specific function to only be callable within the
block
lambda pass into your function?
Something like this: https://pl.kotl.in/y8SqtzEQy
a
There might be a better way, but I was able to modify that to make it work for my needs
What I'm trying to do is sort of a parameterized test, but in pure kotlin without junit5. I just want each test to run twice but using a different version of a class (same interface). So I need a way to construct the class in each test. So bar in this case is actually needs to call something like getInstance(0, "123") where 0=classA and "123" is the constructor args foo just runs block() in a loop with i = 0,1
p
why not something like this? https://pl.kotl.in/MwK72EVNA
a
How would this work with constructing multiple instances of the class in each test that have a unique constructor parameter?
The test basically can't construct the class, so it needs access to functions that mirror the constructors, and somewhere else knows which instance to construct
p
Then you're probably better of sticking to my first example, where
FooScope
would be your instance factory
a
Okay sounds good, I had modified yours to make it a class instead of object that took the i = 0,1 in constructor to know which instance to create
p
if the two classes in question have the same signature for the constructor, you can just pass that to the test code block: https://pl.kotl.in/ImQEc3c4Z