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
Make array.repeat
public
#3978
Make array.repeat
public
#3978
Conversation
I'm confused, isn't this what |
Yes you are right. This is the same function that |
Do we really need all these methods? I feel like they're making the scripting unnecessarily complicated. |
These are just a few features and they make scripting easier, not harder. Many languages, especially functional ones, contain them. The presence of these functions allows you to create chains of methods without having to break the chain. In addition, this particular function makes reading scripts easier, not more difficult. Agree, for example, |
Typst is not a general purpose language |
Perhaps we can find a compromise by making a package with those functions? That way all the other ones in the linked references can be added as well. |
I’m not sure how you’re going to turn the built-in methods into a separate package. After all, Typst is not Rust and it has no traits. Here, either you make full-fledged array methods for chaining, or you stay with a procedural language. |
I think this is rather subjective. I personally find |
Well, I was thinking of using expressions like: |
Again, this is subjective |
Just think about whether this is taking too seriously just one of the dozens of types in Typst. Adding this method does not change the language itself and simply improves ergonomics by adding a function that has analogues in dozens of languages. At the same time, you are referring to a feature that is not even properly documented and is simply mentioned in passing in the documentation. I didn’t know that this was possible until you showed it here. And I think many people don’t immediately find out. You don't expect people to start reading documentation like a novel. They usually open it and quickly search for the desired method, sometimes simply by searching through keywords. You can even look at Rust where there are dozens ways to match Result ( |
No, I really don't think so. This is not about one type, or one method. It's a question of philosophy. Any single change made is obviously not going to change the language itself, but it's worth zooming out and looking at the big picture. What kind of language do we want to end up with? There simply aren't "dozens of languages" occupying the same space as typst does.
If your complaint is about lack of documentation, then that should be addressed by improving the documentation.
I don't see why Rust is relevant to this conversation? |
In my opinion, you are confusing the changes in the syntax of the language with adding a method to one single type out of a dozen types. In this particular case, the addition or absence of this method does not affect "What kind of language do we want to end up with?". Typst already has "methods" and there are "method chains". The decision to add "methods" as a language construct has already been made.
By dozens of languages I meant "dozens of languages" that have types like array and have "methods" as a language construct.
Rust is an example of a language that has many methods that can be replaced using one construct - the |
I'm honestly not sure what you're talking about. My complaint is not methods in general. It's that having methods does not mean you have to add every single method under the sun to the language, especially when there are existing ways to do the same thing. And I emphasize again that it's not about "one method" for "a single type". Obviously there will be more PRs in the future, and the discussion should be had early on.
And rust is a general purpose language that honestly has very little in common with typst apart from being the language it's written in. I'm afraid that there's too much focus on "nice to haves" for power users, to the detriment of how the language feels for the majority of users, most of which will have little (if any) exposure to programming. |
I don't think it would necessarily be a problem to have this both as a method and with the operator ( However, I still have the concern I raised above: When seeing the name repeat, it is not clear to me whether it repeats one element N times, or an array N times, which I think is suboptimal, since both can be useful. The operator fairly easily adapts to both (either |
I don't quite understand you. This method takes value and make an array of length N with that value. If we pass a number, the method will return an array containing the number, if we pass an array, then the method will return an array of arrays: #array.repeat((1,) 3) == ((1,), (1,), (1,))
#array.repeat((1, 2) 3) == ((1, 2), (1, 2), (1, 2))
#array.repeat("a", 4) == ("a", "a", "a", "a") |
What I mean is that the method could also be understood as |
One might agree that one might think that fn main() {
use std::iter;
let array = iter::repeat(4).take(5).collect::<Vec<_>>();
assert_eq!(array, [4, 4, 4, 4, 4]);
let array_of_arrays = iter::repeat([1, 2]).take(5).collect::<Vec<_>>();
assert_eq!(array_of_arrays, [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]);
} |
I was referring to
To an extent, yes, that is what documentation is for. If the method would add new functionality I would also consider this less of a problem, but the fact that it is duplicate functionality and slightly amibigious in what it does leaves the motivation for adding it a bit thin in my opinion. Especially since the existing multiplication accomodates both use cases of |
In my opinion and from my collected voice, the repeating array by multiply is much less clear than repeating array by |
I think that's more of a documentation problem. It is stated in the array reference, but people don't read the text at the top. Python also uses multiplication for lists and I think it works fine there. In combination with the unclarity of whether |
The repeat function is useful not only for multiplication, but also for creating an array from a single value.
User-facing API
Analogues in other languages