{"id":396,"date":"2013-09-08T16:18:23","date_gmt":"2013-09-08T15:18:23","guid":{"rendered":"http:\/\/bryants.eu\/blog\/?p=396"},"modified":"2018-02-26T02:45:06","modified_gmt":"2018-02-26T02:45:06","slug":"plinth-isnt-dead","status":"publish","type":"post","link":"https:\/\/bryants.eu\/blog\/2013\/09\/plinth-isnt-dead\/","title":{"rendered":"Plinth Isn&#8217;t Dead!"},"content":{"rendered":"<p><strong>Update 2018-02-26: <\/strong>This was written several years ago, and Plinth development is essentially dead now. (In retrospect,\u00a0although it was true at the time, this was a bad title and it&#8217;s been annoying me ever since.)<\/p>\n<hr \/>\n<p>I thought I&#8217;d write a status update, since it&#8217;s been almost four months since I last posted anything here.<\/p>\n<p>Firstly, Plinth isn&#8217;t dead! I have been working on it, albeit slightly more slowly than I&#8217;d have liked, for the last four months. At first, that was because Generics took me a while to get right, as it required major structural changes throughout the compiler. More recently, I&#8217;ve just been away on a couple of holidays.\u00a0For the last few days, though, development has sped up significantly.<\/p>\n<p>Now, since this is a status update, here&#8217;s a list of all of the major things I&#8217;ve added since my last post:<\/p>\n<h3>Generics\u00a0[<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/661c50ca8a5acc2c93085dd75f031a0dc5e3eeae\">commit<\/a>]<\/h3>\n<p>This took much longer than it should have &#8211; partly because it changed so much of the compiler&#8217;s data structure, but also because code generation for it was much more difficult than I expected (see <a title=\"Native Types and Generics\" href=\"https:\/\/bryants.eu\/blog\/2013\/05\/native-types-and-generics\/\">last post<\/a>).<\/p>\n<h3>A Test Framework [commits: <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/6870959ef019e871b99004c1b3ef3c01df8355e6\">1<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/31133dcb43a4bdb48e608b082b47f1e9a2bf4237\">2<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/ccb77cbdd35445593e43e65b26717940aff2526f\">3<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/cfcb924dddd4fc662858df79f500277fe2b77db8\">4<\/a>]<\/h3>\n<p>There is now <a href=\"https:\/\/github.com\/abryant\/Plinth\/tree\/master\/test\">a suite of tests<\/a> that covers everything up to name resolution. I still need to write tests for: cycle checking, type checking, control flow checking, and exception checking.<\/p>\n<p>If you want to run all of the tests, just run `<code>ant run-tests<\/code>`\u00a0from the project root directory.<\/p>\n<h3>A new array-initialisation syntax [<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/e91facb2eb7954fbee6ce534ce11190d1e1db4d0\">commit<\/a>]<\/h3>\n<p>Before this change, it was impossible to create an array of anything that didn&#8217;t have a default value, unless you knew its size in advance. Here&#8217;s an example:<\/p>\n<pre>[]string foo = new [n]string;<\/pre>\n<p>^ This is impossible &#8211; you can&#8217;t create an array of not-null strings unless you know all of their values beforehand. Here&#8217;s what you could do before:<\/p>\n<pre>[]string foo = new []string {\"foo\", \"bar\"}; \/\/ only has two elements, not n\r\n[]?string bar = new [n]?string;             \/\/ all elements are nullable<\/pre>\n<p>But now, with this new syntax, you can create them much more easily:<\/p>\n<pre>[]string foo = new [n]string(\"hello\"); \/\/ initialises all of the strings to \"hello\"<\/pre>\n<p>Or, if you define a function somewhere first:<\/p>\n<pre>string makeString(uint index)\r\n{\r\n  return \"string number \" + index;\r\n}\r\n[]string foo = new [n]string(makeString);<\/pre>\n<p>This will become much easier to use once closures are added, which will hopefully be soon.<\/p>\n<h3>Wildcard types [commits: <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/4b7680e424a2fdc1e470a5564391af5acd2cf5c0\">1<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/94f5fbff6908bfe890d7e8345d0dafbef47de040\">2<\/a>]<\/h3>\n<p>Wildcards are useful if you want to store an object but don&#8217;t care about its generic type arguments. For example, a List&lt;?&gt; can only store one type inside it, but we don&#8217;t care what that type is. We can&#8217;t store new elements in it, but we can retrieve them as objects.<br \/>\nAdding wildcards required a lot of changes to the way run-time type information works, mainly because of cases like this one:<\/p>\n<pre>ArrayList&lt;?&gt; list = new ArrayList&lt;Bar&gt;();\r\nList&lt;? extends Foo&gt; list2 = cast&lt;List&lt;? extends Foo&gt;&gt; list;<\/pre>\n<p>To do this sort of check, we need to go through all of the super-types of the value being tested (<code>list<\/code>) for the target type (<code>List<\/code>), and then make sure the constraints on the wildcard type argument encompass the actual type argument we get from the object&#8217;s run-time type information. This gets especially complicated when you cast to a type like <code>List&lt;? extends Foo super Bar&gt;<\/code>, or when you have multiple type arguments, so the runtime&#8217;s <a href=\"https:\/\/github.com\/abryant\/Plinth\/blob\/master\/runtime\/core\/typeinfo.ll\">code for checking this<\/a> is a bit long (although that&#8217;s partly because it&#8217;s written in LLVM bitcode).<\/p>\n<h3>A copyright notice [<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/5dac61741ebac20fac5b5214c4a45496ad93748a\">commit<\/a>]<\/h3>\n<p>This should have been added a while ago, but the code is now officially under the three-clause BSD license, so you can use it for almost anything you want.<\/p>\n<h3>Array bounds checking [<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/995060a84d1a6630b73618bc1ae77f6d46f3b440\">commit<\/a>]<\/h3>\n<p>Now, if you index past the end of an array, you won&#8217;t get a segmentation fault any more &#8211; an IndexError will be thrown instead. Also, IndexError has been added to the standard library.<\/p>\n<h3>Various standard-library improvements [commits: <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/a40f829705b73b42c544a3e947428a70b52f0196\">1<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/1f1858c05c980e8eb81c38108db2f55148bd583c\">2<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/3184e026c3200eb597ec81ae87551c76c19eb71e\">3<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/a18ce8daf6d9a5025c98600ec921007820ae2d7e\">4<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/ba14e0938ca7dfb8b8a32e31ffe332ba98bd463f\">5<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/04ec5837b829ac64ba4f2888d4512664188fecf9\">6<\/a>]<\/h3>\n<p>Some new methods have been added to string, such as trim() which trims all unicode whitespace characters.<br \/>\nstring now has a read-only property called &#8220;length&#8221; instead of a method, which will be the standard way of exposing the length of something in the standard library (no size() methods on Lists!).<br \/>\nAlso, the stdin::readLine() method can now throw EOFException.<\/p>\n<h3>ArrayList&lt;T&gt; and LinkedList&lt;T&gt; implementations [commits: <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/984f900a6ae0be4e0d2cd20a852ef4ec0bfb0f73\">1<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/862c50022d2a56f806ac797d98fbf241ee26d4ad\">2<\/a> <a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/f8e905560764806b44f5f2dc4938f729202922a5\">3<\/a>]<\/h3>\n<p>These both implement a new List&lt;T&gt; interface, which extends Iterable&lt;T&gt;.<br \/>\nArrayList&lt;T&gt; is actually implemented as a ring buffer in an array. I&#8217;m not sure why this isn&#8217;t more standard in ArrayList implementations, but it makes insertions to and deletions from the start of the list O(1) with hardly any overhead.<\/p>\n<h3>For-each loops [<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/d73b01e56a9a898e22ebdaf1eb6edfc5fdf73d33\">commit<\/a>]<\/h3>\n<p>These loops are extremely flexible: they don&#8217;t just iterate through arrays and Iterables, they also work on integers (signed and unsigned) and Iterators. This means you can do things like this:<\/p>\n<pre>for uint x in 5\r\n{\r\n  stdout::print(\" \" + x); \/\/ prints: 0 1 2 3 4\r\n}\r\nfor short y in value\r\n{\r\n  stdout::print(\" \" + y);\r\n  \/\/ if value\u00a0== -5, prints: 0 -1 -2 -3 -4\r\n  \/\/ if value\u00a0== 0, prints nothing\r\n  \/\/ if value\u00a0== 3, prints: 0 1 2\r\n}<\/pre>\n<p>And this:<\/p>\n<pre>Iterator&lt;Foo&gt; iter = list.iterator();\r\nif iter.hasNext()\r\n{\r\n  iter.next(); \/\/ skip the first element\r\n}\r\nfor Foo foo in iter\r\n{\r\n  stdout::println(foo); \/\/ starts at the second element of the list\r\n}<\/pre>\n<h3>Auto-assign parameters [<a href=\"https:\/\/github.com\/abryant\/Plinth\/commit\/70a6dc193cf459fb9215ae901f34c15141982133\">commit<\/a>]<\/h3>\n<p>These are something I&#8217;ve wanted Java to have for a while, but they&#8217;re purely syntactic sugar. Basically, they let you define a constructor\/method(\/property setter) parameter which, instead of creating a local variable, just assigns to a field. It&#8217;s easier to show it than to describe it. Instead of writing this:<\/p>\n<pre>class Item\r\n{\r\n  string name;\r\n  string description;\r\n  uint price;\r\n  create(string name, string description, uint price)\r\n  {\r\n    this.name = name;\r\n    this.description = description;\r\n    this.price = price;\r\n  }\r\n  \/\/ ... getters and setters ...\r\n}<\/pre>\n<p>You can write this (also, you can use properties instead of writing getters and setters!):<\/p>\n<pre>class Item\r\n{\r\n  property string name;\r\n  property string description;\r\n  property uint price;\r\n  create(@name, @description, @price);\r\n}<\/pre>\n<p>The auto-assign parameters get assigned just before the body of whatever you&#8217;re calling.<br \/>\nWith this change, when you&#8217;re using an auto-assign parameter a body is added automatically, so you can put a semicolon instead of an empty body.<br \/>\nYou can also do some cool things like:<\/p>\n<pre>void setHalfPrice(@price)\r\n{\r\n  price \/= 2;\r\n}<\/pre>\n<p>This works because the parameter doesn&#8217;t create a local variable, so when you say &#8220;price&#8221; you&#8217;re actually referencing the property.<\/p>\n<p>&nbsp;<\/p>\n<p>That&#8217;s as far as I&#8217;ve got right now. Hopefully I&#8217;ll add default arguments and varargs soon. However, I do need to do some preparation for job interviews, so I&#8217;m not sure when my next post will be.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update 2018-02-26: This was written several years ago, and Plinth development is essentially dead now. (In retrospect,\u00a0although it was true at the time, this was a bad title and it&#8217;s been annoying me ever since.) I thought I&#8217;d write a status update, since it&#8217;s been almost four months since I last posted anything here. Firstly, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":7,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/396\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}