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'.

Friday, January 20, 2012

Not So Generic

I was working on a post on Generics in Dart, had a significant amount written yesterday but was running into a couple of bugs (bug reports were, for the most part, previously submitted). Coming back today and looking at my post and trying to pick up where I left off, I decided the best option, for your sake and mine, was to delete it. I had a mess of things jumping from one topic to another, code samples that had far too much fluff and useless elements that it was far from clean, concise and illuminating. So I admitted my mind wasn't as focused as it should have been and deleted the post entirely. It wouldn't serve much purpose to try and clean it up at all.

I've decided for the time being I'm going to leave the generics to Seth Ladd's most capable post: Generics in Dart. I highly recommend reading it and pay particular attention towards the end of the post to help avoid some 'gotchas'. He does a great job explaining covariance and keeping it simple at the same time. The only additional thing I will add is that not only can you use generics on the initialization of a variable but also in the declaration as a type. Doing so will really help, particularly the editor, to keep a close eye on what you are assigning or adding to a variable. The only example I will show is very brief: http://try.dartlang.org/s/lywo
main() {
  var list1 = ["One", "Two", "Three"];
  List list2 = new List();
  List list3 = ["One", "Two", "Three"];
  
  list1.add(4); // No warning because the type isn't specifically declared.
  list2.add(4); // generates a warning
  list3.add(4); // Generates a warning
}

Assuming you are not running in checked mode, all three cases will run properly. However, list2 and list3 will generate errors on the add lines indicating that int is not assignable to a string. The first case, list1 will quietly and happily accept 4 as being added to it, even though we initially specified that the variable of Dynamic type list1 is a List of type Strings. If running in checked mode, all three will fail.

Apart from that, I decided that over the weekend I'm going to start working on an introductory tutorial to Dart. Something for the programmer with little experience. Many of the resources I've seen have been geared towards those experienced with Java programming or heavy JavaScript users. I'm debating if I should go with just VM initially and get the foundations down or just DOM initially so they can get right into results. Because lets face it, when you first start as a programmer we're all about the results and making the computer do something cool rather than all the theory and foundations. However I believe I can offer that with the VM on a starting level.

Thursday, January 19, 2012

Mapping the Future

So yesterday I looked at Lists. Today, I'm onto Maps. As has been the way, this is based off of the information I read from Seth Ladd's Blog series on Dart. Maps and Hashes in Dart.

Maps are a great utility in web programming especially. They are an efficient way to create a relationship between a 'key' to a value. In Dart values can be of any type, and similar to Lists, they do not have to be of the same type. That is a map can contain keys which are strings, numbers, and other classes including Lists etc.

Unlike some other languages though (Ruby for instance), the 'key' in a map must be a string. It cannot be an instance of a class for example. You can't use a number as a key (which would cause confusion with Lists). 99.99998% of the time this won't be a problem as there is only a very rare case you would need anything other than a string. I will admit though I did enjoy the lighter-weight use of 'symbols' in Ruby, particularly for maps. But different roles for the languages. [edit:] I see a note at the bottom of Seth Ladd's blog indicating that this may change in the future. [/edit]

In Dart, as in most (but not all other languages), when you query a map for a key that doesn't exist, null is returned. I much prefer this over the alternative of a runtime 'indexOutOfRange' error as I have seen in another language. So for instance if we have the following:
main() {
  var map = { "Hello" : "World" };
  print(map['Foo']);
}

The result is:
null

We can then do a simple check to see if the value is null and assign it if we have to. Its also extremely easy to add to a map. Even if the key doesn't exist, if we assign to it, it will be added. So for instance we can do the following:
main() {
  var map = { "Hello" : "World" };
  map["Hello"] = "Joe";
  map["foo"] = "bar";
  // Map now contains { "Hello" : "Joe", "foo" : "bar" }
}

