Skip to content

Can't distinguish between nil variables (that don't exist) and default values  #58

@jeremyherbert

Description

@jeremyherbert

It seems unclear to me as to how to deal with variables that do not exist in the lua state. This combined with issue #41 means that lots of things can go bad without the user being informed. I think the correct behaviour here would be an exception, as int cannot be NULL. However it has been noted in #10 that exceptions are not going to be used so I am not sure how to proceed.

$ lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print(y)
nil
>

#include "Selene/include/selene.h"
#include <string>
#include <iostream>

int main(void) {
    sel::State state;
    state["x"] = 0;
    state["y"] = std::string("");

    int x = state["x"];
    int x2 = state["x2"]; // this doesn't exist

    std::string y = state["y"];
    std::string y2 = state["y2"]; // this doesn't exist

    std::cout << (x == x2 ? "same" : "different") << std::endl;
    std::cout << (y == y2 ? "same" : "different") << std::endl;
    return 0;
}
$ g++-4.9 -std=c++11 -g -I/usr/include/lua5.2 test.cpp -llua5.2 -o test
$ ./test
same
same
$

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions