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 7980165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:59:01+00:00 2026-06-04T09:59:01+00:00

scala> class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println

  • 0
scala> class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length); def getWidth ():Int = println (Width); }
<console>:11: error: type mismatch;
 found   : Unit
 required: Int
       class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length:Int); def getWidth ():Int = println (Width:Int); }
                                                                                     ^
<console>:11: error: type mismatch;
 found   : Unit
 required: Int
       class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length:Int); def getWidth ():Int = println (Width:Int); }
                                                                                                                                 ^
  • 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-04T09:59:03+00:00Added an answer on June 4, 2026 at 9:59 am

    I can tell you where you’re going wrong, but it’s not entirely obvious what it is you want to do here.

    You’ve declared the getLlength (sic) method to be returning a type of Int. However, the body of this method is just a println call, and so it will return the result of this call. println has a return type of Unit (since it returns nothing, and is executed only for its side effects).

    Hence your method is declared to return Int, but you’ve implemented it in such a way that it’s return type would be Unit. Which is why you get a compile-time error.

    Do you want your method to return the length? If so, you should just return Length rather than printing it. However, an even better way to do this is Scala would be to mark the constructor parameter as a val, which will automatically generate an accessor for it:

    class Rectangle(val Length: Int, ...
    

    Now anyone with a reference to e.g. a Rectangle rect object can call rect.Length to get the length. This is in fact the preferred way to access fields and field-like properties in Scala, rather than the getFoo convention that Java uses.


    If you really just want a “container” class that allows someone to access named parameters, consider case classes. You could define a functionally equivalent class as

    case class Rectangle(length: Int, width: Int)
    

    This automatically generates accessor methods for both fields, sensible toString(), equals() and hashCode() implementations – and you also get pattern matching as an added bonus.

    Any time you want a class which is mainly/entirely just a bundle of fields, case classes should be your starting point.


    Note as well that variables in Scala are lowerCamelCase by convention. Declaring fields/constructor parameters with upperclass names (i.e. Length instead of length) is surprisingly confusing for people reading your code, so you will do yourself and everyone else a favour by sticking to convention.

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

Sidebar

Related Questions

If I define a Scala class: class X(i:Int) { println (i) } How do
Foo.java public class Foo{ public int i = 0; } Bar.scala class Bar() extends
Suppose I have a simple class in Scala: class Simple { def doit(a: String):
I have the following scala class definition (found in a paper), modeling categories: trait
Consider this simple Scala class: class A(val d: Int) Is there a difference in
I have a scala class: class Foo { def this(st: String) { var baz
I have the following Scala class: case class Person(firstName: String, lastName: String, age: Int)
In a Scala class, I can conveniently declare the return type of a method
Have a look at this Scala class: class Example { val (x, y): (Int,
Say I have a Scala class with default arguments class MyClass(p1: Int = 0,

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.