Poly fold return type not inferred correctly












0















I am trying to create a poly function that folds over a tuple of Foos:



case class Foo[A](a: A)

object extractFold extends Poly2 {
implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] = {
???
}
}

def extract[In, A <: HList, B <: HList](keys: In)
(implicit
gen: Generic.Aux[In, A],
folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
tupler: Tupler[B])
: Foo[tupler.Out] = {
???
}

val result = extract((Foo(1), Foo("a")))


The function works at runtime, but the compiler inferred result type is always Foo[Unit] which is not right - in this example it should be Foo[(Int, String)]










share|improve this question



























    0















    I am trying to create a poly function that folds over a tuple of Foos:



    case class Foo[A](a: A)

    object extractFold extends Poly2 {
    implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] = {
    ???
    }
    }

    def extract[In, A <: HList, B <: HList](keys: In)
    (implicit
    gen: Generic.Aux[In, A],
    folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
    tupler: Tupler[B])
    : Foo[tupler.Out] = {
    ???
    }

    val result = extract((Foo(1), Foo("a")))


    The function works at runtime, but the compiler inferred result type is always Foo[Unit] which is not right - in this example it should be Foo[(Int, String)]










    share|improve this question

























      0












      0








      0








      I am trying to create a poly function that folds over a tuple of Foos:



      case class Foo[A](a: A)

      object extractFold extends Poly2 {
      implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] = {
      ???
      }
      }

      def extract[In, A <: HList, B <: HList](keys: In)
      (implicit
      gen: Generic.Aux[In, A],
      folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
      tupler: Tupler[B])
      : Foo[tupler.Out] = {
      ???
      }

      val result = extract((Foo(1), Foo("a")))


      The function works at runtime, but the compiler inferred result type is always Foo[Unit] which is not right - in this example it should be Foo[(Int, String)]










      share|improve this question














      I am trying to create a poly function that folds over a tuple of Foos:



      case class Foo[A](a: A)

      object extractFold extends Poly2 {
      implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] = {
      ???
      }
      }

      def extract[In, A <: HList, B <: HList](keys: In)
      (implicit
      gen: Generic.Aux[In, A],
      folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
      tupler: Tupler[B])
      : Foo[tupler.Out] = {
      ???
      }

      val result = extract((Foo(1), Foo("a")))


      The function works at runtime, but the compiler inferred result type is always Foo[Unit] which is not right - in this example it should be Foo[(Int, String)]







      scala shapeless






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 23:27









      AlexFoxGillAlexFoxGill

      5,27013250




      5,27013250
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in



          val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
          Foo[Int] :: Foo[String] :: HNil,
          Int :: String :: HNil]((Foo(1), Foo("a")))


          the code correctly typechecks. Obviously you don't want to specify those types explicitly though.



          According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:



          trait Extractor[L <: HList, HF] {
          type FR <: HList
          type TR
          val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
          val tupler: Tupler.Aux[FR, TR]
          }

          object Extractor {
          type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}

          implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
          tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
          type FR = FR0
          type TR = TR0
          override val folder = folder0
          override val tupler = tupler0
          }
          }


          and then using it like this:



          def extract[In, A <: HList, B <: HList, C](keys: In)
          (implicit gen: Generic.Aux[In, A],
          extractor: Extractor.Aux[A, extractFold.type, B, C])
          : Foo[C] = {
          val hli = gen.to(keys)
          val fr = extractor.folder(hli, Foo(HNil))
          Foo(extractor.tupler(fr.a))
          }


          This is a hacky solution but at least it seem to work (see also online demo).






          share|improve this answer


























          • thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

            – AlexFoxGill
            Jan 21 at 11:17











          • @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

            – SergGr
            Jan 21 at 13:00













          • perhaps i missed something, i'll try again and confirm

            – AlexFoxGill
            Jan 21 at 13:03











          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%2f54262699%2fpoly-fold-return-type-not-inferred-correctly%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









          1














          Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in



          val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
          Foo[Int] :: Foo[String] :: HNil,
          Int :: String :: HNil]((Foo(1), Foo("a")))


          the code correctly typechecks. Obviously you don't want to specify those types explicitly though.



          According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:



          trait Extractor[L <: HList, HF] {
          type FR <: HList
          type TR
          val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
          val tupler: Tupler.Aux[FR, TR]
          }

          object Extractor {
          type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}

          implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
          tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
          type FR = FR0
          type TR = TR0
          override val folder = folder0
          override val tupler = tupler0
          }
          }


          and then using it like this:



          def extract[In, A <: HList, B <: HList, C](keys: In)
          (implicit gen: Generic.Aux[In, A],
          extractor: Extractor.Aux[A, extractFold.type, B, C])
          : Foo[C] = {
          val hli = gen.to(keys)
          val fr = extractor.folder(hli, Foo(HNil))
          Foo(extractor.tupler(fr.a))
          }


          This is a hacky solution but at least it seem to work (see also online demo).






          share|improve this answer


























          • thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

            – AlexFoxGill
            Jan 21 at 11:17











          • @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

            – SergGr
            Jan 21 at 13:00













          • perhaps i missed something, i'll try again and confirm

            – AlexFoxGill
            Jan 21 at 13:03
















          1














          Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in



          val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
          Foo[Int] :: Foo[String] :: HNil,
          Int :: String :: HNil]((Foo(1), Foo("a")))


          the code correctly typechecks. Obviously you don't want to specify those types explicitly though.



          According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:



          trait Extractor[L <: HList, HF] {
          type FR <: HList
          type TR
          val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
          val tupler: Tupler.Aux[FR, TR]
          }

          object Extractor {
          type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}

          implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
          tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
          type FR = FR0
          type TR = TR0
          override val folder = folder0
          override val tupler = tupler0
          }
          }


          and then using it like this:



          def extract[In, A <: HList, B <: HList, C](keys: In)
          (implicit gen: Generic.Aux[In, A],
          extractor: Extractor.Aux[A, extractFold.type, B, C])
          : Foo[C] = {
          val hli = gen.to(keys)
          val fr = extractor.folder(hli, Foo(HNil))
          Foo(extractor.tupler(fr.a))
          }


          This is a hacky solution but at least it seem to work (see also online demo).






          share|improve this answer


























          • thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

            – AlexFoxGill
            Jan 21 at 11:17











          • @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

            – SergGr
            Jan 21 at 13:00













          • perhaps i missed something, i'll try again and confirm

            – AlexFoxGill
            Jan 21 at 13:03














          1












          1








          1







          Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in



          val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
          Foo[Int] :: Foo[String] :: HNil,
          Int :: String :: HNil]((Foo(1), Foo("a")))


          the code correctly typechecks. Obviously you don't want to specify those types explicitly though.



          According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:



          trait Extractor[L <: HList, HF] {
          type FR <: HList
          type TR
          val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
          val tupler: Tupler.Aux[FR, TR]
          }

          object Extractor {
          type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}

          implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
          tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
          type FR = FR0
          type TR = TR0
          override val folder = folder0
          override val tupler = tupler0
          }
          }


          and then using it like this:



          def extract[In, A <: HList, B <: HList, C](keys: In)
          (implicit gen: Generic.Aux[In, A],
          extractor: Extractor.Aux[A, extractFold.type, B, C])
          : Foo[C] = {
          val hli = gen.to(keys)
          val fr = extractor.folder(hli, Foo(HNil))
          Foo(extractor.tupler(fr.a))
          }


          This is a hacky solution but at least it seem to work (see also online demo).






          share|improve this answer















          Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in



          val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
          Foo[Int] :: Foo[String] :: HNil,
          Int :: String :: HNil]((Foo(1), Foo("a")))


          the code correctly typechecks. Obviously you don't want to specify those types explicitly though.



          According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:



          trait Extractor[L <: HList, HF] {
          type FR <: HList
          type TR
          val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
          val tupler: Tupler.Aux[FR, TR]
          }

          object Extractor {
          type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}

          implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
          tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
          type FR = FR0
          type TR = TR0
          override val folder = folder0
          override val tupler = tupler0
          }
          }


          and then using it like this:



          def extract[In, A <: HList, B <: HList, C](keys: In)
          (implicit gen: Generic.Aux[In, A],
          extractor: Extractor.Aux[A, extractFold.type, B, C])
          : Foo[C] = {
          val hli = gen.to(keys)
          val fr = extractor.folder(hli, Foo(HNil))
          Foo(extractor.tupler(fr.a))
          }


          This is a hacky solution but at least it seem to work (see also online demo).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 2:28

























          answered Jan 19 at 2:04









          SergGrSergGr

          20.9k22243




          20.9k22243













          • thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

            – AlexFoxGill
            Jan 21 at 11:17











          • @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

            – SergGr
            Jan 21 at 13:00













          • perhaps i missed something, i'll try again and confirm

            – AlexFoxGill
            Jan 21 at 13:03



















          • thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

            – AlexFoxGill
            Jan 21 at 11:17











          • @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

            – SergGr
            Jan 21 at 13:00













          • perhaps i missed something, i'll try again and confirm

            – AlexFoxGill
            Jan 21 at 13:03

















          thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

          – AlexFoxGill
          Jan 21 at 11:17





          thank you for the reply. this approach obeys inference if i type it like val result: Foo[(Int, String)] = extract(...), without the type hint it resolves to Foo[Nothing]

          – AlexFoxGill
          Jan 21 at 11:17













          @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

          – SergGr
          Jan 21 at 13:00







          @AlexFoxGill, I probably miss something but this is not what I see. If I write val r2 = extract((Foo(1), Foo("a"))) and then try to assign it to val r3a:Foo[(Int, String)] = r2, it works, but val r3b:Foo[Nothing] = r2 fails. See the modified demo. Could you tell me how you reproduce the issue?

          – SergGr
          Jan 21 at 13:00















          perhaps i missed something, i'll try again and confirm

          – AlexFoxGill
          Jan 21 at 13:03





          perhaps i missed something, i'll try again and confirm

          – AlexFoxGill
          Jan 21 at 13:03


















          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%2f54262699%2fpoly-fold-return-type-not-inferred-correctly%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







          Popular posts from this blog

          Callistus III

          Ostreoida

          Plistias Cous