https://kotlinlang.org logo
#announcements
Title
# announcements
s

steenooo

12/04/2019, 5:47 PM
is it possible to do this more elegant? Let these perhaps be generated, instead of having to write them all out
s

streetsofboston

12/04/2019, 5:53 PM
Alas, not that I know. But you could do something like this:
Copy code
class MyException @JvmOverloads constructor(
    cause: Throwable? = null,
    message: String? = cause?.toString(),
    enableSuppression: Boolean = true,
    writableStrackTrace: Boolean = true
)Exception(message, cause, enableSuppression, writableStrackTrace)
Where the
@JvmOverloads
guarantees Java compatibility.
s

steenooo

12/04/2019, 5:56 PM
thanks! but this still isn't ideal It causes a problem when I don't put the message = there. It would ask for a Throwable instance
b

bezrukov

12/04/2019, 6:01 PM
because JvmOverloads generates constructor per default parameters prefix. In case above it generates four constructors () (throwable) (throwable, message) (throwable, message, enableSupression)
so calling KotlinPluginException("message without named argument") is not working because there is no
(message)
constructor
you could swap message and throwable, but then there will be no
(throwable)
constructor. So you need just add one more constructor by hands:
constructor(message: String) : this(null, message)
t

trevjones

12/04/2019, 9:53 PM
passing null as the cause in the constructor breaks the initCause function as well. not sure if you care