Replies: 3 comments 3 replies
-
A simpler solution is to provide constructors or constructor-like functions that should throw an exception in case of a failure. They will be easier to use than encapsulating them in Here's an example for the fun PositiveInt(number: Number): PositiveInt Here's an example of creating a // Before
val number: PositiveInt = 23.toPositiveInt()
.getOrThrow()
println(number) // 23
// After
val number = PositiveInt(23)
println(number) // 23 Here's an example of creating a // Before
final Object result = PositiveIntKt.toPositiveInt(23);
final PositiveInt number = (PositiveInt) result;
System.out.println(number); // 23
// After
final PositiveInt number = PositiveIntFactory.create(23);
System.out.println(number); // 23 |
Beta Was this translation helpful? Give feedback.
-
We've created the topic #587 for implementing this idea. |
Beta Was this translation helpful? Give feedback.
-
Hello 👋 I have been following this project a bit on the sidelines lately. I don’t even remember how I found out about it, probably reddit 🤔 Anyway, I like the idea of the project, so I was considering to try it out on a pet project. I also noticed the very structured issues list and documentation. Very nice 👍 Anyway, back to the original discussion. I have been going through the code a bit and I understand the issues you are having with the java interface. I was wondering if you also considered marking the create and createOrNull methods with the @JvmStatic annotation. If I am not mistaken that would mean we could get rid of the .Companion in java method calls everywhere. That would make it a lot more readable already in my opinion. One more thing I wanted to add to this discussion. You mentioned that you wanted to convert the value classes back to "normal" classes. I am bit concerned that that might increase the memory overhead quite a bit, which might make the project less suitable for people who want to start using it. |
Beta Was this translation helpful? Give feedback.
-
🤔 The problem
The current stable API provides factory functions encapsulating their result in a
Result
type.This is for example the case for the
toNonZeroInt
factory function.But this practice increases the complexity of the API by using a concept that is not widely accepted in the Kotlin community.
Also, the Kotlin team recommends other practices for validating inputs and representing an absence of value using
null
.💡 The idea
For all platforms, Kotools Types should provide different types of factory functions for creating types:
create
functions oncompanion
objects that should throw an exception in case of invalid inputscreateOrNull
functions oncompanion
objects that should returnnull
in case of invalid inputs.For instance, we could add the following factory functions to the
PositiveInt
type:Here are examples of calling the
PositiveInt.Companion.createOrNull
function from Kotlin code:Here are other examples of calling the same function from Java code:
🙏 Help needed
Please give this post a reaction if you are interested in this change and comment below your thoughts on it.
We would love to have your feedback!
Beta Was this translation helpful? Give feedback.
All reactions