-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Refactor implementation of array-like literals #5703
Comments
Why not use |
No, why not just use Path.new("path", "to", "file"). Or I'd even prefer that constructor named Path.join. Path isn't an array and it shouldn't be treated like one. |
A path consists of a sequence of components... I don't think this wouldn't qualify as a suitable use case for an array-like literal. Of course you could just use If this is not wanted for Path, you could go one step further: why are there array and array-like literals at all? Array literals could also be written as |
There's an array constructor with two integers: size and default value. So that won't work. |
Yeah that's not entirely thought out, I'm just trying to make a point against that. It could probably just use a different name. Or a symbol such as |
Arrays, tuples, named tuples, and to a lesser extent hashes are by far the most common data types and are worth special syntax. I'm not sure less-generic or less-common types should have the same. I'm actually fine with this idea of changing the way generic array literals work, its just I would argue against it being applied to path. |
Hit that issue when tried to create immutable (so it can't have
Yet, it would be nice to have an uniform way to declare collections always the same way |
That can be achieved at the API level, not the language semantics though. |
An array-like literal such as
Foo{1, 2, 3}
is currently rewritten to create a new instance and sequentially invoke<<
for each item. So something like this:This implementation works well for regular collection types like
Set
or custom array aliases.When implementing #5635 (adding a
Path
type) it showed some drawbacks. Literals likePath{"path", "to", "file"}
are much slower than creating an instance likePath.new("path", "to", "file")
. The latter invokesjoin
with all arguments at once which is much faster than invokingjoin
once for each component.Another issue with this is that you can't have really immutable objects.
@asterite suggested to simply rewrite
T{x, y, z}
toT.new({x, y, z})
orT.literal(x, y, z)
. This could even be optional: If a class implements aliteral
method, it is used. Otherwise, it could keep the current behaviour with<<
.It would be nice to find a similar solution for hash-like literals, although this is a bit trickier. A dynamic-sized list of items can easily be passed to a method as a tuple but hash-like literals contain key-value pairs. But I guess it could work if each kv-pair is converted to a tuple, so that
T{"a" => 1, "b" => 2}
is rewritten toT.literal({{"a", 1}, {"b", 2}})
.The text was updated successfully, but these errors were encountered: