Showing posts with label dartboard. Show all posts
Showing posts with label dartboard. Show all posts

Monday, January 23, 2012

Wat?

Yesterday, Seth Ladd posted a link to a humorous video which highlights a few WAT?!?! moments in Javascript and Ruby. He then discussed how Dart deals with some of these moments in Javascript WAT moments and Dart. Please watch the video and read Seth's blog prior to reading.

There are WAT moments in virtually every language. A line from the video says something along the lines of So since it is the plus operator, we can expect the same result if we switch the operands around.. In the case of the video it was discussing how Javascript handles adding an array and a hash, and a hash and an array. However this reminded me of the issue I ran into myself already in experimenting with Dart. Look at the following code:
main() {
  print("wat" + 1);
  print(1 + "wat");
}

What is particularly interesting is running this example in the variety of locations. First lets run it from the Dartboard, without checked mode. We get the result:
wat1
1wat

More or less what you'd expect to get adding a string and a number. We do see a little warning off to the side indicating String is not assignable to num. But being not in checked mode it does run and because its Dartboard it falls back to the Javascript implementation of number + string. Well lets see what we'll get in the DartVM...
wat1
Unhandled exception:
NoSuchMethodException - receiver: 'wat' function name: 'addFromInteger' arguments: [1]]
 0. Function: 'Object.noSuchMethod' url: 'bootstrap' line:360 col:3
 1. Function: 'IntegerImplementation.+' url: 'bootstrap_impl' line:1469 col:32
 2. Function: '::main' url: '/home/mbutler/dart/Test/Test.dart' line:3 col:11

Err WAT? Even more, that runtime error is.. far from clear on just what exactly is going on. Additionally in the VM we do get the same warning as stated above, on the line with the issue so that helps to track it down easy enough. Well lets go back to the dartboard and enable checked_mode:
wat1
Failed type check: type String is not assignable to type Number

Well that's kind of a reasonable error. Must get the same in the Dart VM using the 'enable_type_checks' flag.
wat1
Unhandled exception:
'bootstrap_impl': Failed type check: line 1468 pos 22: type 'OneByteString' is not assignable to type 'num' of 'other'.
 0. Function: 'IntegerImplementation.+' url: 'bootstrap_impl' line:1468 col:22
 1. Function: '::main' url: '/home/mbutler/dart/Test/Test.dart' line:3 col:11

Okay WAT? That error is almost as long as the first one but its a little more obscure I think. I see from the very last line of the error where the issue is in my code (I notice this now in the first error as well. I'm so used to seeing my code point at the top of the error with subsequent library calls following after that, that I didn't realize to look at the bottom of the error for my error). But the error itself is interesting "OneByteString" is not assignable to type "num" of "other"...? Of other what? String not assignable to num is understandable though not necessarily expected. But of other? I'm not sure. Just rambling a little bit at this point as well as I procrastinate getting some work done in the office today.

I'll post again later today if I get the opportunity as Seth has a couple of other blog posts written that I want to follow up on. Particularly on his one about 'this'.

Wednesday, January 18, 2012

A List of Errors.

