Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:54:52+00:00 2026-06-13T06:54:52+00:00

I want to have this comparison function that matches two case classes, but it’s

  • 0

I want to have this comparison function that matches two case classes, but it’s a bit verbose.

Leafs are always in the sorted order in the List.

  abstract class CodeTree
  case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
  case class Leaf(char: Char, weight: Int) extends CodeTree

  def sortCodeTreeFun(x: CodeTree, y: CodeTree) = {
    (x, y) match {
      case (x1: Leaf, y1: Leaf) => true
      case (x1: Fork, y1: Leaf) => x1.weight < y1.weight
      case (x1: Leaf, y1: Fork) => x1.weight < y1.weight
      case (x1: Fork, y1: Fork) => x1.weight < y1.weight
    }
  }

I tried to modify CodeTree constructor to be:

  abstract class CodeTree(weight: Int)

So that I can compare x and y directly, but compiler says:

“not enough arguments for constructor CodeTree: (weight: Int)patmat.Huffman.CodeTree”

Is there another way to shorten sortCodeTreeFun method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:54:53+00:00Added an answer on June 13, 2026 at 6:54 am

    You could simply say:

    def sortCodeTreeFun(x: CodeTree, y: CodeTree) = {
      (x, y) match {
        case (_: Leaf, _: Leaf)           => true
        case (x1: CodeTree, y1: CodeTree) => x1.weight < y1.weight
      }
    }
    

    And define abstract class CodeTree as

    abstract class CodeTree {
      def weight: Int
    }
    

    The reason for the error is that when you extend an class that take a parameter you need to supply the parameter. So for

    abstract class CodeTree(weight: Int)
    

    You need to extend it as

    case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree(weight)
    

    That’s what the error you were getting was saying:

    "not enough arguments for constructor CodeTree: (weight: Int)"
    

    It was because you weren’t supplying the required argument weight when extending CodeTree.

    The problem with this approach though is that weight is not a member of CodeTree and hence can not to be accessed from an instance of type CodeTree. That is to say, if you did:

     scala> Fork(...).asInstanceOf[CodeTree].weight
     <console>:11: error: value weight is not a member of CodeTree
    

    So, in your pattern match, you wouldn’t be able to do x1.weight since the type of x1 is a CodeTree and CodeTree does not have a weight.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this stored procedure that I want to use for my SAP crystal
I have this html code that i want to edit with jQuery. Here is
I have this code that I want to make point-free; (\k t -> chr
I have a bunch of classes that each have a property called Sequence. This
I have some function declared like this that check element for something(true, false): (function($)
I want to implement a String comparison function that doesn't take a different amount
I have this code and I want to have my button as a square,
I have this problem: I want to generate a new source code file from
I have this webservice snippet and i want to print the actual value returned.
I have this string in php i want to skip the ppattern ------------------------------------------ FROM:Andy;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.