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
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
add a comment |
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
add a comment |
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
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
scala generics
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
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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