bloder
06/30/2019, 1:47 PMfun handleError(e: Exception) if(e is NetworkException) {}
fun handleError(e: Exception) if(e is WrongUserException) {}
fun handleError(e: Exception) if(e is OtherException) {}
gildor
06/30/2019, 1:55 PMfun handleError(e: Exception) {
when (e) {
is NetworkException -> ...
is WrongUserException -> ...
else -> ...
}
}
bloder
06/30/2019, 2:04 PMwhen
is exactly what I want to avoid and my main problem today... Imagine a lot of Exception checkings, this code is getting to be a modifiable and not extensible one, splitting it with functions we can improve that, separating more the code, of course we can create other solutions to handle that like I've already did (composing exceptions handling, avoiding this when
), but I really think this guard solution is the thing that I'm looking for to solve itgildor
06/30/2019, 2:08 PMfun NetworkException.handle()
fun WrongUserException.handle()
when (e) {
is NetworkException -> e.handle()
is WrongUserException -> e.handle()
}
bloder
06/30/2019, 2:15 PMgildor
06/30/2019, 2:33 PMbloder
06/30/2019, 2:36 PMgildor
06/30/2019, 2:39 PMbloder
06/30/2019, 2:44 PMfun handleError_someHashCodeToConditionOne(e: Exception) {
if (e is NetworkException) {}
}
gildor
06/30/2019, 2:55 PMbloder
06/30/2019, 3:09 PMhandleError
being called, is not a good idea, maybe move that when
logic in your first example part to compiler would be better solution, something like (but of course I still don't know the effects of it to compiler or if it's possible todo that):
fun handleError_someHashCodeToConditionOne(e: Exception) {}
fun handleError_someHashCodeToConditionTwo(e: Exception) {}
fun main() {
// all stuffs here and now create checks to //figure out which handleError we're going to //call and the expressions are geted from the //function declaration like
if (e is NetworkException) handleError_someHashCodeToConditionOne(e)
if (e is WrongUserException)
handleError_someHashCodeToConditionTwo(e)
}
Of course are missing a lot of usecases that I didn't think about itgildor
07/01/2019, 6:16 AMhandleError(somError)
and it should automatically select proper function, if second than it looks as some generic pattern matching than something related to exceptions only and not sure how such haskell-style pattern matching would work with Kotlin, it’s dramatic change of how function resolution worksbloder
07/01/2019, 12:06 PMhandleError(somError)
and compiler decides based on function condition declarations which function will be called, the solution for that? I don't know, I see that with a giant change with pattern matching or an alternative solution as I said with some similiar solution to what compiler does with extension functions (get the written function and generate bytecode to other function struct based on the original)Pavlo Liapota
07/01/2019, 12:38 PMwhen
like @gildor suggested you want to add some very special syntax to a language, so that compiler can generate this function for you?
Does this worth it? Especially if this most probably will be limited that all these functions should be placed in the same file (like for sealed classes) as it may be hard to find all implementations otherwise.gildor
07/01/2019, 1:05 PMbloder
07/01/2019, 1:59 PMgildor
07/01/2019, 2:03 PMwith
bloder
07/01/2019, 2:14 PMwhen
implies us to use that in the end being easier to read and understand but being less extenaible.Pavlo Liapota
07/01/2019, 2:24 PMwhen
there is an explicit check for an argument type.
And why do you consider when
to be less extensible?bloder
07/01/2019, 2:39 PMfun handleError(e: Exception) {
when(e) {
is NetWorkProblem -> {}
is OtherProblem -> {}
}
}
and you need to put some other handling on this solution you're actually modifying your code instead of extending it, in some time you will figure out that you've created a giant solution just to check which type is your error, one another good example is consuming states from a state machine with when
, I've created an alternative way to supply that but I'm still not 100% satisfied with that. ( https://github.com/bloderxd/result#error-cases-with-composition )Pavlo Liapota
07/01/2019, 2:53 PMbloder
07/01/2019, 2:56 PMgildor
07/01/2019, 3:22 PMbloder
07/01/2019, 3:37 PM