Placeholder within function-style object construction, syntactically solves vector
construction uniformity.
#672
Replies: 6 comments 12 replies
-
This has come up a number of times and everytime we just don't get to a solution. While I don't like this fix, a fix is definitely needed. |
Beta Was this translation helpful? Give feedback.
-
Could cpp2 have a shim layer between the std library that patches a small number of problems, so we could gain the benefit of the entire std library, but cover up some of the warts? Basically somethinf that imports the std library and reexports almost all of it unchanged with a few extra helpers or replacements for a few symbols?
On 11 September 2023 07:08:55 Sadeq ***@***.***> wrote:
You're right. Users already suggested many fixes in other discussions.
I hope to have a fix, no matter of which one.
—
Reply to this email directly, view it on GitHub<#672 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQO7UCU46UYQWUWENHDXZ2THLANCNFSM6AAAAAA4SYI7X4>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
So to fix the Cpp1 std library, Cpp2 should:
To forbid bad API in Cpp2, it can make Seq: type = {
operator=: (out this, a: int, more: initializer_list<int>) = {...}
}
x: Seq = (1, 2, 3); // Seq(a: = 1, more: = (2, 3)) What's the point? Cpp2 can produce an error if the user tries to make a bad API: Seq: type = {
operator=: (out this, all: initializer_list<int>) = {...}
operator=: (out this, a: int, more: initializer_list<int>) = {...}
operator=: (out this, a: int, b: int, c: int) = {...}
}
x: Seq = (1, 2, 3); // ERROR!
// It's ambiguous to call which constructor:
// Seq(all: = (1, 2, 3))
// Seq(a: = 1, more: = (2, 3))
// Seq(a: = 2, b: = 2, c: = 3) It's ambiguous, because essentially all constructors have the same signature. So after this change (forbidding bad API), it doesn't matter how the object is constructored (either by Seq: type = {
operator=: (out this, a: int, more: initializer_list<int>) = {...}
}
// These are completely the same:
x: Seq = (1, 2, 3); // Seq(a: = 1, more: = (2, 3))
y: = Seq(1, 2, 3); // Seq(a: = 1, more: = (2, 3)) To fix bad API from Cpp1 std library, one way is to support extension methods and keyword (named) arguments in Cpp2: // `+=` means we are adding methods to an already declared type `vector`.
vector: <T> type += {
// `as` means we are assigning name `count` as a keyword to the argument `n`.
operator=: (out this, n as count: size_type, value: T) = {
// Implementation is here...
}
}
// It calls the constructor from Cpp1 std library.
a: vector<int> = (1, 2); // A vector of (1, 2)
// It calls our declared constructor which has a keyword (named) argument.
b: vector<int> = (count: = 1, 2); // A vector of (2) The syntax can be anything else that can integrate well within Cpp2 grammar. |
Beta Was this translation helpful? Give feedback.
-
Here's a cpp20 example auto a = std::vector(1,2); //[2]
auto b = std::vector({1,2}); //[1,2] I find this very clear and so a solution in cpp2 could be of the form
where |
Beta Was this translation helpful? Give feedback.
-
I thought the guidance and consensus was that C++'s (*) When looked at with modern eyes - at the time it was introduced there were no initializer lists for custom classes' constructors. |
Beta Was this translation helpful? Give feedback.
-
Thanks. It seems I should close this discussion as resolved. Because other fixes are available which can be considered instead of my suggestion. |
Beta Was this translation helpful? Give feedback.
-
Let's make it short...
a
will create a vector of(1, 2)
(list initialization), butb
will create a vector of(2)
(direct initialization).b
is instantiated by function-style object construction syntax. We could write it like this:I suggest to allow to use placeholder (
_
character) on the right-side like this:In this way, we can construct a vector using one of the following uniform syntaxes in a consistent manner:
a
will create a vector of(1, 2)
(list initialization), butb
will create a vector of(2)
(direct initialization). It's complementary to how we use placeholder on the left-side:Every type can be used instead of
vector<int>
in the above examples.Beta Was this translation helpful? Give feedback.
All reactions