equivur.blogg.se

Kotlin uuid
Kotlin uuid












kotlin uuid

This tells the compiler to inline the type where possible but still provides compile-time safety if we try to use the incorrect type. In order to use inline classes, you would need to create a class with a single value, and prefix it with the word inline. Val safeCompilingIngredient = Ingredient(ingredientId, recipeId) Val recipeId = RecipeId(UUID.randomUUID()) Val doesntCompileIngredient = Ingredient(ingredientId, ingredientId) Can be quite easy to pass in the incorrect UUID without inline classes: Val ingredientId = IngredientId(UUID.randomUUID()) But how can we make this more explicit? Example with inline classes 👩🏼‍🏫 // With inline classes ✅ĭata class Ingredient(id: IngredientId, recipeId: RecipeId) If you are lucky, you might have a unit test that indicates to you that you’ve used the wrong ID. Val incorrectIngredient = Ingredient(recipeId, recipeId) Without inline classes 😞ĭata class Ingredient(id: UUID, recipeId: UUID) This would still compile and there is no indication that there is an issue. When creating a new Ingredient class, it can be quite easy to pass the incorrect ID to the Ingredient class. Example without inline classes 🕵🏻‍♀️įor instance, we have two classes - Recipe and Ingredient, which both use a UUID as the type to represent the ID of the instance. This is similar to inline functions, where the compiler inserts the contents of the function into where it was called.

kotlin uuid

At runtime, the compiler will “inline” the property where it was used. An inline class is a special type of class defined in Kotlin that only has one property.














Kotlin uuid