int i = 1.5

498 views
Skip to first unread message

bpar...@adobe.com

unread,
Oct 13, 2011, 5:37:06 PM10/13/11
to General Dart Discussion
What is the expected result for i in this expression? :
int i = 1.5

I am getting 1.5 in Dartboard but I was expecting 1.

Is 1.5 the correct result, or is this a bug?

Thank you,

- Bernd

John Tamplin

unread,
Oct 13, 2011, 5:41:10 PM10/13/11
to bpar...@adobe.com, General Dart Discussion
On Thu, Oct 13, 2011 at 5:37 PM, <bpar...@adobe.com> wrote:
What is the expected result for i in this expression? :
int i = 1.5

I am getting 1.5 in Dartboard but I was expecting 1.

Is 1.5 the correct result, or is this a bug?

Remember program behavior is unchanged if you remove the types, so if you did var x = 1.5, obviously you would expect it to contain 1.5 and not 1.   Dartc will give you a warning, and you can enable runtime type checks with --enable_type_checks if you like. 

--
John A. Tamplin
Software Engineer (GWT), Google

Florian Loitsch

unread,
Oct 13, 2011, 5:42:16 PM10/13/11
to bpar...@adobe.com, General Dart Discussion
On Thu, Oct 13, 2011 at 23:37, <bpar...@adobe.com> wrote:
What is the expected result for i in this expression? :
int i = 1.5

I am getting 1.5 in Dartboard but I was expecting 1.

Is 1.5 the correct result, or is this a bug?
Correct result.
Since type-annotations don't have any effect on the outcome of the program, your statement must behave equivalent to "var i = 1.5".
Dartboard warns however that you try to assign a double to an int.

// florian

Thank you,

- Bernd



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

bpar...@adobe.com

unread,
Oct 15, 2011, 7:10:00 PM10/15/11
to General Dart Discussion
Thanks for clarifying and congratulations to the Dart release.

Just a follow-up remarks:

1. I ran "java -jar dartc.jar hello.dart" with and without --
enable_type_checks and I got this warning or error for "var i = 1.5;"
in both cases:
hello.dart/hello.dart:3: double is not assignable to int

2. dartc's explains "--enable_type_checks" as "Generate runtime type
checks".
It seems that "var i = 1.5;" triggers a warning at compile time, not
at runtime.

3. I am having a hard time differentiating warnings from errors. For
syntax errors I get messages like:
hello.dart/hello.dart:3: Unexpected token 'IDENTIFIER' (expected ';')
To me those messages look the same as other messages.

4. If you run "java -jar dartc.jar hello.dart" a second time, you
don't get a warning.
I suspect that has to do with incremental compilation, because I do
get my warning back when I delete the "out" output folder.

That's all for now.

Best wishes,

- Bernd


bpar...@adobe.com

unread,
Oct 15, 2011, 7:15:24 PM10/15/11
to General Dart Discussion
Stupid typos in my last reply:
Replace "var i = 1.5;"
With: "int i = 1.5;"

I've been coding too much in JavaScript!

- Bernd

John Tamplin

unread,
Oct 15, 2011, 11:19:13 PM10/15/11
to bpar...@adobe.com, General Dart Discussion
On Sat, Oct 15, 2011 at 7:15 PM, <bpar...@adobe.com> wrote:
Stupid typos in my last reply:
Replace "var i = 1.5;"
With: "int i = 1.5;"

I've been coding too much in JavaScript!

int i = 1.5 isn't a runtime type error when compiled to JS with --enable_type_checks because double and int are both of JS type Number.  We might be able to special-case typechecks of int to check that the value is integral rather than just relying on the run-time type of the JS value. 
Reply all
Reply to author
Forward
0 new messages