How can I handle this “which is not a subtype of overridden” error












0















I'm trying to write some validatable form interface in Kotlin. In the validation part I'm using https://github.com/kamedon/Validation.



Here is the very simple code I'm trying to run;



import com.kamedon.validation.Validation


abstract class Validatable {
abstract val validation: Validation<Any>

fun validate() = validation.validate(this)
}

class LoginForm : Validatable() {
val name: String = "Onur"
val age: Int = 23

override val validation = Validation<LoginForm> {
"name" {
be { name.length >= 5 } not "5 characters or more"
be { name.length <= 10 } not "10 characters or less"
}
"age" {
be { age >= 20 } not "Over 20 years old"
}
}
}


fun main(args: Array<String>) {
val user = LoginForm()
val result = user.validate()
println(result)
}


This code gives me;



Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<Any> defined in Validatable'


If I use Validation<out Any> in Validatable it says;



Kotlin: Out-projected type 'Validation<out Any>' prohibits the use of 'public final fun validate(value: T): Map<String, List<String>> defined in com.kamedon.validation.Validation'


If I use Validation<in Any> in Validatable it says;



Kotlin: Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<in Any> defined in Validatable'


If I use Validation<Any> instead of Validation<LoginForm> in LoginForm, the code runs but this time name and age inside of validation are used from the class inside itself. I don't want to change this in respect of the usage of the library.



Is there anyway to use in and out keywords together or may be there is another way to achieve my goal.










