Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Kotlin value classes #30

Open
Azzpg7 opened this issue Feb 24, 2024 · 0 comments
Open

Support Kotlin value classes #30

Azzpg7 opened this issue Feb 24, 2024 · 0 comments
Labels
feature New feature or request

Comments

@Azzpg7
Copy link

Azzpg7 commented Feb 24, 2024

It would be nice to support value classes from Kotlin.

It will provide more convenient way to validate data and will be consistent with the domain model.
For example:

@JvmInline
value class Password(private val s: String) {
    init {
        require(s.length >= 6) { "Password is too short" }
        require(s.contains(Regex("[a-z]"))) { "Password must contain at least one lowercase letter" }
        require(s.contains(Regex("[A-Z]"))) { "Password must contain at least one uppercase letter" }
        require(s.contains(Regex("[0-9]"))) { "Password must contain at least one digit" }
        // etc
    }
}

As I can see from the decompiled code, the value class uses the hashCode() and equals() methods of the incapsulated class.

public final class Password {
   @NotNull
   private final String s;

   public static int hashCode_impl/* $FF was: hashCode-impl*/(String arg0) {
      return arg0.hashCode();
   }

   public int hashCode() {
      return hashCode-impl(this.s);
   }

   public static boolean equals_impl/* $FF was: equals-impl*/(String arg0, Object other) {
      if (!(other instanceof Password)) {
         return false;
      } else {
         return Intrinsics.areEqual(arg0, ((Password)other).unbox-impl());
      }
   }

   public boolean equals(Object other) {
      return equals-impl(this.s, other);
   }

   // $FF: synthetic method
   private Password(String s) {
      this.s = s;
   }

   @NotNull
   public static String constructor_impl/* $FF was: constructor-impl*/(@NotNull String s) {
      Intrinsics.checkNotNullParameter(s, "s");
      return s;
   }

   // $FF: synthetic method
   public static final Password box_impl/* $FF was: box-impl*/(String v) {
      return new Password(v);
   }

   // $FF: synthetic method
   public final String unbox_impl/* $FF was: unbox-impl*/() {
      return this.s;
   }

   public static final boolean equals_impl0/* $FF was: equals-impl0*/(String p1, String p2) {
      return Intrinsics.areEqual(p1, p2);
   }
}
@lavrukov lavrukov added the feature New feature or request label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants