Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

cannot load() returned values from string

$
0
0

I have this assignment from the official book about lua:Exercise 16.1: Frequently, it is useful to add some prefix to a chunk of code when loading it. (We saw an example previously in this chapter, where we prefixed a return to an expression being loaded.) Write a function loadwithprefix that works like load, except that it adds its extra first argument (a string) as a prefix to the chunk being loaded.Like the original load, loadwithprefix should accept chunks represented both as strings and as reader functions. Even in the case that the original chunk is a string, loadwithprefix should not actually concatenate the prefix with the chunk. Instead, it should call load with a proper reader function that first returns the prefix and then returns the original chunk.

function loadwithprefix(prefix, chunk)local prefixflg = truereturn function(...)    local x = select(1, ...)    local env = { x = x, __index = { _G = _G } }    if not prfxflg then        prfxflg = true        return load(prefix, nil, nil, env)    else        return load(' return ' .. code(), nil, nil, env)    endendendlocal line = io .read()local f = loadwithprefix('local x = ...; return x ', function(...) return line end)for i = 1, 10 do   print(string.rep('*', f(i)()))end

I get : bad argument #2 to 'rep' (number expected, got nil) meaning i cannot evaluate the local x = ...; return x to actually return 1 from the for-loop statement. Any suggestion?


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>