My Notes on Wren, the Language of EOS

Earlier today I was skimming through the EOS Telegram and noticed this comment from @dantheman:

That led me on a little trip down the rabbit hole of learning wren, the language (from what I understand) EOS will use. If you're not familiar with EOS, it's the next generation blockchain application platform some have called an operating system which Dan Larimer, the creator of Steemit and BitShares, is currently working on. I recently resteemed @trogdor's excellent post summarizing the Consensus 2017 announcement of EOS if you want to play catch up a bit.

The wren.io getting started docs are actually a lot of fun! It's super easy to get up and running quickly. I built my first website in 1996 and have programmed professionally in HTML, JavaScript, PHP, ColdFusion, Java, ASP, and other languages so I figured I could pick things up rather quickly, and I wasn't disappointed. What follows are my scratch notes saved on the blockchain so I can easily refer to them later.

If you're a developer (and even if you are not, you should consider becoming one), wren and EOS may end up being one of the most important and disruptive technologies to come around for quite some time. I want to get a head start and begin writing contracts and EOS messages as soon as the testnet comes online.

Most of the getting started docs are really easy to walk through, especially for those who have already done some programming. Some gotchas or things I found interesting are noted below.

Basics: lists and maps = arrays and associative arrays (as they are called in PHP)

When I ran one of the first examples in the docs, I was surprised it errorred out:

Null does not implement 'iterate(_)'.

Strangely, when I run it saved as a file, it works just fine:

Odd. When I got to the section on control flow I think my suspicion was confirmed that i hadn't yet been defined (but for some reason works fine when read in as a file). Strange.

The other example was pretty cool (spoiler alert):

OMG... why don't more languages support this?!?!

This causes me so much frustration in PHP.

I found this interesting:

Wren makes no promises about what order keys and values are iterated in when you use these methods.

For code like this:

var birds = {
  "Arizona": "Cactus wren",
  "Hawaii": "Nēnē",
  "Ohio": "Northern Cardinal"
}

for (state in birds.keys) {
  System.print("The state bird of " + state + " is " + birds[state])
}

Don't expect things to print in some rational order. This has tripped me up before with languages like JavaScript as well.

This means 0, empty strings, and empty collections are all considered “true” values.

Interesting. And I thought PHP was pretty loose when it comes to evaluating things. :)

Calling functions is a little confusing to me with stuff like

class Blondie { 
  callMe(fn) { 
    fn.call() 
  } 
}

but I guess this isn't much different than JavaScript. I'm not really good at functional or event driven programming, but I do need to get more comfortable with them both.

The use of | to show the function inputs is a little odd as well, but no biggie.

Closures often trip me up, but that's nothing new.

The method scope is interesting in that capitalized things will be assumed as classes outside of the method scope.

Constructors are done with construct new(a, b) while noting:

The word “new” isn’t special to Wren, it’s just a common constructor name.

(but really, why would we use any other name?)

All state stored in instances is stored in fields. Each field has a name that starts with an underscore.

Good to know. If I want what I would consider a property of a class, I need to name it with an _.

Also interesting:

One thing we’ve learned in the past forty years of software engineering is that encapsulating state tends to make code easier to maintain, so Wren defaults to keeping your object’s state pretty tightly bundled up. Don’t feel that you have to or even should define getters or setters for most of your object’s fields.

Also two underscores (__) is how we get static fields within a class.

Inheritance is done with is such as the example class Pegasus is Unicorn {} but static methods and constructors are not inherited, but you can call super.

Whoa... Fibers. Now things are getting interesting.

They are lightweight enough that you can, for example, have a separate fiber for each entity in a game. Wren can handle thousands of them without breaking a sweat. For example, when you run Wren in interactive mode, it creates a new fiber for every line of code you type in.

.isDone... interesting:

It’s a runtime error to try to call a fiber that is already done.

Ah, yielding:

The main difference between fibers and functions is that a fiber can be suspended in the middle of its operation and then resumed later.

Huge:

Note that even though this program has concurrency, it’s still deterministic.

This is always something that bothered me about multi-threaded and event driven code. Sometimes it's very difficult to figure out what the heck is going to happen each time you run it. I wonder if this is the main reason Dan chose wren for EOS?

coroutines. Whoa. I'm a little out of my element here.

Here, we’re calling yield() from within a function being passed to the map() method. This works fine in Wren because that inner yield() call will suspend the call to map() and the function passed to it as a callback.

That sounds like a lot of word salad to me right now. I'm sure I'll get it over time. I wonder if EOS contracts will make use of this at all? I'm hoping I won't have to deal with the transfer stuff, as that seems even more complex as far as keeping track of what's going on.

A file containing Wren code defines a module.

Nice.

This means, for example, that two modules can define a top-level variable with the same name without causing a name collision.

Also nice.

I see this modularity as a nice way to start building a list of reusable modules related to EOS which other developers could benefit from. This could get exciting.

I liked this explanation of the execution process for imports:

Think of it like traversing the tree of imports, one node at a time. At any given point in time, only one module’s code is running.

Another good point about importing:

a module’s code is only executed the first time it is loaded. After that, importing the module again just looks up the previously loaded module.

Similar to PHP's include_once approach but done automatically.


So those are my scratch notes. If you're a developer and you've at all looked into BitShares and Steemit, you probably have a hunch how big EOS could be. I highly recommend spending a bit of time and going through the wren docs and getting familiar with the language. Who knows, businesses, "governments," personal contracts, property agreements, and many other human interactions we haven't yet imagined may be written in the wren scripting language.

In the future, your ability to understand the code you're agreeing to may be the difference between you being scammed and you having success.

The future, it seems, favors the programmer.

Created with love using ChainBB


Luke Stokes is a father, husband, business owner, programmer, voluntaryist, and blockchain enthusiast. He wants to help create a world we all want to live in.

H2
H3
H4
3 columns
2 columns
1 column
53 Comments