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

tvede

06/10/2017, 10:31 AM
Hi Guys, So if i have a type parameter, is there no way to say that it can be “Any” except “Exception” ?
v

voddan

06/10/2017, 10:36 AM
tvede: not directly.
Any
is just too broad.
As a workaround you can create an overload with
T: Exception
which throws on calling
t

tvede

06/10/2017, 10:39 AM
Thanks,was afraid I overlooked something. Well the idea is that, when modeling results (which are either good or bad), then having a unified construct, could be a good idea, but not being able to say “exceptions are not good results” (i know this can depend on the type of thing you are modeling). 😛
v

voddan

06/10/2017, 10:41 AM
(btw that above was to the wrong thread 😂)
Your case is easily solvable with strong typing
Copy code
sealed class Result {
    class Good(val result: Any)
    class Bad(val exs: Exception)
}
Now you write functions for
Result.Good
2 Views