luna-lib 1.3.0

a light weight interpreted programming language
Documentation
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../../img/favicon.ico">
    <title>Objects &mdash; Luna Docs</title>
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:400,700">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/tonsky/FiraCode@1.206/distr/fira_code.css">
    <link rel="stylesheet" href="//use.fontawesome.com/releases/v5.8.1/css/all.css">
    <link rel="stylesheet" href="//use.fontawesome.com/releases/v5.8.1/css/v4-shims.css">
    <link rel="stylesheet" href="../../css/theme.css">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
    <script>
        hljs.initHighlightingOnLoad();
    </script> 
</head>

<body ontouchstart="">
    <div id="container">
        <aside>
            <div class="home">
                <div class="title">
                    <button class="hamburger"></button>
                    <a href="../.." class="site-name"> Luna Docs</a>
                </div>
                <div class="search">
                    <div role="search">
    <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
        <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
    </form>
</div>
                </div>
            </div>
            <nav class="nav">
                <ul class="root">
                    <li class="toctree-l1"><a class="nav-item" href="../..">Home</a><a class="nav-item" href="../..">Home</a></li>
                    <li class="toctree-l1"><button class="section nav-item">Getting Started</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../../getting_started/installation/">Installation</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../getting_started/environment/">Environment</a></li>
</ul></li>
                    <li class="toctree-l1"><button class="section nav-item">Guide</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../grammar/">Grammar</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../statements/">Statements</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../expressions/">Expressions</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../basic-values/">Basic Values</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../variables/">Variables</a></li>
    <li class="toctree-l2 current"><a class="nav-item current" href="./">Objects</a>
<ul class="subnav">
<li class="toctree-l3"><a class="nav-item toc" href="#meta-objects">Meta Objects</a></li>
<li class="toctree-l3"><a class="nav-item toc" href="#meta-fields">Meta Fields</a></li>
<li class="toctree-l3"><a class="nav-item toc" href="#user-objects">User-Objects</a></li>
</ul></li>
    <li class="toctree-l2"><a class="nav-item" href="../functions/">Functions</a></li>
</ul></li>
                    <li class="toctree-l1"><button class="section nav-item">Standard Modules</button>
<ul class="subnav">
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/globals/">Globals</a></li>
    <li class="toctree-l2"><button class="section nav-item hide">Type Modules</button>
<ul class="subnav hide">
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/int_module/">Int Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/float_module/">Float Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/bool_module/">Bool Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/char_module/">Char Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/string_module/">String Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/vector_module/">Vector Module</a></li>
    <li class="toctree-l3"><a class="nav-item" href="../../standard_modules/type_modules/object_module/">Object Module</a></li>
</ul></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/math/">Math</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/io/">IO</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/fs/">FS</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/env/">Env</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/net/">Net</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/os/">OS</a></li>
    <li class="toctree-l2"><a class="nav-item" href="../../standard_modules/typed/">Typed</a></li>
</ul></li>
                </ul>
            </nav>
            <div class="repo">
    <div class="previous"><a href="../variables/">&laquo; Previous</a></div>
    <div class="next"><a href="../functions/">Next &raquo;</a></div>
</div>
        </aside>
        <div id="spacer"><button class="arrow"></button></div>
        <main>
            <div class="home-top">
                <button class="hamburger"></button>
                <a href="../.." class="site-name"> Luna Docs</a>
            </div>
            <div id="main">
                <nav class="breadcrumbs">
<ul>
    <li>Guide</li>
</ul>
</nav>
                <div id="content"><h1 id="objects">Objects</h1>
<p>Objects are an important part of Luna. They are a map of string-value pairs, that can be read and assigned freely. An object value is only a reference to the actual object, which means passing it around means passing around the reference. Any function that has a reference to an object can modify it's contents.</p>
<h2 id="meta-objects">Meta Objects</h2>
<p>Every object can have an optional reference to another object, being it's meta object. Those objects are just like normal objects, except they can define custom behaviour for the object that references it as an meta object.</p>
<pre><code>let person = { name = &quot;joe&quot;, age = 25 }
setmeta(person, {})
</code></pre>
<p>Now the object referenced by <code>person</code> got an empty meta object. This meta object does not have any fields that could influence the <code>person</code> object.</p>
<h2 id="meta-fields">Meta Fields</h2>
<p>There are a few meta fields predefined in Luna.</p>
<table>
<thead>
<tr>
<th>field</th>
<th>effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>__name: string</code></td>
<td>when the object is casted to a string, this string will be displayed instead of the default <code>object</code></td>
</tr>
<tr>
<td><code>__type: string</code></td>
<td>when the object's type is trying to be determined, this string will be returned</td>
</tr>
<tr>
<td><code>__str: fn(self: object): string</code></td>
<td>when the object is trying to be casted to a string, this function will be called with the object as the first argument</td>
</tr>
<tr>
<td><code>__call: fn(self: object, ...)</code></td>
<td>when the object is trying to be called, this function will be called</td>
</tr>
<tr>
<td><code>__get: fn(self: object, key: string)</code></td>
<td>when trying to get a field of the object, this function will be called</td>
</tr>
<tr>
<td><code>__set: fn(self: object, key: string, value)</code></td>
<td>when trying to set a field of the object, this function will be called</td>
</tr>
<tr>
<td><code>__next: fn(self: object)</code></td>
<td>when trying get the next iteration in a for-loop, this function will be called and it's return value will be assign to the for-loop's variable</td>
</tr>
</tbody>
</table>
<h2 id="user-objects">User-Objects</h2>
<p>User-objects are externally defined structures in Luna's mother language, Rust. They allow for interoperational functionality between Luna and Rust. This is usefull for file handling for example.</p>
<pre><code>let file = fs.open(&quot;test.txt&quot;, &quot;r&quot;)
if some(file) {
    print(file)
    print(file:read())
}
</code></pre>
<p>output:</p>
<pre><code>file:0x631349d52af8 
test test!
</code></pre>
<p>This code opens a file with the name <code>file.txt</code> in <strong>r</strong>ead mode and saves the <code>file</code> user-object in the local <code>file</code>. if the file was found, then the program first prints the file object and then the content of it with the <code>read</code> function.</p></div>
                <footer>
    <div class="footer-buttons">
        <div class="previous"><a href="../variables/" title="Variables"><span>Previous</span></a></div>
        <div class="next"><a href="../functions/" title="Functions"><span>Next</span></a></div>
    </div>
    <div class="footer-note">
        <p>
            Built with <a href="http://www.mkdocs.org">MkDocs</a> using
            <a href="https://github.com/daizutabi/mkdocs-ivory">Ivory theme</a>.
        </p>
    </div>
</footer>
            </div>
        </main>
    </div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js"></script>
    <script src="../../search/main.js"></script>
</body>

</html>