'upvalue를 색인화하려는 시도'의 의미는 무엇입니까?
문제
LUA에서 첫 단계 프로그래밍을 수행하고 스크립트를 실행할 때이 오류가 발생합니다.
attempt to index upvalue 'base' (a function value)
아마도 내가 아직 파악하지 못한 것은 매우 기본적인 것이 있기 때문일 것입니다. 그러나 인터넷 검색을 할 때 좋은 정보를 찾을 수 없습니다. 누군가 나에게 그것이 무엇을 의미하는지 설명 할 수 있습니까?
해결책
이 경우 보입니다 base
함수이지만 테이블처럼 색인하려고합니다 (예 :. base[5]
또는 base.somefield
).
'upvalue'부분은 어떤 종류의 변수를 알려줍니다. base
이 경우 upValue (일명 외부 로컬 변수)입니다.
다른 팁
하나의 "지역"이 너무 많습니까?
처럼 마이크 f 설명, "upvalue"는 외부 로컬 변수입니다. 이 오류는 종종 변수가 선언되었을 때 발생합니다. local
선언문에서 선언했다 local
다시 초기화 될 때. 이것은 전방 선언 변수를 다음의 값으로 남겨 둡니다. nil
. 이 코드 스 니펫은 무엇을 보여줍니다 ~ 아니다 할 것:
local foo -- a forward declaration
local function useFoo()
print( foo.bar ) -- foo is an upvalue and this will produce the error in question
-- not only is foo.bar == nil at this point, but so is foo
end
local function f()
-- one LOCAL too many coming up...
local foo = {} -- this is a **new** foo with function scope
foo.bar = "Hi!"
-- the local foo has been initialized to a table
-- the upvalue (external local variable) foo declared above is not
-- initialized
useFoo()
end
f()
이 경우 제거 local
앞에 foo
초기화 될 때 f()
예제를 수정합니다
foo = {}
foo.bar = "Hi!"
이제 usefoo ()로 전화하면 원하는 출력이 생성됩니다.
안녕!
제휴하지 않습니다 StackOverflow