lua-users home
lua-l archive

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


It was thus said that the Great steve donovan once stated:
> On Sun, Jun 9, 2019 at 5:23 AM JKB <bcs@hush.ai> wrote:
> >   a = if b == 1 then x else y
> 
> Anybody else really feeling the need for 'end' at the end?
> 
> Is there any serious syntax/semantic reason why the if 'statement'
> cannot be an expression?

  It feels weird to me.

  What does the block return?

	a = if b == 1 then
	  local x = foo()
	  x -- wold have to be valid I guess
	else
	  local x = bar()
	  x + 5 -- this would be valid too I suppose
	end

  What about nested statements?

	a,n = if b == 1 then
	  if c == 2 then
	    5
	  else
	    6
	  end
	else
	  if c == 2 then
	    3,blimy()
	  else
	    wtf() -- does this return one or two values?
	  end
	end

And a function rewritten with the proposed 'if as expr' syntax:

	function request(location)
	  return if location.host == 'localhost' then
	    if location.selector == 'bookmarks' then
	      (require "CONF".bookmarks),0
	    elseif location.selector == 'history' then
	      '/tmp/gopher.log',0
	    else
	      nil,'error'
	    end
	  end

  Eh ...

  -spc