-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
discussion: tuples to replace ... #533
Comments
Previous discussion here about the general tuple idea: #208 |
could be replaced with
where the compiler constructs the tuple from provided parameters, w/o explicit syntax. |
@tiehuis, oops, missed that one, thanks! The use cases I see are:
@PavelVozenilek, the problem with having the compiler do it is that it is still "magic." How would I assign a tuple to a field in a struct using your syntax? How would I pass a tuple as one of the middle arguments in a function, but not the last one? The problem I see with the current use of |
@kyle-github: yes, it is magic, special case, but "natural" and easy to use. People tend to avoid correct but clumsy solutions (e.g. C++ streams). Ordinary tuple in the middle should be passed via the As an aside, some languages, e.g. Kotlin and Nim, support "trailing inlined function definition", something like:
where the |
@PavelVozenilek, err, not sure what you mean by avoiding C++ streams. You do not see many calls to printf in C++ anymore. Everything is "<<". Here is an alternate proposal that builds on what is already in Zen a little more cleanly.
That would support the variadic case but still provide a less "magic" foundation for it than all the special cases around the current Since I know nothing about how the compiler is implemented I cannot comment on how hard this would be. I suspect that what could be done would be to mark functions with |
@kyle-github: C++ streams are not seen as the final solution. I remember many libraries like Boost.Format. I do not have proposal how to implement varargs internally, only pointed out that their use should be simple, that even single excessive character is one too many. I use varargs quite a lot: for detailed asserts (like |
Re-opening the tuple issue: #208 |
This follows from the discussion in #522 and the use of
...
.Currently the only way to pass heterogeneous params is to explicitly list them or use the magic
...
syntax. But there is no way to use such a thing in a struct field or anywhere else. The magic only works as the last argument of a function.Borrowing the idea of tuples from other languages may help. Tuples are often accessed as if they were arrays, but the types of each element are different. Tuples' lengths and component types are known at compile time.
Tuples could help with that and remove, I think, the need for
...
entirely.This is just made up syntax.
Using this would allow you to write a printf-like function without any magic other than inline tuple creation. Since the compiler supports inline struct creation, this would be almost the same, only positions instead of names.
For instance, here is the example printf implementation rewritten to use tuples, and you do not need the
...
magic.I think there are only two changes. The first is to use the new type
tuple
for the arguments. The second is the introduction of a test for the argument tuple length in the part that generates the output code for the argument. This allows both argument overflow and underflow to be checked. Underflow was not checked before.I am not sure that this is otherwise valid syntax, but hopefully the idea carries across anyway.
You can get much the same effect if you allow inline creation of
[]var
. That works as well. I think that it is a little different in that you allow any value at any position whereas a tuple would be implicitly typed at each position. I.e. you could not assign the wrong type to a tuple position.So while a
var
array gives you some of the flexibility, I do not see how it would be able to give you the compile time type checking. A tuple is like a struct without the names.The text was updated successfully, but these errors were encountered: