lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto wrote:
> The problem is that there is a difference between a "function that does
> not change the struct lua_State" and a "function that does not affect the
> state seen through the API". The `struct lua_State' being const in the
eyes
> of the C compiler is too implementation dependent, and so it is not a good
> idea to export such details to the API.
>
> For instance, in the current implementation, the `lua_insert' function
> (that changes elements in the stack) could be declared const, while
> lua_getn (that only returns the size of an array) could not. (The first
one
> changes only the stack, but not its size, so lua_State is constant. The
> second may need to create a string, an therefore can potentially resize
> some internal structures, changing lua_State).

You are right, the C compiler checking is not useful in this case.

I still think it's useful to clarify which functions change the Lua state
(not meaning the literal structure or internals but the state observable by
the user) at the API level.  That is why the "mutable" qualifier exists in
C++... to let you fiddle with implementation-dependent internals while still
giving an appearance of const to the user.  With a C API like Lua this
illusion can be done by declaring function arguments as const where
appropriate and then casting to non const in the implementation.

Thanks for your help,
-John