So that's nothing new to people familiar with Maps from other languages and with those two methods we have an easy way to check for a value and assign one to it if it doesn't exist otherwise. However there is an interesting function that exists in Dart for Maps: putIfAbsent.
This function allows us to query a map for a key and returns its value. However if the key was not previously defined, it will call the function you pass to it and assign the return value to that key. Lets look at an example:
main() {
  var map = { "Hello" : "world" };
  var res = map.putIfAbsent('foo', assign() {
      // Do some stuff
      return 'bar';
    });
  print(res);
  print(map['foo']);
}
bar
bar

So as you can see map is queried for the key 'foo'. When it does not find it, it then calls the function (in this case I named it assign). You can write some code to calculate a value and then whatever is returned is the value that is assigned to that key in map. That value is then also returned by map.putIfAbsent and assigned to the res variable. Something important to note however: putIfAbsent checks if the key is absent, not the value of that key. That means if the key has already been assigned but is simply null, the method will return null and not call the function that you pass to it. As the following demonstrates:
main() {
  var map = { "Hello" : "world", "foo" : null };
  var res = map.putIfAbsent('foo', () => 'Bar');
  print(res);
  print(map['foo']);
}
null
null

This is the same as the containsKey method. The method verifies if the key exists in the map already, not if it has a non-null value.

Another interesting tidbit is if you want to run in checked mode, or at least generate warnings when working with maps, you have option to specify the types to be expected. As previously mentioned, at the moment the key must be a string, but the capability, using Generics, allows you to catch potential errors or surprises. Seth does a great job explaining the use of generics for Maps on his blog, so check it out there.

Wednesday, January 18, 2012

A List of List stuff

So I continued on reading Seth Ladd's blog post on Lists and Arrays in Dart. One thing I didn't see mentioned that I think should be said, is that Lists are not type dependent. For people familiar with current dynamic languages such as Python, Ruby, and JavaScript that's not a big deal. But for those coming from Java, C, and other similar languages it is important to note that a List doesn't need to be all the same type. For instance the following is entirely valid:
List list = [1, "two", 3.48283008, [4, 4, "four", 4]];

So a List can contact mixed variable types, numbers, strings, even more Lists. However for those familiar with Javascript and other such dynamic languages who are used to associative arrays, its important to know that Lists are not that. As such you cannot assign a value to an index that does not yet exist.
List list = new List();
list[0] = "zero";
list[1] = 1;
IndexOutOfRangeException: 0

However, for extendable arrays, you can call set on length and increase the value. So you can grow, or shrink with more than just the add or addAll methods. For instance the following is valid:
List list = new List();
list.length = list.length + 2; // Now have two null values in the list.
list[0] = "zero";
list[1] = 1;
list.length = list.length - 1; // This removes the last value from the list.

Something important to note however, currently the API does not specifically state that changing the length to a value lower than the current will always have this effect. Currently there is no guarantee that the values will be removed from the end of the List, or even that they will be removed at all. I can see a situation where, if the values at the end are not null, then they may not reduce the size of the List. That or when setting list size, the new value must be greater than or equal to current size. As the libraries are improved and change (and there will be change don't doubt that for a second), we'll get a better idea of what to expect.

There are a number of List methods which can be extremely useful, that require a function be passed to them as an argument. You can create these functions inline or pass the name of another function. I'll cover more on passing functions in another post. Some of these methods are: filter, some, every, sort. Some and Every return true or false values. every returns true if every value in a list meets the criteria defined by the function passed as an argument. some returns true if at least one of the values meets the criteria.
bool isNum(var e) => e is num;

main() {
  var list = [1, 2, "three"];
  print( list.every(isNum) );
  print( list.some(isNum) );
}

Gives the result:
false
true

Something to note is that the some, every and filter methods are defined as part of the Collection interface not specifically in List.

Something to make note of is that filter will return a new list of items that return true from the passed function, whereas the sort method will modify the existing list rather than return a new one. Take the following code as an example of how to use each.
main() {
  var list = [4, 3, 6, 1, 2, 5];
  list.sort(compare(a,b) {
    if (a == b) {
      return 0;
    } else if (a > b) {
      return 1;
    } else {
      return -1;
    }
  });
  // List is now [1, 2, 3, 4, 5, 6]

  var evens = list.filter(f(e) => e % 2 == 0);
  // list is still [1, 2, 3, 4, 5, 6]
  // evens is [2, 4, 6]
}

In the above code the list.sort method modifies the List we're working with, whereas the list.filter method returns a new List and does not make any changes to the existing list. One thing I'm not currently sure of is why the difference. Is it that sort is not removing or modifying the elements of the list where as filter does? To me anything that modifies the existing List, even if it's changing the ordering of the elements, should return a new List, which I can then assign back to the same variable if I so choose. As it stands now, I have to make a copy of the list prior to the sort if I want to keep the original ordering.

Another item to note, based on my limited trials, when iterating over the values of a List, elements are passed as value not reference. Thus if you make any changes to the value, they will not persist outside of the scope of the function. Here's an example of what I mean:
main() {
  var list = [1, 2, 3];
  for(var x in list) {
    x = x*2;
  }
  // List is still [1, 2, 3]
  
  list.forEach(f(x) { x = x*2; });
  // List is still [1, 2, 3]
}

In the first case, using the for loop I can understand that the elements are passed as values and that it does not modify the original list. However in the second case, using the forEach method, you would expect that any changes to the value there would be reflected in the list after the fact.

In any case. I highly recommend reading through the API references for List and for Collection to get an idea as to what other capabilities they hold.

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.

Tuesday, January 17, 2012

Some easy roads

I received some comments to my last two posts which is great. Always nice to see that it's being reading, even by a small audience. But these particular comments were nice as they were from Seth Ladd, who is a Developer advocate at Google. It's great getting feedback to my issues directly from the team working on the project. First comment was to submit bug reports, the other about some easier roads to take which I was totally unaware of and want to share.

But first regarding the bug reports for those of you who may be curious. I searched through the issue tracker and could not find a similar bug report for the issue I ran into with named arguments being passed to a function. So I created a new issue for that. Issue 1199. I verified the same example code works in dartboard and it does compile with frogc, but will not run in the VM. Take a look at the bug report if you're interested.

Thus far, I have been unable to consistently replicate the issue I'm having with the editor randomly greying out the 'Lunch in Dart Server' button and menu item. I'll continue to work on this to see if I can determine the cause and reproduce the issue. If I do I will also provide information on the bug report for that one.

Seth Ladd also shared with me a link to the root location of Dart Editor downloads. You can find that here: http://gsdview.appspot.com/dart-editor-archive-integration/latest/. Of note here are a couple of other files listed. In particular is: dart-linux-latest.zip which is a pre-compiled version of the SDK (from trunk, not bleeding edge). Also available are the MacOS and Win32 builds however I can't account for how well these may work as I'm running on my linux machine.

After my experiences yesterday pulling down the code, compiling and testing I do highly recommend using the above download unless you are specifically looking for Dartium. This will help save your system a little space and save yourself a little time an patience. I'm glad for the experience I had with pulling down the source but going forward I will most like be sticking the pre-built binaries from the above source to keep consistency with official builds and documentation. Here's to hoping for a pre-built dartium in the (near?) future!

That's all for now. May post more later today as I get some actual coding done ;)

Monday, January 16, 2012

Not named?

So as my previous post mentioned I now have the dart VM compiled and running. However, now I'm running into something new I didn't expect. Apparently the dartVM (from trunk) doesn't recognize the named function parameters as expected, or at all. Seth Ladd's blog post about Learning Functions for Dart explicitly states that optional function parameters can be named. Regardless of how I format the function and calling functions, I am receiving the error:
...unexpected token ':'

This is a little confusing as, while I realize I'm not using the bleeding_edge version of the VM, but it shouldn't be that old. Here's a link to the code I'm using http://try.dartlang.org/s/cgIo (it does work as expected in dartboard).
hello(msg, to, [from, rate]) => '${from} sent ${msg} to ${to} via ${rate}';

main() => print(hello('world', 'Seth', from:'Bob', rate:'First Class'));

Results on Dartboard:
Bob sent world to Seth via First Class

And the results in the VM (from both the dart editor console and from command line):
/home/mememe/dart/Test/Test.dart': Error: line 3 pos 44: unexpected token ':'
main() => print(hello('world', 'Seth', from:'Bob', rate:'First Class'));

Additionally. For some reason in the Dart Editor, if I highlight all my code and copy it and paste it else-where, when I come back to the Dart Editor and unselect the code (but make no changes to it), I can no longer click on "Launch in Dart Server" the icon becomes greyed out and I can't run the code from within the editor again until I close out of the editor and re-launch it. Again this is using Dart editor build: 3331.

I'll take some more time tomorrow to play around with the Dart editor and see if I can verify specifically what causes me to no longer be able to run the code in the server. (No amount of saving, or editing the code will restore the functionality that I've seen so far, short of closing out entirely).

