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

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript