My Notes on Wren, the Language of EOS

in #eos7 years ago

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.

Sort:  

Looks like Wren may not be used after all. Here's the latest from @dantheman: https://steemit.com/eos/@dantheman/web-assembly-on-eos-50-000-transfers-per-second Follow him and @eos for up-to-date information.

Thanks for the post i've been trying to do as much research as possible on the EOS ICO as well and your post was well written! Nice to meet you I'm TheHulk!

Hello Hulk, nice to meet you as well! Yeah, everyone keeps asking in the Telegram about the ICO. The answer is always the same:

Sign up at http://eos.io/ to get on the mailing list

We're all going to find out together, it seems.

Wren is going to be a pretty important part of EOS adoption. A simple scripting language like Wren is going to let many more people write code for the blockchain. Unlike complex (or different, however you want to word it) languages like Ocaml - to be used by the upcoming project Tezos - Wren is very simple and intuitive. While Ocaml, a functional language, may provide many guarantees of contract code safety, it is a paradigm shift for many programmers.

With the space moving toward a polychain future, there is a place for both projects. There will be a spectrum of applications with a spectrum of security requirements - these will dictate what platform the app will run on. Wren positions EOS in the mass market, easy to use part of that spectrum. There will be users who want the security and formal verification of functional languages.

Will have to give that demo a go, the Mandelbrot looks pretty.

I remember hearing Dan talk about the difference between his approach and the approach many others take. Instead of trying to make code "perfect" or building in error checking into every single step of the process, he prefers error detection and recovery. To many pure programmers of the academic type, this sounds like sacrilege!

To me, it sounds great. I've always been a pragmatic programmer and I appreciate that code is never perfect. Code can be great, useful, even excellent, but it can't be perfect. Perfection is the enemy of good enough. The approach, as I understand it, is to have validation in the beginning and then, once it's been validated and added to the blockchain, ignored. Since it's already validated data, everyone else just needs to replay it, not validate it again. That makes a lot of sense to me. I also like how things can be shut down (like individual apps) without disrupting the entire blockchain.

Tezos sounds interesting, but I kind of feel like it's trying for perfection when the real world is quite messy. Businesses involve humans and humans are messy. I'm signed up to the mailing list, but I don't know much more about it at this point.

I think the mentality you have over code is very healthy. I think there is a place in this space for both types of blockchain. DPOS already has some trade offs when compared to other consensus methods. I think there is a place for functional programming and the benefits it brings to the table. It will be interesting to see how the space evolves as more applications arive.

Interesting, thanks for the notes. I also took a look at wren and I am glad you started the #wren hashtag.

I am not a developer, so I first learned Python as I was told was the easiest... I will try to tackle wren now as they are somewhat similar.. one thing I didnt like from my noob experience was that you need to put: "System.print" with capital S, not sure why, but I hate getting errors for case sensitive commands

Anywayz, expecting to read a lot more from you on wren!

Hopefully I'll find something interesting to do with wren once the testnet launches, and I attempt to wrap my head around what EOS actually is.

I hate getting errors for case sensitive commands

I did notice (but didn't add to my notes) that wren is case sensitive. One thing most non-programmers don't fully appreciate is how important exactness is when writing good code. If we get sloppy, things break. From that perspective, I don't mind seeing an error telling me I'm starting to get lazy with my exactness. :)

Good luck with it. Let me know if you have any questions and I'll try to help as best I can (though I'm a noob here too).

Awesome post Luke. Thanks for taking the time to put this together. I am looking forward to learning more about EOS. Resteem.

Thanks @dwinblood! I always appreciate a resteem. :)

Excellent post. Easy to follow up even for noobs like me.

Thanks @kyriacos! This was mostly notes for me I'll refer to later, but I'm quite happy it has been helpful to others as well.

Ooh.. I had no idea about this stuff. This looks fun! I have some catching up to do and some scripting to experiment with!

Excellent! EOS could be a really big deal.

I did a quick post on messing with Wren. It looks fun and doesn't seem like too much of a learning curve. 😁

EOS is going to be a different story. Still don't completely have my head around that one.

great write up. Didn't know you were a coder too. I think it'll be huge. You ever read my PAL posts about forming a block chain government. The day will come.

I built most of FoxyCart over the last ten years. I've written a bit of PHP to play around with Steemit as well, including the code to generate my weekly exchange transfer report.

Block chain government sounds good to me. That's why I put "government" in quotes. :)

get rid of the quotes homie. It won't look like a 20th century or earlier government. It will be faster, fairer, better, and cooler. In the future you'll put quotes around calling these pieces of shit government service corporations masquerading as de jure governments in quotes, and talk about actual models of fair governance that have come about post crypto revolution.

Heheh, sounds good to me. "Government service corporations" is an interesting term. I wonder how long it will be before we start shaming companies which take contracts from governments who back them with violent force.