Dart VM Install?.... Yes!

Firstly, allow me to comment that I have been corrected in how Dartboard works. In my post That's a Type of what?, I made mention that I believed that the Dartboard sends code to the server and is executed in the DartVM and the results are displayed back. I have found out that this is incorrect and in actuality the code is sent to the server which is compiled into JavaScript and then sent back to be executed client side. This is what results in the type confusions.

So to circumvent this confusion going forward I am going to work on today on downloading and compiling the dart VM. I will keep information throughout the day on how this process goes and any issues I may run into.
  • Step one: Install the the 32-bit libraries on my 64-bit machine. Following the automated setup instructions (very handy I might add). Running the install-build-deps.sh script is pretty quick and extremely handy. Though the initial question about installing the debugging symbols is a little poorly worded. I wasn't sure if I was being prompted to install the debugging symbols or all the packages. Hitting "N" I receive the message that it will not install the debugging symbols and starts to proceed with the installation of other packages (after asking me for a sudo password).
  • Step one (b): I'm prompted if I want to install the gold linker, which is apparently 5x faster than regular ld.. yeah sure why not. Lots of building and such occurs. No errors encountered that I notice. I wonder will the system detect this is installed automatically or will I have to pass that as an optional parameter myself? We'll see..
  • Step one (c): I'm prompted if I want to install the 32-bit dev libraries to build a 32-bit chrome on a 64-bit system that it will need to download a large number of debs then will extract the files it requires, create new debs and install them (and, one assumes, will remove the larger and more numerous original debs)... wait I thought this is what I was doing already anyways? Re-reading the Preparing Your Machine wiki again says that you may need to install the 32-bit libraries on a 64-bit system. Better hit yes just to be on the safe side
  • Righty, all done that... I notice it left a binutils-2.21.1 directory there... might need to get rid of that later. On to the next part, check out the code. Copy and paste.... and done. And export the path.. Oo no compiling this time yay.
  • Okay, now onto Getting the Source. Now I'm stuck with a choice... do I get 'everything' or do I get the standalone VM? I already have the Dart Editor installed. A little confused as to what additional packages there are to install. Does this install Dartium too? I kinda assume frogc was part of the VM package... well I've got a fair bit of space to install... might as well go ahead and do 'everything', to save me from having to come back and look for them after the fact if I did decide I wanted something else.
  • gclient sync ran into an issue... exited with a non-zero status code. I see a notice that I can't download DumpRenderTree. I'm advised to run a script gsutil config... yeah okay. Enter in the authorization code... blank project-id.. Okay done now...? Well lets try running gclient sync again. Uhm... ends saying Syncing projects: 85% done. I dunno did this work or not? Well only one way to find out. Time to try and build.
  • Building everything: Alright... into the dart directory and run the build... odd that I have to specify my arch as a 32 bit system. A quick groups search shows me that the 64-bit system isn't yet implemented. Hopefully that will change in the future, particularly as server side code starts rolling out. Looks like this may take a while. So I'm going to grab some lunch. -- Back from lunch and it appears that the build failed. Getting an error "Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-6-openjdk/lib/tools.jar" A quick google search shows I'm missing the openjdk-6-jdk package. I look where it expected to find the file and it only shows jre directory and such no lib directory. Quick apt-get install openjdk-6-jdk installs this for me. I double check and the file is where it expects. Still... odd though that it wasn't detected with the build deps.
    And bang, another error... This one a little more complex. Lots of information on the screen but it really boils down to this line: "runtime/vm/dart_api_impl_test.cc:1432:15: error: variable ‘result’ set but not used [-Werror=unused-but-set-variable]" Doing some looking around I see a few similar errors though not specifically that file/line. I double check the file and see the declaration and assignment to that variable but not used after that. Not passed or returned apparently. Odd. I double check the source online in both trunk and bleeding_edge. Both are showing the same thing. Looks like I have resort to the work around. I know right away that the work around will fix that error as the work around makes warnings just warnings and stops them from being errors. The work around is comment 8 as seen here.
  • Building has finished apparently, though I can't seem to see the Dartium build in there anywhere. But in anycase lets start with the testing... Oh look an error doesn't like the Chromium testing. Okay we'll just drop that part. Progressing VERY slowly for these tests. A few of them have timed out so far, haven't reached the results yet. Starting to think about if I really need the "All" option. Doesn't appear to have compiled the editor either (which isn't a big deal since I already have that downloaded from the dartlang page anyways.)
  • Eventually my tests finally complete (well over an hour). And I, for reasons I'm still not 100% sure of, decide in my infinite wisdom, decide "No, I only want the standalone VM". So I remove all the sources and start basically from the beginning. First I check out the "standalone.deps" and sync.
  • Now to build again. This time however I also make note to build as a release instead of as debug which is the default. Now to go through the similar steps to build (such as disabling warnings as errors on the compiler). Build is pretty quick. Now tests... wow much faster as well. None time out this time either.
