carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)
http://docs.carbon-lang.dev/
Other
32.27k stars 1.47k forks source link

Calling member functions like C++ #2048

Open L4stR1t3s opened 2 years ago

L4stR1t3s commented 2 years ago

From https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#methods:

Methods are called using the dot . member syntax, c.Diameter() and c.Expand(...).

I suggest also allowing calling methods like this: Diameter(c) and Expand(c, ...), like some C++ STL functions.

begin(), end(), size(), empty(), ... are defined in the STL to access them like begin(vec) for example. begin() and end() for example also have implementations for C style arrays. This means it is possible to write generic code that works for both STL containers and C style arrays.

If Carbon made member functions by default available as func(object), there would be no need to define template functions to access member functions, if you want to do this.

This can especially come in handy when writing templated code:

void print(int x) { ... }
struct Object {
    void print() { ... }
}
template <typename T>
void func(T x) {
    ...
    print(x);
    ...
}
geoffromer commented 2 years ago

I'm fairly sure C++ doesn't allow calling member functions that way, so I'd suggest changing the issue title to something like "Calling member functions as free functions".

I think adapters already address the template use case for this feature in a more general way.

L4stR1t3s commented 2 years ago

I'm fairly sure C++ doesn't allow calling member functions that way, so I'd suggest changing the issue title to something like "Calling member functions as free functions".

Uh, right. Was writing this, and then was called away and when I came back I assumed I had already written more than I apparently had...

I forgot to mention that the C++ STL offers a whole range of template functions to call common member functions like begin(), end(), empty(), size(), ... so you can use them like func(object). begin() and end() for example also have an implementation for C style arrays, meaning you can write generic code that loops over STL containers as well as C style arrays.

I was thinking that this could be a generic feature for Carbon.

I will update the initial post.

geoffromer commented 2 years ago

I forgot to mention that the C++ STL offers a whole range of template functions to call common member functions like begin(), end(), empty(), size(), ... so you can use them like func(object). begin() and end() for example also have an implementation for C style arrays, meaning you can write generic code that loops over STL containers as well as C style arrays.

The C++ standard library has to do this to work around limitations of C++ that don't apply to Carbon. C style arrays are not library types or class types, so they can't be extended to support method calls. In Carbon, our intent is that all object types (including array types) are library types, so for example, if container iteration in Carbon is defined in terms of Begin and End methods, we can define those methods on the array type. So it seems like this feature would be much less useful in Carbon than in C++.

github-actions[bot] commented 1 year ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time. This issue is labeled inactive because the last activity was over 90 days ago.