[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to store multi return value provisionally?
- From: starwing <weasley.wx@...>
- Date: Mon, 20 Dec 2010 22:03:01 +0800
2010/12/20 Peter Cawley <lua@corsix.org>:
> On Mon, Dec 20, 2010 at 1:43 PM, starwing <weasley.wx@gmail.com> wrote:
>> 2010/12/20 Axel Kittenberger <axkibe@gmail.com>:
>>> Just put em into a table:
>>>
>>>> function r5() return 1, 2, 3, 4, 5 end
>>>> re = {r5()}
>>>> for k, v in ipairs(re) do print(k, v) end
>>> 1 1
>>> 2 2
>>> 3 3
>>> 4 4
>>> 5 5
>>>
>>>
>>> On Mon, Dec 20, 2010 at 2:23 PM, starwing <weasley.wx@gmail.com> wrote:
>>>> 2010/12/20 Michal Kottman <k0mpjut0r@gmail.com>:
>>>>> On Mon, 2010-12-20 at 20:48 +0800, starwing wrote:
>>>>>> i.e. i want to do that:
>>>>>>
>>>>>> ??? = coroutine.resume(...)
>>>>>> if ???[1] then
>>>>>> --- do something with ???[1...]
>>>>>> else
>>>>>> --- do something with ???[1...]
>>>>>> end
>>>>>>
>>>>>> but I don't want to use table, I just wonder it's possible to
>>>>>> implement it just operate the multi-values itself.
>>>>>
>>>>> Create locals from them. For example, if your thread yields 3 values:
>>>>>
>>>>> local ok, val1, val2, val3 = coroutine.resume(...)
>>>>> if ok then
>>>>> -- do something with 'val1', 'val2', 'val3'
>>>>> else
>>>>> -- 'val1' is the error message, process it
>>>>> end
>>>>>
>>>>>
>>>>>
>>>>>
>>>> but what if I don't know the amount of multi return values?
>>>>
>>>>
>>>
>>>
>>
>> e, there are something like this Vim code?
>> let a, b, c; d = [1,2,3,4,5,6,7] --> a = 1, b = 2, c = 3, d = [4,5,6,7]
>>
>> in a word, just use one structure/semantics to split a multi return
>> value into two parts, and we can store each part of values.
>>
>
> function take3(a, b, c, ...) return a, b, c, {...} end
> a, b, c, d = take3(1, 2, 3, 4, 5, 6, 7) --> a = 1, b = 2, c = 3, d = {4,5,6,7}
>
>
thank you all :-)
Lua is a extreme flexible language, though it can not control all
types (i mean stack), but we can do it self.
what if we mixed stack (multi-return value and vararg function
arguments) and array-part of table, and make table just a hash one?
maybe the semantics of lua will get more flexibly.