Int-variable is calculated as a variable of type String

I have an expression:

var level:Int = Core.COUNT_QUESTIONS_IN_EPISODE * currentEpisode.get__index() + currentEpisode.get__currentQuestion();

where

Core.COUNT_QUESTIONS_IN_EPISODE is 30
currentEpisode.get__index() is 1
currentEpisode.get__currentQuestion() is 8

all this variables are Int-variables.

But I get the result is 308, although it must be 38.

Try using type checks:

var level:Int = (Core.COUNT_QUESTIONS_IN_EPISODE:Int)
	* (currentEpisode.get__index():Int)
	+ (currentEpisode.get__currentQuestion():Int);

The parentheses are mandatory.

Breaking it up into different lines isn’t mandatory, but it helps. This way, when you get a “String should be Int” error message, you’ll be able to see based on the line number that the error came from get__currentQuestion(), not get__index() or COUNT_QUESTIONS_IN_EPISODE.

Okay, but why it happens? Now, in complex expressions I need to do casting of types, even if types are the same?

Are you sure currentQuestion is an Int, and not a String?

For example:

var level:Int = 30 * 1 + "8";

However, what platform are you targeting? It seems like this sort of thing would throw a compile error. Perhaps something is wrongfully typed, like __currentQuestion is meant to be an Int, but there’s a Std.parseInt missing for conversion from a JSON or other data object somewhere?

Plaform - HTML5. I’m sure that all variables have type Int.

You could also do this to be sure:

var count = Core.COUNT_QUESTIONS_IN_EPISODE;
var index = currentEpisode.get__index ();
var current = currentEpisode.get__currentQuestion ();

trace ("Is count really an Int? " + (count + 1 != count + "1"));
trace ("Is index really an Int? " + (index + 1 != index + "1"));
trace ("Is current really an Int? " + (current + 1 != current + "1"));

If you don’t supply type hints, Javascript seems to be quite happy to turn numbers into strings and vice versa. For instance:

(1 + "5") * 2
30

It combined 1 and "5" to make "15", then automatically turned that back into an int so it could be doubled.

You shouldn’t have to. This is just to help diagnose the problem.

Can you post your get__currentQuestion() function?

That can’t be the case if my suggestion worked.

This is more simple example.

var newIndexEpisode:Int = currentEpisode.get__index() + 1;
trace(newIndexEpisode);

currentEpisode.get__index() returns 1, but in trace I see 11.
This is currentEpisode.get__index() function:

public function get__index():Int { return _index; }
trace ("Is count really an Int? " + (count + 1 != count + "1"));
trace ("Is index really an Int? " + (index + 1 != index + "1"));
trace ("Is current really an Int? " + (current + 1 != current + "1"));

I can’t check it, because appears an error: “String should be Int”.

That means that compile-time typing is working fine. Which isn’t surprising: Haxe is pretty type-safe, while Javascript isn’t. Makes sense that the problem would happen only at runtime.

Try this instead:

trace(Type.typeof(count));
trace(Type.typeof(index));
trace(Type.typeof(current));

Sounds like it returns "1", not just 1. They’d show up the same if you traced them, but only the string would make it add up to 11.

And I assume _index is declared something like this:

private var _index:Int;

That’s nice and all, but when targeting Javascript, none of this guarantees that _index will be an integer. You can still assign a string to it. Or an array. Or an arbitrary object.

Try this code to see what I mean:

_index = 1;

trace("Round 1:");
trace(get__index());
trace(get__index() + 1);

_index = cast "1";

trace("Round 2:");
trace(get__index());
trace(get__index() + 1);

Output:

Round 1:
1
2
Round 2:
1
11

In trace I see:

TClass({
	__name__ : [String]
})

Yes, it is.

Yes, I have too.

But why in some cases Javascript converts Int to String, and in other cases doesn’t?

It isn’t random, I know that much. Javascript converts between types when it needs to, and leaves them as-is otherwise.

None of the code you’ve posted is the cause of this problem; it’s just the effect. At some earlier time in your program, you set _index to a string value. Try to figure out where.

Yes, you are right. When creating an object I sent value for variable _index received from openfl.utils.Object. And, apparently, by default these values are String. I converted it to Int and now code works fine.

Thanks for help!