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.23k stars 1.48k forks source link

Open design idea: properties #1925

Open chandlerc opened 2 years ago

chandlerc commented 2 years ago

Disclaimer

This issue is part of a series that are just recording language design ideas that have come up for Carbon. It isn't necessarily a good idea, or one that Carbon should definitely adopt. However, it is an interesting area that has come up several times and seems to at least be promising. But it still might not work out!!

Before picking up a language design idea like this and fully developing it, we encourage you to find some folks who are very active in Carbon's language design (as well as potentially one of the leads) and discuss the area with them to get a feel for what would make sense, challenges they anticipate, etc.

Properties

"Properties" in programming languages are a way to begin defining more of an API around fields.

https://en.wikipedia.org/wiki/Property_(programming)

These seem really interesting to help with a number of use cases:

Many other languages have features in this space. Some good places to research before digging into how Carbon should approach this:

Some key ideas in @chandlerc's mind to make a properties design for Carbon compelling:

Others (especially leads) should feel free to add specific considerations in their minds here.

JamesJCode commented 2 years ago

+1 for this goal:

Ideally, would like to reduce the proliferation of almost-colliding names. Goal would be to use a single name for the property accessed and the field storage (if it exists).

This is one thing that can make properties really messy in some other languages.

josh11b commented 2 years ago

Another idea (which I think Swift supports): making it easy for a property declaration in an interface to be backed by a field in a class implementing that interface. This would close a gap where interfaces currently only have functions and types, not data.

chandlerc commented 2 years ago

Another wrinkle to think about here is out-of-line definitions. I can imagine this being especially useful in some cases for example to manage dependencies on assertion / logging frameworks that are implementation details and shouldn't be in the interface.

tkadur commented 2 years ago

General thought - should this be special-cased in the language or done with static reflection/metaprogramming?

jonmeow commented 2 years ago

General thought - should this be special-cased in the language or done with static reflection/metaprogramming?

One of the possible advantages of properties is that they could look identical to member access. That way, members and properties can be transparently swapped without modifying callers. i.e., given the code foo.bar, this could either be accessing a member bar on foo, or using the property bar on foo.

The interchangeability is advantageous both because it allows switching the implementation without modifying callers, and also because it allows a developer to swap direct member access with a property that would allow breakpoint-based debugging within the property access (which a simple member doesn't support quite so well).

If property access is identical to member access, that likely means some special-casing in the language in order to avoid function-like parentheses, i.e. foo.bar().

josh11b commented 2 years ago

One concern I have is that the "set function" paradigm is in conflict with the "all mutation happens through a pointer" paradigm. In particular, right now in Carbon all member functions that mutate a value of a type involve passing a pointer to the value, and all other mutations, like assignments, are going to be rewritten into such calls. I'm particularly concerned about properties being slow due to having to make a bunch of unnecessary calls to the get and set functions when inlining (either the property's functions or the mutations being applied to the property) is not possible.

joseviccruz commented 6 months ago

Hey folks! What is the status of this issue? Is this still something to be considered / has it already been addressed?

lexi-nadia commented 6 months ago

I'm certainly not working on it anymore...