lua-users home
lua-l archive

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


Soni They/Them L. <fakedme@gmail.com> wrote:

> On 2018-01-28 12:19 PM, Paige DePol wrote:
>> Soni They/Them L. <fakedme@gmail.com> wrote:
>> 
>>> On 2018-01-28 12:04 PM, Paige DePol wrote:
>>>> Soni They/Them L. <fakedme@gmail.com> wrote:
>>>> 
>>>>> I want a switch where you can use math.random() in switch cases and it
>>>>> lazy-evaluates them and just works.
>>>> What practical use would that even have?
>>>> 
>>>> ~Paige
>>> It's equivalent to if...elseif chains and supports ==
>> I meant using math.random() as a case value.
>> 
>> I think I'd need to see a code example of what you mean
>> to understand how this switch block would work.
>> 
>> ~Paige
> 
> It's just a natural consequence of evaluating them in-order and supporting
> ==, like a desugaring into if...elseif chains.

So, you're saying you'd want something like "case math.random() == 12"?

The case blocks themselves are already essentially == comparisons to the initial
value being switch'd... so couldn't you just do "switch math.random()" and then
do case blocks for each random value you wanted results for? Without you giving
some sort of pseudo-code example I am just grasping at your meaning here! ;)

I also came up short trying to search for examples for what you are asking by
searching for "lazy evaluation switch case" but that came up blank for the most
part. However, I did discover that the language Python also does not have any
support for switch/case, and their reasoning[1] seems to be the same as Lua:


You can do this easily enough with a sequence of if... elif... elif... else.
For cases where you need to choose from a very large number of possibilities,
you can create a dictionary mapping case values to functions to call. 

There have been some proposals for switch statement syntax, but there is no
consensus (yet) on whether and how to do range tests. See PEP 275[2] for
complete details and the current status.


By the status of the PEP it doesn't look like Python will be getting a native
switch/case statement any time soon. However, it looks like I pretty much
implemented PEP 275 for Lua! :)

~Paige

[1] https://docs.python.org/2/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python

[2] https://www.python.org/dev/peps/pep-0275/