mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
Deploy to GitHub Pages:
This commit is contained in:
105
classes.html
105
classes.html
@ -652,6 +652,111 @@ class Derived is Base {
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Attributes <a href="#attributes" name="attributes" class="header-anchor">#</a></h2>
|
||||
<p><small><strong>experimental stage</strong>: subject to minor changes</small></p>
|
||||
<p>A class and methods within a class can be tagged with ‘meta attributes’.</p>
|
||||
<p>Like this:</p>
|
||||
<pre class="snippet">
|
||||
#hidden = true
|
||||
class Example {}
|
||||
</pre>
|
||||
|
||||
<p>These attributes are metadata, they give you a way to annotate and store
|
||||
any additional information about a class, which you can optionally access at runtime.
|
||||
This information can also be used by external tools, to provide additional
|
||||
hints and information from code to the tool.</p>
|
||||
<p><small>
|
||||
Since this feature has just been introduced, <strong>take note</strong>.</p>
|
||||
<p><strong>Currently</strong> there are no attributes with a built-in meaning.
|
||||
Attributes are user-defined metadata. This may not remain
|
||||
true as some may become well defined through convention or potentially
|
||||
through use by Wren itself.
|
||||
</small></p>
|
||||
<p>Attributes are placed before a class or method definition,
|
||||
and use the <code>#</code> hash/pound symbol. </p>
|
||||
<p>They can be </p>
|
||||
<ul>
|
||||
<li>a <code>#key</code> on it’s own</li>
|
||||
<li>a <code>#key = value</code></li>
|
||||
<li>a <code>#group(with, multiple = true, keys = "value")</code></li>
|
||||
</ul>
|
||||
<p>An attribute <em>key</em> can only be a <code>Name</code>. This is the same type of name
|
||||
as a method name, a class name or variable name, an identifier that matches
|
||||
the Wren identifier rules. A name results in a String value at runtime.</p>
|
||||
<p>An attribute <em>value</em> can be any of these literal values: <code>Name, String, Bool, Num</code>.
|
||||
Values cannot contain expressions, just a value, there is no compile time
|
||||
evaluation.</p>
|
||||
<p>Groups can span multiple lines, methods have their own attributes, and duplicate
|
||||
keys are valid.</p>
|
||||
<pre class="snippet">
|
||||
#key
|
||||
#key = value
|
||||
#group(
|
||||
multiple,
|
||||
lines = true,
|
||||
lines = 0
|
||||
)
|
||||
class Example {
|
||||
#test(skip = true, iterations = 32)
|
||||
doStuff() {}
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>Accessing attributes at runtime <a href="#accessing-attributes-at-runtime" name="accessing-attributes-at-runtime" class="header-anchor">#</a></h3>
|
||||
<p>By default, attributes are compiled out and ignored.</p>
|
||||
<p>For an attribute to be visible at runtime, mark it for runtime
|
||||
access using an exclamation:</p>
|
||||
<pre class="snippet">
|
||||
#doc = "not runtime data"
|
||||
#!runtimeAccess = true
|
||||
#!maxIterations = 16
|
||||
</pre>
|
||||
|
||||
<p>Attributes at runtime are stored on the class. You can access them via
|
||||
<code>YourClass.attributes</code>. The <code>attributes</code> field on a class will
|
||||
be null if a class has no attributes or if it’s attributes aren’t marked.</p>
|
||||
<p>If the class contains class or method attributes, it will be an object with
|
||||
two getters:</p>
|
||||
<ul>
|
||||
<li><code>YourClass.attributes.self</code> for the class attributes</li>
|
||||
<li><code>YourClass.attributes.methods</code> for the method attributes</li>
|
||||
</ul>
|
||||
<p>Attributes are stored by group in a regular Wren Map.
|
||||
Keys that are not grouped, use <code>null</code> as the group key.</p>
|
||||
<p>Values are stored in a list, since duplicate keys are allowed, multiple
|
||||
values need to be stored. They’re stored in order of definition.</p>
|
||||
<p>Method attributes are stored in a map by method signature, and each method
|
||||
has it’s own attributes that match the above structure. The method signature
|
||||
is prefixed by <code>static</code> or <code>foreign static</code> as needed.</p>
|
||||
<p>Let’s see what that looks like:</p>
|
||||
<pre class="snippet">
|
||||
// Example.attributes.self =
|
||||
// {
|
||||
// null: { "key":[null] },
|
||||
// group: { "key":[value, 32, false] }
|
||||
// }
|
||||
|
||||
#!key
|
||||
#ignored //compiled out
|
||||
#!group(key=value, key=32, key=false)
|
||||
class Example {
|
||||
#!getter
|
||||
getter {}
|
||||
|
||||
// { regular(_,_): { regular:[null] } }
|
||||
#!regular
|
||||
regular(arg0, arg1) {}
|
||||
|
||||
// { static other(): { isStatic:[true] } }
|
||||
#!isStatic = true
|
||||
static other()
|
||||
|
||||
// { foreign static example(): { isForeignStatic:[32] } }
|
||||
#!isForeignStatic=32
|
||||
foreign static example()
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p><br><hr>
|
||||
<a class="right" href="concurrency.html">Concurrency →</a>
|
||||
<a href="functions.html">← Functions</a></p>
|
||||
|
||||
15
values.html
15
values.html
@ -162,11 +162,21 @@ again"
|
||||
"\%" // A percent sign.
|
||||
"\a" // Alarm beep. (Who uses this?)
|
||||
"\b" // Backspace.
|
||||
"\e" // ESC character.
|
||||
"\f" // Formfeed.
|
||||
"\n" // Newline.
|
||||
"\r" // Carriage return.
|
||||
"\t" // Tab.
|
||||
"\v" // Vertical tab.
|
||||
|
||||
"\x48" // Unencoded byte (2 hex digits)
|
||||
"\u0041" // Unicode code point (4 hex digits)
|
||||
"\U0001F64A" // Unicode code point (8 hex digits)
|
||||
</pre>
|
||||
|
||||
<p>A <code>\x</code> followed by two hex digits specifies a single unencoded byte:</p>
|
||||
<pre class="snippet">
|
||||
System.print("\x48\x69\x2e") //> Hi.
|
||||
</pre>
|
||||
|
||||
<p>A <code>\u</code> followed by four hex digits can be used to specify a Unicode code point:</p>
|
||||
@ -180,11 +190,6 @@ of the basic multilingual plane, like all-important emoji:</p>
|
||||
System.print("\U0001F64A\U0001F680") //> 🙊🚀
|
||||
</pre>
|
||||
|
||||
<p>A <code>\x</code> followed by two hex digits specifies a single unencoded byte:</p>
|
||||
<pre class="snippet">
|
||||
System.print("\x48\x69\x2e") //> Hi.
|
||||
</pre>
|
||||
|
||||
<p>Strings are instances of class <a href="modules/core/string.html">String</a>.</p>
|
||||
<h3>Interpolation <a href="#interpolation" name="interpolation" class="header-anchor">#</a></h3>
|
||||
<p>String literals also allow <em>interpolation</em>. If you have a percent sign (<code>%</code>)
|
||||
|
||||
Reference in New Issue
Block a user