I wrote:
> > **Describe the bug**
> > In if-endif block:
> > If TRUE, both `:fini` and `fini` work;
> > if FALSE, `:fini` works, but not `fini` - `E117: Missing :endif`.
> >
> > Seems this only happens with `fini`; other Ex commands work well.
> >
> > **To Reproduce**
> >
> > vim9script
> > if 0
> > echo ":: if-block"
> > fini
> > endif
> > echo ":: Finally"
>
> This behavior is documented: help vim9-discovery
>
> This is not ideal, but it's hard to make this work predictably.
>
> Keep in mind that "finish" is a bit of a weird thing. You never see
> that in a C or Java file. Why would you want the file to continue but
> not use it?
>
> A more realistic example is when defining a function in two variants:
>
> if condition
> def TheFunc()
> ... solution A
> enddef
> else
> def TheFunc()
> ... solution B
> enddef
> endif
>
> It has the same problem: in the discovery phase evaluating "condition"
> may not work. It might be possible to do when it is a constant or uses
> "has('feature')". Defenitely not if it calls a function, since that
> might have side effects.
>
> Now, the question is if we should evaluate "condition" in the discovery
> phase, and what to do if it's not a constant? This may lead to
> unpredictable behavior. I haven't found a good choice yet.
>
> For now, let's approach it as a normal code file, and ignore those
> conditions, parse all the lines. Since it only looks at the start of
> the line, you can make a workaround like:
> if condition | def TheFunction()
> ... solution A
> else | def TheFunction()
> ... solution B
> endif
>
> This doesn't work, since the functions are not found and won't be
> compiled in the next phase.
>
> Not sure if there is any solution in this direction. The alternative
> would be to only compile a function once it is called. You would get
> any errors only then, not when defining the function. We would need to
> fall back to this if we can't make the discovery phase work.
The more I think about this, the more I tend to go for the postponed
compilation.
The discovery phase has some obvious flaws, as mentioned above. It's
quite a big deviation from the previous Vim script behavior, where you
could use "if condition" to skip parts of the script, or chose to define
a function in a separate way. And the problem that started this thread:
Finish the script.
I don't think the discovery phase can be made to work well enough to
behave predictably.
The alternative, not compile functions right away, is not ideal, but we
can deal with the disadvantage. At least it's similar to what happened
before: An error in a function is only found when calling that function.
We can make it a bit better by adding a :compile command. This will
compile all the functions in the current script. Then errors will be
reported early. It won't behave very different from the using the
discovery + compile phases.
Another advantage is that when a script is sourced but functions are
never actually called we don't pay the price of compilation. That makes
sourcing a script a bit faster.
--
hundred-and-one symptoms of being an internet addict:
172. You join listservers just for the extra e-mail.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/202005241726.04OHQgCm090319%40masaka.moolenaar.net.