Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"unless" conditional with assignment fails to compile #251

Closed
line0 opened this issue Mar 9, 2016 · 8 comments
Closed

"unless" conditional with assignment fails to compile #251

line0 opened this issue Mar 9, 2016 · 8 comments

Comments

@line0
Copy link

line0 commented Mar 9, 2016

Moonscript v0.4.0 fails to compile an assignment conditionals with the unless keyword (if works).

Minimal Sample:

unless x = true
    -- never encountered

Compiler errors out with:

Compile error: Failed to find value compiler for: {"assign", {{[7] "ref", "x"}}, {{[11] "ref", "true"}}}

Linter dies, too:

PANIC: unprotected error in call to Lua API ((null))

The reference manual doesn't explicitly state that unless in particular can be used with assignment, but as opposed to if not there's no apparent reason why it shouldn't work.
Even if it's not supposed to work, i suppose the compiler should die with a more helpful error message.

@RyanSquared
Copy link
Contributor

if not x = true doesn't work either with native Lua. This is more of a "Unknown error with compiler on bad syntax" than an issue about the syntax?

@line0
Copy link
Author

line0 commented Mar 10, 2016

if not x = true doesn't work either with native Lua. This is more of a "Unknown error with compiler on bad syntax" than an issue about the syntax?

but that's not what it's supposed to compile to.

if x = true
    dongs!

compiles to

do
  local x = true
  if x then
    return dongs()
  end
end

I'll leave it to you to figure out how it'd work with unless

@RyanSquared
Copy link
Contributor

Sorry. I didn't realize if did that.

@leafo
Copy link
Owner

leafo commented Mar 10, 2016

What scenario would you like to use this syntax?

When you use if the variable is only available in the scope of the if block:

if x = something!
   -- x is only available in this scope

When using unless the value would be falsy, so I can't imagine why you'd need a reference to it.

@line0
Copy link
Author

line0 commented Mar 10, 2016

When you use if the variable is only available in the scope of the if block

The variable may be declared beforehand, putting it in a higher scope.

local x
unless x = tryTheOneThing!
    x = assert tryTheOtherThing!, "We're doomed!"

@RyanSquared
Copy link
Contributor

Just to add an example, now that I think of it.

local lfs
unless lfs = tryrequire "lfs"
    lfs = require "syscall.lfs"

Imagine that for this scenario tryrequire is a require() that doesn't error but instead just returns false.

@leafo
Copy link
Owner

leafo commented Apr 11, 2016

In that scenario I think it makes more sense to write:

lfs = tryrequire "lfs"
unless lfs
    lfs = require "syscall.lfs"

@RyanSquared
Copy link
Contributor

Well, that was a basic example. I'm certain there are better reasons for implementing this.

@leafo leafo closed this as completed in c19a7fd Sep 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants