Writing a generic Scala merge function, can't get types to line up for compilerCreating an empty IndexedSeq of a sub-type and using TailRecHow to get the type of T from a member of a generic class or method?How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?Scala: Abstract types vs genericsGet generic type of class at runtimeHow to get a class instance of generics type TGeneric type inference in ScalaScala: conforming concrete type to generics with boundsScala generic type parameters issuesScala Generics Type Bounds - Pointing to the Actual TypeScala can't convert Java map keySet to a Sequence type

How soon after takeoff can you recline your airplane seat?

Aligning arrays within arrays within another array

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

Why doesn't SpaceX land boosters in Africa?

Simplify the code

Why can't i use !(single pattern) in zsh even after i turn on kshglob?

Why should I allow multiple IP addresses on a website for a single session?

What caused the flashes in the video footage of Chernobyl?

My mom helped me cosign a car and now she wants to take it

Was Wolfgang Unziker the last Amateur GM?

What was the ASCII end of medium (EM) character intended to be used for?

When does it become illegal to exchange bitcoin for cash?

Can I deep fry food in butter instead of vegetable oil?

How to idiomatically express the idea "if you can cheat without being caught, do it"

Existence of infinite set of positive integers s.t sum of reciprocals is rational and set of primes dividing an element is infinite

How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?

Is this house-rule removing the increased effect of cantrips at higher character levels balanced?

Trace in the category of propositional statements

How far can gerrymandering go?

Is it OK to say "The situation is pregnant with a crisis"?

Does the Grothendieck group of finitely generated modules form a commutative ring where the multiplication structure is induced from tensor product?

What is the meaning of ゴト in the context of 鮎

usage of y" not just for locations?

Could citing a database like libgen get one into trouble?



Writing a generic Scala merge function, can't get types to line up for compiler


Creating an empty IndexedSeq of a sub-type and using TailRecHow to get the type of T from a member of a generic class or method?How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?Scala: Abstract types vs genericsGet generic type of class at runtimeHow to get a class instance of generics type TGeneric type inference in ScalaScala: conforming concrete type to generics with boundsScala generic type parameters issuesScala Generics Type Bounds - Pointing to the Actual TypeScala can't convert Java map keySet to a Sequence type













1















I'm trying to learn FP with Scala. I've got a function on an object to merge two sorted collections and return the same type. It looks like this:



@tailrec def merge[A, Repr <: Seq[A]](merged: Repr, l1: Repr, l2: Repr)(
implicit ordering: Ordering[A]): Repr =
if (l1.isEmpty) merged ++ l2
else if (l2.isEmpty) merged ++ l1
else
val l1Head: A = l1.head
val l2Head = l2.head
val orderVal = ordering.compare(l1Head, l2Head)
if (orderVal <= 0)
val m2: Repr = l1Head +: merged
merge[A, Repr](m2, l1.tail, l2)
else
merge[A, Repr](l2Head +: merged, l1, l2.tail)





It's giving me the compiler error "Expression of type Seq[A] doesn't conform to the expected type Repr










share|improve this question




























    1















    I'm trying to learn FP with Scala. I've got a function on an object to merge two sorted collections and return the same type. It looks like this:



    @tailrec def merge[A, Repr <: Seq[A]](merged: Repr, l1: Repr, l2: Repr)(
    implicit ordering: Ordering[A]): Repr =
    if (l1.isEmpty) merged ++ l2
    else if (l2.isEmpty) merged ++ l1
    else
    val l1Head: A = l1.head
    val l2Head = l2.head
    val orderVal = ordering.compare(l1Head, l2Head)
    if (orderVal <= 0)
    val m2: Repr = l1Head +: merged
    merge[A, Repr](m2, l1.tail, l2)
    else
    merge[A, Repr](l2Head +: merged, l1, l2.tail)





    It's giving me the compiler error "Expression of type Seq[A] doesn't conform to the expected type Repr










    share|improve this question


























      1












      1








      1


      0






      I'm trying to learn FP with Scala. I've got a function on an object to merge two sorted collections and return the same type. It looks like this:



      @tailrec def merge[A, Repr <: Seq[A]](merged: Repr, l1: Repr, l2: Repr)(
      implicit ordering: Ordering[A]): Repr =
      if (l1.isEmpty) merged ++ l2
      else if (l2.isEmpty) merged ++ l1
      else
      val l1Head: A = l1.head
      val l2Head = l2.head
      val orderVal = ordering.compare(l1Head, l2Head)
      if (orderVal <= 0)
      val m2: Repr = l1Head +: merged
      merge[A, Repr](m2, l1.tail, l2)
      else
      merge[A, Repr](l2Head +: merged, l1, l2.tail)





      It's giving me the compiler error "Expression of type Seq[A] doesn't conform to the expected type Repr










      share|improve this question
















      I'm trying to learn FP with Scala. I've got a function on an object to merge two sorted collections and return the same type. It looks like this:



      @tailrec def merge[A, Repr <: Seq[A]](merged: Repr, l1: Repr, l2: Repr)(
      implicit ordering: Ordering[A]): Repr =
      if (l1.isEmpty) merged ++ l2
      else if (l2.isEmpty) merged ++ l1
      else
      val l1Head: A = l1.head
      val l2Head = l2.head
      val orderVal = ordering.compare(l1Head, l2Head)
      if (orderVal <= 0)
      val m2: Repr = l1Head +: merged
      merge[A, Repr](m2, l1.tail, l2)
      else
      merge[A, Repr](l2Head +: merged, l1, l2.tail)





      It's giving me the compiler error "Expression of type Seq[A] doesn't conform to the expected type Repr







      scala generics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 17:29









      Yuval Itzchakov

      119k26 gold badges182 silver badges250 bronze badges




      119k26 gold badges182 silver badges250 bronze badges










      asked Mar 25 at 17:23









      Jason CarreiraJason Carreira

      12311 bronze badges




      12311 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Try



          @tailrec def merge[A, Repr](merged: Repr, l1: Repr, l2: Repr)(implicit
          ev: Repr => SeqLike[A, Repr],
          cbf: CanBuildFrom[Repr, A, Repr],
          ordering: Ordering[A]
          ): Repr =
          if (l1.isEmpty) merged ++ l2
          else if (l2.isEmpty) merged ++ l1
          else
          val l1Head: A = l1.head
          val l2Head = l2.head
          val orderVal = ordering.compare(l1Head, l2Head)
          if (orderVal <= 0)
          val m2: Repr = l1Head +: merged
          merge[A, Repr](m2, l1.tail, l2)
          else
          merge[A, Repr](l2Head +: merged, l1, l2.tail)








          share|improve this answer























          • That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

            – Jason Carreira
            Mar 25 at 19:18










          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%2f55343336%2fwriting-a-generic-scala-merge-function-cant-get-types-to-line-up-for-compiler%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









          0














          Try



          @tailrec def merge[A, Repr](merged: Repr, l1: Repr, l2: Repr)(implicit
          ev: Repr => SeqLike[A, Repr],
          cbf: CanBuildFrom[Repr, A, Repr],
          ordering: Ordering[A]
          ): Repr =
          if (l1.isEmpty) merged ++ l2
          else if (l2.isEmpty) merged ++ l1
          else
          val l1Head: A = l1.head
          val l2Head = l2.head
          val orderVal = ordering.compare(l1Head, l2Head)
          if (orderVal <= 0)
          val m2: Repr = l1Head +: merged
          merge[A, Repr](m2, l1.tail, l2)
          else
          merge[A, Repr](l2Head +: merged, l1, l2.tail)








          share|improve this answer























          • That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

            – Jason Carreira
            Mar 25 at 19:18















          0














          Try



          @tailrec def merge[A, Repr](merged: Repr, l1: Repr, l2: Repr)(implicit
          ev: Repr => SeqLike[A, Repr],
          cbf: CanBuildFrom[Repr, A, Repr],
          ordering: Ordering[A]
          ): Repr =
          if (l1.isEmpty) merged ++ l2
          else if (l2.isEmpty) merged ++ l1
          else
          val l1Head: A = l1.head
          val l2Head = l2.head
          val orderVal = ordering.compare(l1Head, l2Head)
          if (orderVal <= 0)
          val m2: Repr = l1Head +: merged
          merge[A, Repr](m2, l1.tail, l2)
          else
          merge[A, Repr](l2Head +: merged, l1, l2.tail)








          share|improve this answer























          • That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

            – Jason Carreira
            Mar 25 at 19:18













          0












          0








          0







          Try



          @tailrec def merge[A, Repr](merged: Repr, l1: Repr, l2: Repr)(implicit
          ev: Repr => SeqLike[A, Repr],
          cbf: CanBuildFrom[Repr, A, Repr],
          ordering: Ordering[A]
          ): Repr =
          if (l1.isEmpty) merged ++ l2
          else if (l2.isEmpty) merged ++ l1
          else
          val l1Head: A = l1.head
          val l2Head = l2.head
          val orderVal = ordering.compare(l1Head, l2Head)
          if (orderVal <= 0)
          val m2: Repr = l1Head +: merged
          merge[A, Repr](m2, l1.tail, l2)
          else
          merge[A, Repr](l2Head +: merged, l1, l2.tail)








          share|improve this answer













          Try



          @tailrec def merge[A, Repr](merged: Repr, l1: Repr, l2: Repr)(implicit
          ev: Repr => SeqLike[A, Repr],
          cbf: CanBuildFrom[Repr, A, Repr],
          ordering: Ordering[A]
          ): Repr =
          if (l1.isEmpty) merged ++ l2
          else if (l2.isEmpty) merged ++ l1
          else
          val l1Head: A = l1.head
          val l2Head = l2.head
          val orderVal = ordering.compare(l1Head, l2Head)
          if (orderVal <= 0)
          val m2: Repr = l1Head +: merged
          merge[A, Repr](m2, l1.tail, l2)
          else
          merge[A, Repr](l2Head +: merged, l1, l2.tail)









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 18:55









          Dmytro MitinDmytro Mitin

          12.6k1 gold badge7 silver badges22 bronze badges




          12.6k1 gold badge7 silver badges22 bronze badges












          • That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

            – Jason Carreira
            Mar 25 at 19:18

















          • That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

            – Jason Carreira
            Mar 25 at 19:18
















          That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

          – Jason Carreira
          Mar 25 at 19:18





          That works! Unfortunately now that I can test my code it doesn't do the right thing... but at least now I can test it!

          – Jason Carreira
          Mar 25 at 19:18






          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55343336%2fwriting-a-generic-scala-merge-function-cant-get-types-to-line-up-for-compiler%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현