Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Io (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == {{Unreferenced section|date=June 2013}} In its simplest form, Io [[Syntax (programming languages)|syntax]] is composed of one identifier:<ref>{{Cite web |title=io guide |url=https://iolanguage.org/guide/guide.html#Syntax |access-date=2023-06-22 |website=iolanguage.org}}</ref> <syntaxhighlight lang="io"> doStuff </syntaxhighlight> Assuming the above doStuff is a [[Method (computer programming)|method]], it is being called with zero arguments and as a result, explicit parentheses are not required. If doStuff had arguments, it would look like this: <syntaxhighlight lang="io"> doStuff(42) </syntaxhighlight> Io is a [[message passing]] language, and since everything in Io is a message (excluding [[Comment (computer programming)|comment]]s), each message is sent to a receiver. The above example demonstrates this well, but not fully. To describe this point better, let's look at the next example: <syntaxhighlight lang="io"> System version </syntaxhighlight> The above example demonstrates message passing in Io; the "version" message is sent to the "System" object. [[Operator (computer programming)|Operators]] are a special case where the syntax is not as cut-and-dried as the above examples. The Io [[Syntax analysis|parser]] intercepts a set of operators defined by the interpreter, and translates them to method calls. For example, the following: <syntaxhighlight lang="io"> 1 + 5 * 8 + 1 </syntaxhighlight> translates to: <syntaxhighlight lang="io"> 1 +(5 *(8)) +(1) </syntaxhighlight> All operators in Io are methods; the fact that they do not require explicit parentheses is a convenience. As you can see, there is also a little bit of [[operator precedence]] happening here, and the precedence levels are the same as with the [[Operator precedence in C|C precedence levels]]. === Methods and blocks === In Io there are two ways of creating [[anonymous function]]s: methods and blocks. Between them, they are almost identical except for [[Scope (programming)|scope]]. While blocks have [[lexical scope]], methods have [[dynamic scope]]. Both ''method'' and ''block'' are [[higher-order function]]s. === Examples === The ubiquitous [[Hello world program]]: <syntaxhighlight lang="io"> "Hello, world!" println </syntaxhighlight> New objects are created by [[Cloning (programming)|cloning]] objects. In Io specifically, a new, empty object is created and only the differences between it and its parent are stored within the new object; this behavior is known as [[differential inheritance]]. An example of this behavior is shown: <syntaxhighlight lang="io"> A := Object clone // creates a new, empty object named "A" </syntaxhighlight> A simple non-recursive factorial function, in Io: <syntaxhighlight lang="io"> factorial := method(n, if(n == 0, return 1) res := 1 Range 1 to(n) foreach(i, res = res * i) ) </syntaxhighlight> Because assignment of <code>res * i</code> to <code>res</code> is the last action taken, the function implicitly returns the result and so an explicit return expression is not needed. The above demonstrates the usage of [[Range (computer programming)|ranges]], and doesn't use a <code>for()</code> loop, which would be faster.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)