share|improve this question



























    0















    I'm trying to write some validatable form interface in Kotlin. In the validation part I'm using https://github.com/kamedon/Validation.



    Here is the very simple code I'm trying to run;



    import com.kamedon.validation.Validation


    abstract class Validatable {
    abstract val validation: Validation<Any>

    fun validate() = validation.validate(this)
    }

    class LoginForm : Validatable() {
    val name: String = "Onur"
    val age: Int = 23

    override val validation = Validation<LoginForm> {
    "name" {
    be { name.length >= 5 } not "5 characters or more"
    be { name.length <= 10 } not "10 characters or less"
    }
    "age" {
    be { age >= 20 } not "Over 20 years old"
    }
    }
    }


    fun main(args: Array<String>) {
    val user = LoginForm()
    val result = user.validate()
    println(result)
    }


    This code gives me;



    Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<Any> defined in Validatable'


    If I use Validation<out Any> in Validatable it says;



    Kotlin: Out-projected type 'Validation<out Any>' prohibits the use of 'public final fun validate(value: T): Map<String, List<String>> defined in com.kamedon.validation.Validation'


    If I use Validation<in Any> in Validatable it says;



    Kotlin: Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<in Any> defined in Validatable'


    If I use Validation<Any> instead of Validation<LoginForm> in LoginForm, the code runs but this time name and age inside of validation are used from the class inside itself. I don't want to change this in respect of the usage of the library.



    Is there anyway to use in and out keywords together or may be there is another way to achieve my goal.










    share|improve this question

























      0












      0








      0








      I'm trying to write some validatable form interface in Kotlin. In the validation part I'm using https://github.com/kamedon/Validation.



      Here is the very simple code I'm trying to run;



      import com.kamedon.validation.Validation


      abstract class Validatable {
      abstract val validation: Validation<Any>

      fun validate() = validation.validate(this)
      }

      class LoginForm : Validatable() {
      val name: String = "Onur"
      val age: Int = 23

      override val validation = Validation<LoginForm> {
      "name" {
      be { name.length >= 5 } not "5 characters or more"
      be { name.length <= 10 } not "10 characters or less"
      }
      "age" {
      be { age >= 20 } not "Over 20 years old"
      }
      }
      }


      fun main(args: Array<String>) {
      val user = LoginForm()
      val result = user.validate()
      println(result)
      }


      This code gives me;



      Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<Any> defined in Validatable'


      If I use Validation<out Any> in Validatable it says;



      Kotlin: Out-projected type 'Validation<out Any>' prohibits the use of 'public final fun validate(value: T): Map<String, List<String>> defined in com.kamedon.validation.Validation'


      If I use Validation<in Any> in Validatable it says;



      Kotlin: Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<in Any> defined in Validatable'


      If I use Validation<Any> instead of Validation<LoginForm> in LoginForm, the code runs but this time name and age inside of validation are used from the class inside itself. I don't want to change this in respect of the usage of the library.



      Is there anyway to use in and out keywords together or may be there is another way to achieve my goal.










      share|improve this question














      I'm trying to write some validatable form interface in Kotlin. In the validation part I'm using https://github.com/kamedon/Validation.



      Here is the very simple code I'm trying to run;



      import com.kamedon.validation.Validation


      abstract class Validatable {
      abstract val validation: Validation<Any>

      fun validate() = validation.validate(this)
      }

      class LoginForm : Validatable() {
      val name: String = "Onur"
      val age: Int = 23

      override val validation = Validation<LoginForm> {
      "name" {
      be { name.length >= 5 } not "5 characters or more"
      be { name.length <= 10 } not "10 characters or less"
      }
      "age" {
      be { age >= 20 } not "Over 20 years old"
      }
      }
      }


      fun main(args: Array<String>) {
      val user = LoginForm()
      val result = user.validate()
      println(result)
      }


      This code gives me;



      Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<Any> defined in Validatable'


      If I use Validation<out Any> in Validatable it says;



      Kotlin: Out-projected type 'Validation<out Any>' prohibits the use of 'public final fun validate(value: T): Map<String, List<String>> defined in com.kamedon.validation.Validation'


      If I use Validation<in Any> in Validatable it says;



      Kotlin: Type of 'validation' is not a subtype of the overridden property 'public abstract val validation: Validation<in Any> defined in Validatable'


      If I use Validation<Any> instead of Validation<LoginForm> in LoginForm, the code runs but this time name and age inside of validation are used from the class inside itself. I don't want to change this in respect of the usage of the library.



      Is there anyway to use in and out keywords together or may be there is another way to achieve my goal.







      generics kotlin generic-variance type-projection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 7:56









      Onur Eren ElibolOnur Eren Elibol

      1711110




      1711110
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You could make the abstract class Validatable a generic class, and make the subclass provide an object which exposes both the Validation object, and also itself as the target to validate, e.g.



          abstract class Validatable<T> {
          protected class ValidationInfo<T>(val target: T, val validation: Validation<T>)

          protected abstract val validationInfo: ValidationInfo<T>

          fun validate() = validationInfo.let { it.validation.validate(it.target) }
          }

          class LoginForm : Validatable<LoginForm>() {
          val name: String = "Onur"
          val age: Int = 23

          override val validationInfo = ValidationInfo(this, Validation {
          "name" {
          be { name.length >= 5 } not "5 characters or more"
          be { name.length <= 10 } not "10 characters or less"
          }
          "age" {
          be { age >= 20 } not "Over 20 years old"
          }
          })
          }





          share|improve this answer


























          • I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

            – Onur Eren Elibol
            Nov 21 '18 at 8:28











          • Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

            – Yoni Gibbs
            Nov 21 '18 at 9:00











          • Very clever solution. Thank you.

            – Onur Eren Elibol
            Nov 21 '18 at 10:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407487%2fhow-can-i-handle-this-which-is-not-a-subtype-of-overridden-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You could make the abstract class Validatable a generic class, and make the subclass provide an object which exposes both the Validation object, and also itself as the target to validate, e.g.



          abstract class Validatable<T> {
          protected class ValidationInfo<T>(val target: T, val validation: Validation<T>)

          protected abstract val validationInfo: ValidationInfo<T>

          fun validate() = validationInfo.let { it.validation.validate(it.target) }
          }

          class LoginForm : Validatable<LoginForm>() {
          val name: String = "Onur"
          val age: Int = 23

          override val validationInfo = ValidationInfo(this, Validation {
          "name" {
          be { name.length >= 5 } not "5 characters or more"
          be { name.length <= 10 } not "10 characters or less"
          }
          "age" {
          be { age >= 20 } not "Over 20 years old"
          }
          })
          }





          share|improve this answer


























          • I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

            – Onur Eren Elibol
            Nov 21 '18 at 8:28











          • Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

            – Yoni Gibbs
            Nov 21 '18 at 9:00











          • Very clever solution. Thank you.

            – Onur Eren Elibol
            Nov 21 '18 at 10:38
















          2














          You could make the abstract class Validatable a generic class, and make the subclass provide an object which exposes both the Validation object, and also itself as the target to validate, e.g.



          abstract class Validatable<T> {
          protected class ValidationInfo<T>(val target: T, val validation: Validation<T>)

          protected abstract val validationInfo: ValidationInfo<T>

          fun validate() = validationInfo.let { it.validation.validate(it.target) }
          }

          class LoginForm : Validatable<LoginForm>() {
          val name: String = "Onur"
          val age: Int = 23

          override val validationInfo = ValidationInfo(this, Validation {
          "name" {
          be { name.length >= 5 } not "5 characters or more"
          be { name.length <= 10 } not "10 characters or less"
          }
          "age" {
          be { age >= 20 } not "Over 20 years old"
          }
          })
          }





          share|improve this answer


























          • I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

            – Onur Eren Elibol
            Nov 21 '18 at 8:28











          • Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

            – Yoni Gibbs
            Nov 21 '18 at 9:00











          • Very clever solution. Thank you.

            – Onur Eren Elibol
            Nov 21 '18 at 10:38














          2












          2








          2







          You could make the abstract class Validatable a generic class, and make the subclass provide an object which exposes both the Validation object, and also itself as the target to validate, e.g.



          abstract class Validatable<T> {
          protected class ValidationInfo<T>(val target: T, val validation: Validation<T>)

          protected abstract val validationInfo: ValidationInfo<T>

          fun validate() = validationInfo.let { it.validation.validate(it.target) }
          }

          class LoginForm : Validatable<LoginForm>() {
          val name: String = "Onur"
          val age: Int = 23

          override val validationInfo = ValidationInfo(this, Validation {
          "name" {
          be { name.length >= 5 } not "5 characters or more"
          be { name.length <= 10 } not "10 characters or less"
          }
          "age" {
          be { age >= 20 } not "Over 20 years old"
          }
          })
          }





          share|improve this answer















          You could make the abstract class Validatable a generic class, and make the subclass provide an object which exposes both the Validation object, and also itself as the target to validate, e.g.



          abstract class Validatable<T> {
          protected class ValidationInfo<T>(val target: T, val validation: Validation<T>)

          protected abstract val validationInfo: ValidationInfo<T>

          fun validate() = validationInfo.let { it.validation.validate(it.target) }
          }

          class LoginForm : Validatable<LoginForm>() {
          val name: String = "Onur"
          val age: Int = 23

          override val validationInfo = ValidationInfo(this, Validation {
          "name" {
          be { name.length >= 5 } not "5 characters or more"
          be { name.length <= 10 } not "10 characters or less"
          }
          "age" {
          be { age >= 20 } not "Over 20 years old"
          }
          })
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 9:02

























          answered Nov 21 '18 at 8:08









          Yoni GibbsYoni Gibbs

          1,368113




          1,368113













          • I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

            – Onur Eren Elibol
            Nov 21 '18 at 8:28











          • Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

            – Yoni Gibbs
            Nov 21 '18 at 9:00











          • Very clever solution. Thank you.

            – Onur Eren Elibol
            Nov 21 '18 at 10:38



















          • I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

            – Onur Eren Elibol
            Nov 21 '18 at 8:28











          • Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

            – Yoni Gibbs
            Nov 21 '18 at 9:00











          • Very clever solution. Thank you.

            – Onur Eren Elibol
            Nov 21 '18 at 10:38

















          I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

          – Onur Eren Elibol
          Nov 21 '18 at 8:28





          I tried this but this time validation.validate(this) doesn't work. it requires this as T but receives Validatable<T>. Compiler complains for this.

          – Onur Eren Elibol
          Nov 21 '18 at 8:28













          Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

          – Yoni Gibbs
          Nov 21 '18 at 9:00





          Yup, you're right, my mistake. I've updated my answer with a slightly different approach, though I think there must be a better way of doing this.

          – Yoni Gibbs
          Nov 21 '18 at 9:00













          Very clever solution. Thank you.

          – Onur Eren Elibol
          Nov 21 '18 at 10:38





          Very clever solution. Thank you.

          – Onur Eren Elibol
          Nov 21 '18 at 10:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407487%2fhow-can-i-handle-this-which-is-not-a-subtype-of-overridden-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini