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

Support string concatenation. #6576

Merged
merged 9 commits into from
May 8, 2024

Conversation

Moderocky
Copy link
Member

Summary

  1. Allows the joining of strings with +, e.g. "foo" + "bar" = "foobar".
    Does not allow the joining of strings and numbers or other non-coercible objects.
  2. Adds a var-args concat(...) function that permits joining anything into a single string (including non-texts).

Description

I'm not a fan of strings as they are in Skript: I dislike that there's no true literal, I dislike how cumbersome and inefficient "%...%" interpolation is (both to write and to actually evaluate).
Aside from interpolation, our only other option for joining strings is the expression (concat[enate]|join) %texts% [(with|using|by) [[the] delimiter] %text%] which has a rather unpleasant pattern (it sounds more like an effect than an expression).
This is designed for joining texts, but it can't coerce types properly, so you still end up needing to use interpolation (or store things in middle man variables) to actually join things that aren't strings (e.g. "your lucky number is " and 66).

What This Doesn't Do

In 2.7 or 2.8 (I forget which) a deliberate change was made to make illegal expressions in maths completely fail (evaluate to <none>) rather than be permitted as 0.
This doesn't change that. This doesn't permit you to add "hello" and 81: it will fail at parsing time and, even if you try and get around that with variables, it will still evaluate to <none> as intended.

String addition ONLY supports adding strings together, whether they're from expressions, variables or "...".

broadcast "hello" + "world" # OK!

set {var} to "..."
broadcast "hello" + {var} # OK

broadcast "hello" + [some string expression] # OK
broadcast "hello" + 1 # NOT OK! Fails at parse time

set {var} to 1
broadcast "hello" + {var} # NOT OK! Bad math to <none>

broadcast "hello" + [some non-string expression] # NOT OK! Probably fails at parse time

String Addition

This pull request adds string addition arithmetic: "A" + "B" = "AB".

Lots of languages allow you to concatenate (glue together) strings. Most languages use +, some like to do their own thing with . or & or some other joiner.
Just because other languages do something isn't a sufficient reason to add it to Skript, though.

I like it as a feature:

  • I think it's arguably cleaner and neater than "%...%" (especially if you have multiple interpolations in one text!)
  • It's especially cleaner than trying to use an expression (and heaven forbid a text) inside an interpolation, where you're having to double your quotes and whatever.
  • I think, realistically, it's more immediately understandable to a beginner that "a" + "b" = "ab" than whatever's going on with "a%""b""%".
  • It ought to be safer/easier for the machine to interpret, interpolation's usually pretty heavy (although this is Skript, who knows).

String concat(...) Function

This pull request adds a concat(...) variable arguments function.
This is more advanced than the concatenate|join expression, since it properly supports non-text parameters even without coercion.

Concat takes in multiple objects and returns the result of all of them stringified and glued together.

concat("hello") = "hello"
concat("hello", " world") # "hello world"
concat("hello", " ", "there") # "hello there"

set {_var} to "hello"
concat({_var}, " world") # "hello world"

Concat allows the joining of strings and objects, which are run through Classes.toString.

concat("foo", 1) # "foo1"
concat("my nice new ", stone sword) # "my nice new stone sword"

Concat ALWAYS returns a text, even if none of the inputs are text. It doesn't do maths evaluation, it purely joins things into a text.

concat(1, 2) # "12"
concat(1, 2, 3) # "123"
concat(stone sword, stone sword) # "stone swordstone sword"

Target Minecraft Versions: any
Requirements: none
Related Issues: none

@Moderocky Moderocky requested a review from UnderscoreTud April 16, 2024 09:47
@Moderocky Moderocky added enhancement Feature request, an issue about something that could be improved, or a PR improving something. feature Pull request adding a new feature. labels Apr 16, 2024
@AyhamAl-Ali AyhamAl-Ali added priority: lowest "Nice to have" updates that are not required (tiny low impact bug fixes or QoL enhancements). feature Pull request adding a new feature. and removed feature Pull request adding a new feature. priority: lowest "Nice to have" updates that are not required (tiny low impact bug fixes or QoL enhancements). labels Apr 16, 2024
Copy link
Member

@AyhamAl-Ali AyhamAl-Ali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful 🎉 Let's hope it doesn't break anything we didn't think of

Copy link
Member

@UnderscoreTud UnderscoreTud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For string + string should we treat an empty variable as an empty string? E.x. "some string" + {_none} = "some string"

@Moderocky
Copy link
Member Author

For string + string should we treat an empty variable as an empty string? E.x. "some string" + {_none} = "some string"

Do you have a suggestion? I feel like it shouldn't return nothing as the result like with invalid math.

Moderocky and others added 2 commits April 16, 2024 17:43
Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>
Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>
@Moderocky Moderocky requested a review from UnderscoreTud April 16, 2024 17:50
@UnderscoreTud
Copy link
Member

UnderscoreTud commented Apr 24, 2024

For string + string should we treat an empty variable as an empty string? E.x. "some string" + {_none} = "some string"

Do you have a suggestion? I feel like it shouldn't return nothing as the result like with invalid math.

You can do that by registering a default value for strings using Arithmetics.registerDefaultValue

@AyhamAl-Ali AyhamAl-Ali added the feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version. label Apr 24, 2024
@Moderocky
Copy link
Member Author

You can do that by registering a default value for strings using Arithmetics.registerDefaultValue

Yes I know, I was asking what you think the default should be (if not what it is now).

@UnderscoreTud
Copy link
Member

You can do that by registering a default value for strings using Arithmetics.registerDefaultValue

Yes I know, I was asking what you think the default should be (if not what it is now).

You can just make it an empty string

@Moderocky Moderocky merged commit 5928f8c into SkriptLang:dev/feature May 8, 2024
5 checks passed
@Moderocky Moderocky deleted the string-concatenation branch May 8, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request, an issue about something that could be improved, or a PR improving something. feature Pull request adding a new feature. feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants