Identifying absence of return


#1

I would like to recognize when a ChaiScript function doesn’t call return, back in C++. I.e., the following situation:

chaiscript::Boxed_Value bv = chai.eval("def g() { } g()");
// test bv to recognize that this happened

For example, if the function actually called return, without passing anything to it, I could identify it via:

chaiscript::Boxed_Value bv = chai.eval("def f() { return } f()");
// bv.get_type_info().is_undef() is now true

Is there something equivalent in the first case?


#2

In ChaiScript the return value of a function is the result of the last statement executed.

Example:

def f() { 5; } f() // returns 5
def f2() { return 5; } f2() // returns 5

Those two statements are exactly the same in ChaiScript

So, similarly

def g() { } g() // implicitly returns 'void'
def g2() { return; } g2() // explicitly returns 'void'

There is no way to tell the difference between these statements.

I would recommend that at the end of your function you consider returning a sentry value of some kind, something like:

def func() {
  if (x) {
    return;
  }

  false;
}

Then you can check something like vc.get_type_info().is_void() then you know it came from an explicit return, otherwise it dropped through to the bool return value.


#3

I’d be perfectly happy if g() and g2() in your example were indistinguishable. The trouble is that I can recognize g2 by checking is_undef(), but not g. If I check is_void(), it returns false in both cases.


#4

So it seems that I have normalized the behavior in my current development branch here: https://github.com/ChaiScript/ChaiScript/tree/cleanups_and_reworkds

Sorry about the confusion.

So the question is what to do from here? These changes are a bit too drastic to merge into the 5.x release branch.

  • You can join me on that in-progress branch. It has very many performance improvements and cleanups like this, but also requires a C++14 compiler, and it is in a state of flux
  • You can test for a 3-way kind of test
    • if is_undef() || is_void() || bare_equal_type_info(typeid(bool)) (the default return value of a block is true in the version of ChaiScript you are using)

-Jason


#5

Ah, brilliant. Unfortunately, C++14 is not an option for me. So I’ll go with your final option (testing against bool). Thanks a lot for your help!