Links

Categories

Tags


« | Main | »

Declaring local variables with ‘auto’

By Jewe | March 19, 2016

JewelScript 1.4 adopts the auto keyword known from C++11.

The auto-keyword in newer C++ versions is convenient, especially since class names in C++ tend to become excessively long.

Personally I have mixed feelings about this short-hand. When the code of your function reaches a certain complexity, you tend to lose track of what’s in what variable. A problem especially the dynamically typed languages suffer from (JavaScript!), and exactly the reason why I wanted JewelScript to be a statically typed language.

However, adding this feature was so freaking easy, there just wasn’t any good reason not to. Since JewelScript’s expression parsing supported type-deduction from the very start, I just added the code that writes the resulting type information back to the result variable.

Example code:

import stdlib;

function main()
{
    auto i = 10;
    auto f = 12.34;
    auto s = "hello world";
    for (auto n = 0; n < i; n++)
    {
        stdlib::println(s+" "+n+" "+f);
    }

    auto o = new Very::Long::Namespace::MyClass("hello!");
    o.Print();
}

namespace Very::Long::Namespace
{
    class MyClass
    {
        string mStr;
        method MyClass(string s)
        {
            mStr = s;
        }
        method Print()
        {
            stdlib::println(mStr);
        }
    }
}

Topics: code examples, docs, news | Comments Off on Declaring local variables with ‘auto’

Comments are closed.