https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Andrew

05/08/2022, 7:44 PM
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

phldavies

05/08/2022, 7:46 PM
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

Andrew

05/08/2022, 8:18 PM
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

phldavies

05/08/2022, 8:28 PM
why not something like this? https://pl.kotl.in/MwK72EVNA
a

Andrew

05/08/2022, 8:38 PM
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

phldavies

05/08/2022, 8:47 PM
Then you're probably better of sticking to my first example, where
FooScope
would be your instance factory
a

Andrew

05/08/2022, 8:52 PM
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

phldavies

05/08/2022, 8:59 PM
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
5 Views