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
Switch sections (experimental feature). #6572
Draft
Moderocky
wants to merge
39
commits into
SkriptLang:dev/feature
Choose a base branch
from
Moderocky:switch-section
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…oday only!) method.
Version thingy. Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
…s' into annotations
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
(switch|check) %objects%
section.As it is experimental, this requires the script to be
using switch sections
.Note
I've never been a huge fan of switch-sections in general, and I've voted against their inclusion every time the issue has come up: everything you can do in a
switch
block you can do with an if/else tree, and I think they're quite unintuitive to non-programmers.Unfortunately, it's practically impossible to improve performance with a switch section over simple if/else because of the fluidity of Skript conditions.
That said, I've created this as a draft PR so that, if they're wanted, I can at least make sure they're done in a sensible way.
Rules
We have consistently tried to make sure every line of Skript code is interpretable on its own, and fairly grammatically sound. This is why we have always avoided expression-only lines.
A design like
case 5:
oris true:
will probably break this rule (as well as being nightmarish to implement with current condition patterns) so I've deliberately avoided it.Switch Section Syntax
The switch section
(switch|check) %objects%
takes in an array of values and checks each against a set of switch case conditions. These cases are run (depending on the mode) if the condition passes.Only switch cases may be used inside switch sections.
Switch Case Syntax
Switch cases look like if-statements:
[case|if] %condition%
Switch Subject Syntax
A special expression
(this|it)
can be used to refer to the current subject of a switch section inside its cases.This is used to prevent multiple evaluation of an expression, but it's not necessary.
Switch Design
A simple switch takes in inputs and, for each input, checks it against every case condition. If a condition matches, the case section will be run.
Example
example results:
This means that all matching conditions will run.
(Functionally equivalent to
if X, if Y, if Z
.)In the example below, input
3
will print both cases, because3
is3
and also a number.Since the switch section is technically a form of loop, the exit and continue effects are available within it, so you can break from a switch when you don't want anything else to run.
Switch cases can be stacked together. Stacking multiple cases requires all conditions to run or the section won't be run.
Example
example results:
Switch Modes
For advanced users only, alternative switch modes are provided: STRICT and FALL-THROUGH.
These are currently locked behind the annotations
@strict switch mode
and@fall-through switch mode
, since they are quite unintuitive and difficult to explain.Strict
A strict switch section will run ONLY the first case matched for each input.
(Functionally equivalent to
if X, else if Y, else if Z
.)example results:
Fall-Through
This mode is designed to simulate Java switch cases. It's quite unintuitive and easy to make a mistake if you don't understand what it does, which is why Sovde suggested I gate it behind an annotation.
A fall-through switch will run the first case matched, and then run every case after that, no matter whether it matches or not.
example results:
This is a really weird way of checking, but it does allow for clever avoidance of code duplication.
Target Minecraft Versions: any
Requirements: none
Related Issues: #4000, requires #6552, requires #6571, transitively requires #6551