Giter Site home page Giter Site logo

Return values from loops about rhai HOT 4 CLOSED

rhaiscript avatar rhaiscript commented on May 19, 2024
Return values from loops

from rhai.

Comments (4)

luciusmagn avatar luciusmagn commented on May 19, 2024

from rhai.

rustysec avatar rustysec commented on May 19, 2024

so this works, but without introducing a () type into the script engine we can end up in some weird states. a loop {} may not always return something, there's no enforcement of consistency at this point. for instance a while loop like this:

let i = 0;
while i < 100 {
    if i == 50 { break true; }
    i+=<user input>;
}

depends on the user input to increment. if it never hits 50, the loop returns with () at the end which the engine doesn't really know how to gracefully handle. something like this is a simpler example:

> let x = while true == false { break; }
> print(x)
error: Function argument types do not match

while this works as expected:

> let x = while true == true { break 7 }
> print(x)
7

If anyone has ideas other than me implementing a () type, i'd love to hear them!

from rhai.

schungx avatar schungx commented on May 19, 2024

I'm wondering whether this is really necessary, as it is just as easy to write:

let x;

loop {
    // complicated computations
    x = 7;
    break;
};

This way, statements such as while, loop, for etc. do not need to be turned into valid expressions, avoiding some weird-looking scripts like let x = while true == true { break 7 };.

For Rust, it is actually useful because you need not make x mutable if you create it directly from the loop. However, Rhai variables are mutable by default and opt-out by const.

from rhai.

schungx avatar schungx commented on May 19, 2024

I'm closing this one unless that are new opinions.

from rhai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.