Alright, so now that I have that downloaded and compiled lets give it a try right quick. Yep a simple "hello world" works exactly as expected from the command line. However when I try to run a test in the Dart Editor it just wants to compile it doesn't want to use the VM even after specifying the path.

Upon investigation I do see that there is a new Dart editor release available. I was previously using 3101. Latest build number shows 3331. I try downloading the latest stable release and sure enough it's the 3331 release. One really nice enhancement is that it allows you to decide when making an app if the application type is Web or a script. Choosing script, and having the VM path set automatically runs the examples in the console as well! Success!

In addition. Running the small little script from my previous post gives the following results:
true
true
false
false
false
false

Exactly what we would expect to see. Phew. So now as I progress learning the language I can use the dart VM directly as opposed to running in dartboard all the time. Should help my work flow as well.

Current I only have the dart VM running and did not bother with the compiler, Dartium, etc. I am curious for those of you familiar. Since Frog is written in Dart, does it require the compiler aspect to be compiled or will it work with just the VM as is?
Also for the Dartium build.. the Wiki instructions show to use bleeding_edge repository as opposed to using the trunk. Thoughts on this?

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.

Trials (and Errors) in Dart

So I've decided I'm going to start a new blog. This blog is going to be dedicated to my trials and experiments in Dart, generally referred to as dartlang. It'll contain links to various articles or tutorial on Dart wirtten by others; various code snippets and potentially some additional tutorials I write myself (yet to be determined).

Dart is a language actively being developed by Google as an alternative to JavaScript initially for use as a client side web scripting language. I'm not going to get into a lot of details on the background of the language as, that's covered pretty well on it's homepage. I highly recommend you check it out at: http://www.dartlang.org Additionally, be sure to check out the recently published FAQ Page.

Once your ready, head on over to the Getting Started and give it a try.

I also highly recommend checking out Seth Ladd's Blog. In particular, pay attention to his series on Dart, starting with Variables and Optional Types in Dart.