Sources
My first Wren Article got me excited enough to try Wren out. If you like this, please do not forget to like the motivating article. It is also where I first saw the phrase Language of EOS
.
Wren.io was a big help, as it has the
(in)complete documentation for the language.
The single best source for information about Wren, as a language, has been its tests. They are sparse, but do a better job explaining constructors, closures, etc etc than the documentation.
WARNING
I invest in EOS, I have bias motivations :)
Coming From a C-Family Language
Unless you have been programming Fortran 67 or Whitespace, this language should feel pretty familiar. Its documentation claims similarities to Ruby, of which I have never programmed, but I have heard it is like a majestic, ruby encrusted sword...
So You Want to Close Over?
Whenever I have heard someone attempt to explain closures they tend to contain the following phase.
"...the variables you want to close over..."
It would be good to review the available closure tests before proceeding. Wren’s Closure Tests.
If you are unfamiliar with closures you can have a basic review here
Quick Breakdown
Functions | Class instance methods | Class static methods | |
---|---|---|---|
1. vars declared in outer scope | ✓ (exception: look at point 1) | ✘ | ✘ |
2. import statements | ✓ | ✓ | ✓ |
3. other classes (order of execution does not matter) | ✓ | ✓ | ✓ |
1. Variables Declared in Outer Scope
Wren supports this feature with two exceptions.
- A function cannot reference itself unless the
Fn.new {...}
andvar x =
statements exist on different lines.
The following simple function is an example where the wren file cannot compile due to the factorial
reference within the factorial closure.
System.print(“Test”)
var factorial = Fn.new {|n|
if (n == 1) {
return 1
}
return n * factorial.call(n - 1)
}
System.print(factorial.call(5))
Upon execution you will get the following error.
[main line 5] Error at 'factorial': Undefined variable.
Notice there is no output of Test
from the System.print
line.
As mentioned above, to fix this move the var
declaration to its own line.
System.print(“Test”)
var factorial = null
factorial = Fn.new {|n|
if (n == 1) {
return 1
}
return n * factorial.call(n - 1)
}
System.print(factorial.call(5))
The output this time is
Test
120
Great Success!
- Class methods cannot reference outer scope vars.
The following code shows that referencing outside variables will fail whether in static
or instance
method.
var testing = 5
class Test {
construct new() {}
echo() {
System.print(testing)
}
static echo() {
System.print(testing)
}
}
Test.echo();
Test.new().echo();
2. Import statements
They can be referenced from method or closure
3. Other classes
Classes and functions can reference other classes regardless of declaration order.
Here is a small script outlining the different, out-of-order references to classes that seem to work. As a side note, the output of Random
surprised me, so I thought I would keep it in there. A little exercise for the reader.
import "random" for Random
var fn = Fn.new {
System.print("From fn")
OtherClass.echo()
}
class OtherClass {
static echo() {
System.print("From OtherClass")
EchoClosureValues.new().echo()
}
}
class EchoClosureValues {
construct new() {}
workingEcho() {
System.print("Working Echo")
}
echo() {
System.print("instance echo")
System.print(Random.new().int(10))
}
static echo() {
System.print("static echo")
OtherClass.echo()
}
}
// Toggle with comments
fn.call()
EchoClosureValues.echo()
EchoClosureValues.new().echo()
The output is
From fn
From OtherClass
instance echo
6
static echo
From OtherClass
instance echo
6
instance echo
6
In The End
Wren is a neet little language with few features. Unfortunately this does worry me for writing enterprise level software. As of the time of writing this article, I cannot find a Math
class. To do a binary search, my floor operation looked like ~~
. If you are confused by that or do not know the potential pitfalls of such an integer bitwise operation, then go forth and read! I hope EOS is not stunted by the language of choice.
Congratulations @michaelp983! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published your First Post
You made your First Vote
You got a First Vote
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @michaelp983! You received a personal award!
Click here to view your Board of Honor
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @michaelp983! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit