Elm 0.3.6
JSON support and better error messages

Elm can now handle JSON objects! The JSON library also provides basic dictionary functionality (a mapping from strings to anything). In addition, this release includes lots of internal improvements, making the compiler much more solid. The most noticeable part of this is improved context information for type errors. The type system is also a bit more restrictive now, so there may be some breaking changes. See the Ints and Floats section for more information on this.

The examples section of the website has also been cleaned up a bit to make it easier to find out about Elm’s newer features.


JSON Support

This release comes with a JSON library for native manipulation of JSON objects (docs). The examples listed here show this library in action.

The JSON library is more general than it appears at first. The underlying data structure it uses is just a mapping from strings to values, and those values can have any type. In other words, something of type JsonObject a is the same as something of type Map String a. In effect, you can use this library as a dictionary if necessary. I hope to provide the more general version (Map k v), and I would be really happy if someone was interested on working on something like this! (Email the list if you are interested)

Side Note: Once the general Map k v is implemented, Elm will also have sets. It turns out that Map k () is equivalent to Set k, so very little extra work needs to go into a working set implementation.


Type System Improvements

The visible changes to the type system are as follows:

  • Type error messages now give better context.
  • Elm now supports super-types. Unfortunately you cannot define them yourself yet. The internal stuff added for this should make it possible to add typeclasses in a future release. The most important super-type is probably the “Comparable” super-type which allows the comparision of any values of type {Int,Float,Char,String}. This makes it possible to make Map and Set libraries.


Ints and Floats

Elm’s parser now handles decimal numbers. I had put this off because it required super-types or typeclasses to be implemented in a sensible way. This is no problem now that Elm has super-types (and the internal machinery for typeclasses).

To be parsed correctly, decimal numbers must have a number before the decimal point (much like Haskell).

Possible Problems

Part of adding decimal numbers is a move towards more sensible types for numbers. The type system distinguishes between Ints and Floats instead of just calling everything a Number. For instance, 42 is a type in {Int,Float}, and its concrete type is determined based on how it is used.

The trouble arises because there is now a need to distinguish between division and integer division. Rather than using the same operator for both, I have chosen to use (/) for division and div for integer division (same as Haskell and SML, similar to OCaml).

Unfortunately, this may cause errors in code that was previously correct. I believe any broken code can pass type checking by replacing all uses of (/) with `div`. In some cases you may want normal division. To help with this, new functions have been added to convert between ints and floats:

  • truncate, round, floor, ceiling :: Float -> Int
  • toFloat :: Int -> Float

While I was adding these functions I figured it couldn’t hurt to add a couple other useful ones:

  • (^) :: Number -> Number -> Number where every part has the same type (all integers or all floats).
  • e :: Float

I think the main issues will come from functions like size :: Int -> Int -> Element -> Element and width :: Int -> Element -> Element which require integer arguments. I have found that code that uses collage is likely to have issues as well. Otherwise, it has not been an enormous problem in my experience.

I apologize for any pain this causes, but I think it is for the best. If you are having problems getting something working because of this, please email the list about it!


Minor Improvements and Bug Fixes

  • Foreign import/export statements no longer have to preceed all other variable and datatype definitions. They can be mixed in, making things a bit more readable/natural.
  • toText now properly escapes strings, so you cannot sneak HTML in through strings. This also means newlines are actually respected!
  • Correct castJSTupleToTupleNwhich was totally broken.
  • foldr1took the leftmost element as the base case instead of the rightmost.
  • Fix minor display issue in latest version of Chrome.
  • Fix behavior of [ lo .. hi ] syntax (now [4..0] == [], not [0]).




Comments are closed.