# a possible bug or a misunderstanding?

Hi Guys.

I'm trying to understanding the follow outcome.

I have the following 3 functions

def sum'(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long = {
return sumMultiples(upperLimit, firstMultiple)
+ sumMultiples(upperLimit, secondMultiple)
- sumMultiples(upperLimit, firstMultiple * secondMultiple)
}

def sum(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long = {
return sumMultiples(upperLimit, firstMultiple) +
sumMultiples(upperLimit, secondMultiple) - sumMultiples(upperLimit,
firstMultiple * secondMultiple)
}

def sumMultiples(upperLimit: Long, multiple: Int): Long = {
val n: Long = upperLimit / multiple
return (n * (n + 1) / 2) * multiple
}

I would expect that sum and sum' return the same result, but its not
the case. and I don't understand why.

The same differences can be replicated as follows

1 + 2 + 3 and the return is 6
1
+ 2
+ 3 and the return is 3

I'm not sure if it's a bug or just a misunderstanding. Please enlighten me.

ggarcia

### Re: a possible bug or a misunderstanding?

Am Montag, 4. April 2011, 20:08:14 schrieb Gilberto Garcia:
> Hi Guys.
>
> I'm trying to understanding the follow outcome.
>
> I have the following 3 functions
>
> def sum'(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long
> = { return sumMultiples(upperLimit, firstMultiple)
> + sumMultiples(upperLimit, secondMultiple)
> - sumMultiples(upperLimit, firstMultiple * secondMultiple)
> }
>
> def sum(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long
> = { return sumMultiples(upperLimit, firstMultiple) +
> sumMultiples(upperLimit, secondMultiple) - sumMultiples(upperLimit,
> firstMultiple * secondMultiple)
> }
>
> def sumMultiples(upperLimit: Long, multiple: Int): Long = {
> val n: Long = upperLimit / multiple
> return (n * (n + 1) / 2) * multiple
> }
>
> I would expect that sum and sum' return the same result, but its not
> the case. and I don't understand why.
>
> The same differences can be replicated as follows
>
> 1 + 2 + 3 and the return is 6
> 1
> + 2
> + 3 and the return is 3
>
>
> I'm not sure if it's a bug or just a misunderstanding. Please enlighten me.
>
> ggarcia

That's semicolon (non-)inference at work.
Your first function consists of three independent statements:
1) return sumMultiples(...)
2) + sumMultiples(...)
3) - sumMultiples(...)
Your function terminates on the first calculation due to "return"
If you remove the "return" your function will return
- sumMultiples(...)

In your second function the compiler/parser can tell that the command is not
finished yet because a trailing operator (+) or the still open bracket "("
does not allow the line to terminate with a virtual ";".

I don't know the exact rules of ";" inference out of my head but I make sure
to finish a line to be continued always in a way that tells me (and the
compiler) that there is still something missing. I.e. an operator requiring an
argument, an open brace or bracket / parameterlist.

My rule of thumb is: if I can read a statement up to the current line as a
valid and complete statement the compiler might also do so and infere a
semicolon.
If I don't want the statement to terminate I modify the line in question.

At the beginning of my scala ways I found this very strange and alienating.
But I got used to it...

Greetings
Bernd

### Re: a possible bug or a misunderstanding?

But the result is not what I expected, even without the explicit
return and the extra paranthesis.

I was justing formatting the code to avoid long lines and be a little

The fact is that I was expecting that

1 + 2 + 3 to be equivalent to

1
+ 2
+ 3

without the need to add extra parenthesis.

it turns that

(1
+2
+3)

gives the desired outcome.

thanks guys.

### Re: a possible bug or a misunderstanding?

On Mon, Apr 4, 2011 at 12:09 PM, Gilberto Garcia <giba [dot] dmb [at] gmail [dot] com> wrote:
But the result is not what I expected, even without the explicit
return and the extra paranthesis.

I was justing formatting the code to avoid long lines and be a little

The fact is that I was expecting that

1 + 2 + 3 to be equivalent to

1
+ 2
+ 3

without the need to add extra parenthesis.

1 +
2 +
3

works fine ... but you can't go wrong with parentheses as below and it helps with auto-indenters.

-- Jim

it turns that

(1
+2
+3)

gives the desired outcome.

thanks guys.

### Re: a possible bug or a misunderstanding?

your return value makes sense if you remove the explicit return. i think
you found a bug.

Am 04.04.2011 20:08, schrieb Gilberto Garcia:
> Hi Guys.
>
> I'm trying to understanding the follow outcome.
>
> I have the following 3 functions
>
> def sum'(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long = {
> return sumMultiples(upperLimit, firstMultiple)
> + sumMultiples(upperLimit, secondMultiple)
> - sumMultiples(upperLimit, firstMultiple * secondMultiple)
> }
>
> def sum(upperLimit: Long, firstMultiple: Int, secondMultiple: Int): Long = {
> return sumMultiples(upperLimit, firstMultiple) +
> sumMultiples(upperLimit, secondMultiple) - sumMultiples(upperLimit,
> firstMultiple * secondMultiple)
> }
>
> def sumMultiples(upperLimit: Long, multiple: Int): Long = {
> val n: Long = upperLimit / multiple
> return (n * (n + 1) / 2) * multiple
> }
>
> I would expect that sum and sum' return the same result, but its not
> the case. and I don't understand why.
>
> The same differences can be replicated as follows
>
> 1 + 2 + 3 and the return is 6
> 1
> + 2
> + 3 and the return is 3
>
>
> I'm not sure if it's a bug or just a misunderstanding. Please enlighten me.
>