found: Unit required: Int - How to correct this?

后端 未结 3 842
萌比男神i
萌比男神i 2021-01-15 22:55
scala> class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length); def getWidth ():Int = println (Width); }
:11: error:         


        
相关标签:
3条回答
  • 2021-01-15 23:27

    The error message is telling you that println has the return type Unit, so since your getLlength method returns the result of println, it will return Unit. This is an error because you said that getLlength should have a return type of Int.

    If you want your getLlength method to print the length, you probably should change its return type to Unit (and change the name to something that makes this clear).

    If you want your getLlength method to return the length, your should not be calling println, but rather just return the length.

    0 讨论(0)
  • 2021-01-15 23:30

    You can use a structure something like this if you want to print out the values of Length and width as well as keep the return type of your methods to be Int.

    class Rectangle (Length: Int, Width: Int) {
     def getLlength (): Int ={ println (Length); Length}
     def getWidth ():Int = {println (Width); Width}
     }
    

    what happens in scala is that the compiler treats the last line of the method always as a return statement and hence when you were just writing a println() inside your method body the Scala compiler was assuming that to be your return statement as well and in Scala return type of println() is Unit that is why you are getting this error. So if you want Int as your return type you just need to add one more line as the last line of your method which is your Int parameter Length or Width.

    0 讨论(0)
  • 2021-01-15 23:46

    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 讨论(0)
提交回复
热议问题