Luke. I'm saying what you think of as a government isn't a government. Canada. USA. Russia. Those aren't governments. They are corporations. They are listed on the SEC. Dunn and Bradstreet have business IDS for them. They are literally companies. When you elect a president you're electing a CEO. You're not electing a de jure president. It's weird, but it goes back to the civil war. There wasn't a quorum when the south left so the government couldn't operate. So, they formed a corporation and made it sound almost exactly the same, but legally it changed from being a Constitutional Republic to a government services corporation that "represents" the Republic. You won't find that in history books because it's been purposefully hidden. And it's like that for a million things. Your strawman (you are a corporation), the police, universities, commissions, just about everything is actually a corporation pretending to be something different.

I've used way too many hours going down the sovereign citizen route to spend any more time on it right now, but know that we both ultimately want the same thing. Regardless of the terms used, the current "government" is clearly not looking out for the best interests of its citizens (or, some might say, its product). One of my favorite ways of thinking about this comes from the concept of human farming and the story of your enslavement.

Peace.

Now that my friend is the proper use of quotes around government and I feel very much aligned with your work.

I love your work! I'm embarrassed to say I just started following today. I see your posts in my feed so much I thought I was already following! Corrected that tonight. Namaste bud and looking forward to the next one!

It would be really cool to start the #wren topic as a sub-forum on chainbb

Not a bad idea at all. @jesta, what say you?

Exactly, so that we could share dev tip on wren. There are so many projects from Ethereum that EOS could powered in the next 6-12 month, since ethereum still need to go though a change in the consensus mechanism (from PoW to PoS) that will not be fully complete by Q1-2018. All +200 use cases being build on Ethereum will then have a choice to switch to a more faster, scalable, free Blockchain like EOS. What a timing? To be watch!

great job with this wren overview! just one thing that stood out...

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

The reason the ordering seems random and strange is that you are using a HashMap versus an Array or List.

Array:
[0] -> {"Arizona": "Cactus wren"}
[1] -> {"Hawaii": "Nēnē"}
[2] -> {"Ohio": "Northern Cardinal"}

HashMap:
keyHash(Hawaii) -> [0] -> {"Hawaii": "Nēnē"}
keyHash(Ohio) -> [1] -> {"Ohio": "Northern Cardinal"}
keyHash(Arizona) -> [2] -> {"Arizona": "Cactus wren"}
(array hash position may also change depending on how many buckets are available to the hash table)

It's pretty much the same across all languages. For example, here's an explanation of the differences and syntax in Java: "Difference between ArrayList and HashMap in Java"

and in wren, you've also got lists and maps.

Another great post Luke! I just have a feeling that EOS is going to be another revolutionary platform. And guess what group of crypto nerds will be on it like a Rat On A Cheeto ? YUP...... The Steemians! MUuhahahahahahah

Another interesting post. Thank you!

Nice. I was thinking about digging into Wren as my first language because of its future usefulness with EOS.

From what I've seen so far, that may not be a bad idea at all. It seems like a pretty simple language to understand and start using right away. If you have any questions, let me know if I can help.

Awesome! Thanks!

Can someone explain to me why a new scripting language is needed for EOS? Why not using Python or Ruby?

Because those other languages are slower and more difficult to sandbox and embed.

Thanks for chiming in, Dan. :)

Hy @dan, thank you for your reply.

Okay so suppose I want to dive in, (probably not, but someone I know may want to) how do I start? I went to the eos website, and it currently only offers to join the mailing list. So is there some loony adopters kit? Somewhere I can get an eos iso from and and README files? Git maybe?

I dropped some hints in my notes above. The more I learn about the language, the more it seems perfect for the job.

Can you just recap that for me? What is it? Concurrency, maybe?

I'm just trying to figure out weather I should dwell into learning yet another programming language.

If you want to write code for EOS, yes.

nice.....resteem and upvote for your post....

Woah! Definitely interesting choice of language. @dantheman surely feels like he's ahead of the time. Lots of great points you summarized here, Luke. I took a quick look as well, and it's definitely a complex language. I really like the nested comments, and I seem to be too caught up in that haha! I get what you mean about commenting out blocks that already have comments. That's a huge underrated advantage to have.

Thanks for posting- I'm not a developer but is sounds as if it would be wise ro get up to speed on this- wish me luck.

Do it! I think a time will come when everyone will benefit from learning how to read and understand code (if not write some themselves).

hey lukestokes... sorry for commenting on one of your old posts... I am willing to learn working on EOS.. maybe creating frontend.. where should I start from ?? ... what all language I need to be fluent in ... should I work with steem.js and the community before thinking about EOS?

Hello @tanishqyeverma. Those are all great questions that no one can really answer but yourself. My suggestion would be to join the EOS communities on Telegram (there are multiple ones) and Discord and start asking how you can provide the most value and then start learning the skills needed to do that.