I am trying to reduce a group in Scala.
Below is my code:

```
 val record = file.map(rec => (rec.state,rec.gender,rec.aadharGenerated.toInt)).groupByKey(_._1)
    .reduceGroups((a,b)=>{
    var total = a._3 + b._3
    var mTotal = if(a._2.trim().equalsIgnoreCase("m")) {(a._3.toInt + b._3.toInt)}
    (a._1,total.toString(),mTotal)
    }).collect

```

In second last line I am getting compile time error "type mismatch; found : AnyVal required: Int"

The third parameter is supposed to be int. And the value I am adding are int already.
I am new to Scala, any help is appreciated.
Thanks in advance
['https://Stackoverflow.com/questions/49756059', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9323888/']
You're using `if` as an expression without an `else` for your `mTotal` variable. The return type of an `if` is the nearest common super type of both the *success* block and the *else* block.

If you don't provide an `else` block, Scala will **assume**:

```
else ()

```

`()` is the value of `Unit`. So, for `mTotal` to be an `Int`, you need to provide a fallback value with an `else` block.
I don't know your data, but your assertion: 

> 
> The third parameter is supposed to be int.
> 
> 
> 

doesn't hold. What if `a._2.trim().equalsIgnoreCase ("m")` evaluates to false?

```
scala> var mTotal = if ("a".equalsIgnoreCase("m")) {7} 
mTotal: AnyVal = ()

```