0

When Some cases in programming java or scala i got an error

type mismatch; found : Unit required: String 

that because of my domain or entity g/set methods got the error.

Why i got this error? and

How i can overcome the error?

for example i will show my entity and error codes in scala programming

domain

//@BeanProperty this line commented @Column(name="USERNAME") var userName:String=null def getUserName() { this.userName = userName } //@BeanProperty @Column(name="PASSWORD") var password:String=null def getPassword() { this.password = password } def setPassword(password:String) { this.password = password } 

and error got here

@Transactional(propagation = Propagation.REQUIRED ) def loadUserByUsername(userName: String): UserDetails = { log.info("==>calling loadUserByUsername for the user " + userName); val userAccount: UserAccount = findUserAccount(userName); new User(userAccount.getUserName(), userAccount.getPassword(), here error ---^ ^---- and here true, true, true, true, getGrantedAuthorities(userAccount)); } 

the compilation error shows :

Multiple markers at this line - type mismatch; found : Unit required: String 

if you know the about these please share her..

With Regards MILANO :)

8
  • 3
    Your getters look like setters... Commented Jul 11, 2013 at 10:35
  • 2
    It would probably help if we knew the definition of User and UserAccount Commented Jul 11, 2013 at 10:35
  • 1
    @milano but can you attach the code of User/UserAccount so we could look if there any issues with the .get* methods? Commented Jul 11, 2013 at 10:37
  • 1
    "i don't know actually create setters and getters in scala" That seems to be the problem here. Unit is similar to void in Java. And the "getter" you show looks "void" to me. Commented Jul 11, 2013 at 10:38
  • 1
    maybe your "getter" should return a String? Commented Jul 11, 2013 at 10:45

2 Answers 2

5

As Thilo already mentioned, your getters are setters. If you want to return e.g. the username you have to write something like this:

def getUsername():String = { this.username } 

Important is the = sign, which means, that the function will return (in this case the username ). :String defines the return type. Without the = it is like a void method in java, but your User object expects a String here, while your getUsername() is a function without return type (= Unit):

new User(userAccount.getUserName(), userAccount.getPassword()) 
Sign up to request clarification or add additional context in comments.

Comments

3

Unit means the same in this context as void in Java. That means: Your getUserName method returns nothing. Therefore, the compiler complains rightfully that it cannot assign the value of getUserName (of type Unit) to a String variable.

This is because there is an error in the getUserName method. It's not doing what you want it to do. And I propose that this is because a misunderstanding on how to return and assign values. Let's take a look on your example method first:

def getUserName() { this.userName = userName } 

First, note that in this context, this.userName and userName refer to the exact same instance variable. So, you are just overwriting the value of userName with the same value it contained before.

Here is what I assume you were thinking: "this.userName is the return value of getUserName, so I need to assign userName to it, so that the caller will get this value in return."

That's what I meant with the misunderstanding above. In Scala, you do not assign the return value of a function to a variable. Instead, you assign the function itself to a value. Look at this:

 def getUserName() = userName 

This means exactly what you want.

More than that, in Scala, you typically leave out the () after the method name if the method does not modify the state of the object. That would mean we write:

def getUserName = userName 

However, a method that changes the internal state of the object should have the (). Example:

def resetUserName() {userName = ""} 

Now, what about the braces?

Let me give you an example. Assume you want to write a message to the console every time the getter is called. For debugging, maybe. Your method would look like this:

def getUserName = { println("User name was requested.") userName } 

You see a block of code here, after the equals sign. The return value of the function is the value of the last statement that is executed inside this block. Simple enough, we see it will be userName, which is what we want.

Like in Java, you can also use the return command in Scala. It is discouraged, but since it's supported by the syntax, I think it's good to mention it as well. Using the return statement, you can program your code just like you know it from Java:

def getUserName() { println("User name was requested.") return userName } 

Note the absence of the equals sign. When you explicitly use the return statement, you don't need the equals sign.

I hope that helps. I suggest further reading on the syntax of the Scala programming language. Any good Google search result will do. The resources linked on the Scala home page are recommendable. For more depth, I recommend the book on Scala by Martin Odersky. It's pretty good.

1 Comment

I'd add that {} without '=' is deprecated, Martin Odersky said lately that it was introduced to look more familar for Java newcomers but now it's only a point of confusion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.