Hi, curious question, is there anyway in kotlin fu...
# kotlin-native
r
Hi, curious question, is there anyway in kotlin functions to force at least one of two attributes to be given? otherwise show a compile time error? example
Copy code
fun foo(default: Int, a: Int = default) {
 //todo
}

//good case
foo(a = 5)
foo(default = 5)

//compilation error "at least one of the attributes should be provided"
foo()
I can throw a run time error, but would be nice if it can be shown in compile time as well just like the
when
statement,
else
is only needed if not all enum cases are added
m
That's more #compiler than #kotlin-native. Or maybe #arrow can do something with this? I'm not aware of anything like this in the Kotlin language itself
e
it won't work quite the same way, but if you have overloads instead of defaults, you can ensure that at least one is provided at compile time
r
hmmm, makes sense actually! my intention was to use it for multiple attributes
Copy code
fun foo(default: Int, a: Int = default, b: Int = default, c: Int = default)
but following your comment, what I can do is
Copy code
fun foo(a: Int, b: Int, c: Int)

fun foo(default: Int, a: Int = default, b: Int = default, c: Int = default) {
  foo(a, b, c)
}
so if you don’t provide a default, you will have to use the second function and provide all the values