|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +post-type: blog |
| 4 | +by: Felix Mulder |
| 5 | +title: "Awesome Error Messages for Dotty" |
| 6 | +--- |
| 7 | + |
| 8 | +One thing that really excites me about being part of the core group of |
| 9 | +developers working on [Dotty](http://lampepfl.github.io/dotty) is my chance to |
| 10 | +impact usability. A lot of thought has gone into designing Dotty to be as fast |
| 11 | +and structurally sound as possible. Now comes the next step - adding a new |
| 12 | +level of usability for the compiler and the surrounding tools. |
| 13 | + |
| 14 | +We've looked at how other modern languages like |
| 15 | +[Elm](http://elm-lang.org/blog/compiler-errors-for-humans) and |
| 16 | +[Rust](https://blog.rust-lang.org/2016/08/10/Shape-of-errors-to-come.html) |
| 17 | +handle compiler warnings and error messages, and come to realize that Dotty is |
| 18 | +actually in great shape to provide comprehensive and easy to understand error |
| 19 | +messages in the same spirit. |
| 20 | + |
| 21 | +Let's dive right into some examples, let's say you have this code: |
| 22 | + |
| 23 | + try { |
| 24 | + foo() |
| 25 | + } |
| 26 | + |
| 27 | +It doesn't really make sense to put this in a `try`-block for two reasons: |
| 28 | + |
| 29 | +1. It doesn't throw an exception |
| 30 | +2. It doesn't have a `catch` or `finally` clause |
| 31 | + |
| 32 | +So let's say we compile this file using scalac, we get something like: |
| 33 | + |
| 34 | +<pre> |
| 35 | +test.scala:2: warning: A try without a catch or finally is equivalent to putting its body in a block; no exceptions are handled. |
| 36 | + try { |
| 37 | + ^ |
| 38 | + one warning found |
| 39 | +</pre> |
| 40 | + |
| 41 | +This is helpful, but it has a couple of drawbacks: |
| 42 | + |
| 43 | +1. If we have a bazillion errors, it will be hard to read |
| 44 | +2. If we don't know about `catch` or `finally` blocks - we don't know how to |
| 45 | + solve this (yes I know, most people do know what these are but - toy |
| 46 | + example!) |
| 47 | + |
| 48 | +So what do you get with Dotty? This: |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +All errors are now visually separated, and the output is colorized so that you |
| 53 | +can find your mistakes quickly. |
| 54 | + |
| 55 | +Another one of our goals is to be able to properly explain things when asked. |
| 56 | +As such, if you pass the flag `-explain` when compiling the example above, |
| 57 | +you'll get a more verbose explanation: |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +Mistyping members |
| 62 | +----------------- |
| 63 | +Sometimes, especially when you're in a rush - you might mistype some members. |
| 64 | +Currently we offer you the following support when selecting on a type: |
| 65 | + |
| 66 | + class Foo { |
| 67 | + def bar = ??? |
| 68 | + } |
| 69 | + |
| 70 | + val foo = new Foo() |
| 71 | + foo.barr |
| 72 | + |
| 73 | +Will yield: |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +In the future we want to be able to offer you these types of suggestions on |
| 78 | +other things like missing imports. |
| 79 | + |
| 80 | +Type diffs |
| 81 | +---------- |
| 82 | +Sometimes when working with complex types - it's hard to see exactly where the |
| 83 | +error occurs. The Dotty compiler will in these cases give you a colored diff: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +It will not do this however if the differences are huge - but it will syntax |
| 88 | +highlight the found and expected type anyway. |
| 89 | + |
| 90 | +We want you! |
| 91 | +------------ |
| 92 | +To make the transition to these new error messages as quick and pain-free as |
| 93 | +possible - we need help! This is a perfect entry-point into hacking on the |
| 94 | +compiler as you'll need to create semantic objects that contain the relevant |
| 95 | +information for the error or warning. |
| 96 | + |
| 97 | +So - this is what you do: |
| 98 | + |
| 99 | +1. Go to the [Error messages issue](https://github.com/lampepfl/dotty/issues/1589) |
| 100 | +2. Read the howto on error messages |
| 101 | +3. Choose an error message you want to help with and post a comment saying |
| 102 | + which one(s) |
| 103 | +4. Get hacking |
| 104 | +5. Submit a PR! |
0 commit comments