Before I get started on this post on Lists (actually, I already started it yesterday but didn't complete it yet ;), I did want to share the information that Seth Ladd recently shared regarding the Dart issue tracker. Sometimes it can be hard to locate or you just can't recall the location to access the tracker. Well never fear! There is a new redirect URL available to use.
dartbug.com
Redirects to the main issue list.
dartbug.com/new
Redirects to the new issue template.
dartbug.com/1199
Redirects to a specific issue number (in this case 1199, the one I submitted and mentioned in my last blog post).
So I'm continuing with Seth Ladd's series on Dart. Today I'm looking at Lists and Arrays. One particular quote from the post particularly grabbed my attention and interest:
Fun fact: As of 2011-12-22, the word "array" does not appear in the Dart spec.'
This interested me as virtually all languages have arrays and refer to them as such. But sure enough a quick search through the current language spec reveals no results for 'array'. Now, as stated in Seth's blog, Dart arrays are Lists. I found it an interesting point none the less.

Interestingly enough however, the VM does output an error which refers to arrays. But when compiled to Javascript (via frog or dartc) the errors properly reflects 'lists'. First I'll demonstrate the code and error then I'll actually provide some additional comments regarding the code.
main() {
  var list = new List(3); // Create an immutable, or 'non-extendable' list
  list.add(4); // Now try to change it (extend it).
}

This results in the following error:
Unhandled exception:
UnsupportedOperationException: Cannot add to a non-extendable array

But when executed on the dartboard we get the following error:
UnsupportedOperationException: Cannot add to a non-extendable list

It surprises me a little that the VM does specifically reference array whereas the code compiled to Javascript, which does have arrays, correctly references a list. There already exists a couple of bug reports. One which I'll only briefly mention, indicates that the word 'array' should be removed from all internal libraries. The second, which I added a comment to regarding the VM specifically is: Issue 1028. This issue aims to be more encompassing removing references from the Libraries as all references which may become visible.

So you may be wondering why it is that List provides an add method if it just generates an error. Well that's because there are two types of lists. A 'non-extendable' list which is created by passing a size to the constructor, or you can create a list without the optional argument to the constructor which creates an empty list you can extend by adding (or removing) values from. An extendable list can also be created from a list literally such as:
var list = [1, 2, 3];

Note however, that unlike some other languages (such as Ruby), you can not call a method on the List literal. Thus the following is not valid code:
var list = [1, 2, 3].add(4);

You'll receive a warning indicating "expression does not yield a value". But it will compile and run (at least until you try to call a List method on list such as length which will generate a runtime error.

However there is a bug in the add method which I have just discovered. The API for List.add indicates that the return type is 'void' (or no return value). This works as expected in the dartboard (javascript). However in the Dart VM, list.add(value) returns the new size of the list. I have submitted a bug report for it here: Issue 1213.

Friday, January 13, 2012

That's a type of what?

So I'm playing around with the Dartboard again this morning. Firstly, dartboard is purportedly an interface which sends the Dart code to the dart VM on a server, and displays to output, as opposed to compiling the script to javascript. This is of particular note, as I will mention briefly.

First, the code. Here's the link: http://try.dartlang.org/s/csQn

main() {
  int x = 10;
  double y = 10.3282;
  print(x is Dynamic);
  print(x is num);
  print(x is double); // Int is a double? (plausible granted)
  print(y is int); // Double is an int?? (bye bye precision?)
  print(x is bool);
  print(y is bool);
}

And the results:
true
true
true
true
false
false

That's right. Not only is an int a double, but a double is an int. I previously mentioned the fact that we are supposed to be connected to the dart VM through the dartboard is of significance, as this topic has come up within the dart groups. In particular, Peter Ahé mentions:
Dart is a new programming language. If you want to see it as we intended it to work, use the Dart VM.

On the Dart VM, int and double are different.

Now I realize that Dart is 1) still a new language under development and has bugs still to be worked out and 2) Primarily designed to be a dynamic language. However I can see issues arising from this in a few special cases. Additionally, based on Seth Ladd's blog post:
The is check is not affected by the declared types, instead it is checking what the type of the variable actually is.
Unfortunately at this time I do not have the Dart VM compiled and installed on my local machine which is why I've been relying on the dartboard for the time being. If anyone has the VM installed please try running the above and let me know if your results differ.

Thursday, January 12, 2012

Ranting Runtime

So to start things off, I decided I would look at a slice of code I had an issue with yesterday. Now first off know that the error in the following code was entirely intentional. I was looking to see how dart, and Dartboard would handle various errors. Looking to see if I would receive a warning, error or runtime error.

First a link to the dartboard (and I'll paste the code in as well until I can figure out how to embed the dartboard into here).

class States {
  static final ON = 1;
  static final OFF = 0;
}

main() {
  var test = new States();
  print('Give try.dartlang.org a try.');
  print('On is: ${States.ON}');
  if(test is States) print('Test is a State instance');
  // The following never prints?
  print('test.ON is: ${test.ON}');
}

Which produces the following output:

Give try.dartlang.org a try.
On is: 1
Test is a State instance

That's it.. no error, no warning. Just did not ever print anything from the final statement. That struck me as really weird. But what I figured was probably happening is that dartboard was receiving a runtime error but didn't display it at all. Still it seemed off to me.

However, after writing a post on Google+, later in the day the dartboard did start reporting the runtime error that was being generated and produced the following:

NoSuchMethodException - receiver: '' function name: 'ON$getter' arguments: []]

Yay! Errors... but errr huh? NoSuchMethodException.. yeah okay I can see that, it's looking for getter method for a class property. But why is the receiver blank? Receiver should be the 'test' instance of the States class. And the arguments are... err what? Okay I can see maybe showing an empty array for the arguments (thus the '[ ]' ) but why am I seeing the extra ']' ? But I well realize that the VM is still in alpha. I've not had a chance yet to go through the issue trackers to see if these are known bugs but being that they are so prominent I have reason to believe they are.