Comments (6)
Option 4 seems good.
Regarding option 5, a few things I'd suggest considering before adopting the noted implicit flattening/nesting conversions via struct update syntax:
-
For background, considering C++, I think this is a change in behavior which may lead to different results.
.base
gives a syntax to update the parent without confusion about whether the field is on the child, which is not in C++ and would likely be helpful; still, there's a distinction between base and child structs. Maybe there's some way to make struct initialization work, but the naive way doesn't:struct A { int x; }; struct B : public A { int y; }; // Error: field designator 'x' does not refer to any field in type 'B' B c = {.x = 0, .y = 1}; struct D : public A { int x; int y; }; // Valid C++ code. D e = {.x = 0, .y = 1};
-
In most cases of name ambiguity, I would expect ambiguities to be resolved by adding qualifiers. In this case though, ambiguities are resolved by specifying more things: the original designated name remains. This feels a little unusual to me.
// An error due to ambiguity in the `.x` initialization? var a: {.base: {.x: i32}, .x: i32} = {.x = 0}; // Now unambiguous. var b: {.base: {.x: i32}, .x: i32} = {.base = {.x = 0}, .x = 1};
-
What about classes and tuples? In classes, making
base
optional seems like it would be similar, although it may raise concerns about type safety. In tuples, I think it'd already been discussed and the decision was to treat nesting as requiring explicit flattening, not an implicit conversion.For example:
// The struct literal example: var a_struct: {.base = {x: i32, y: i32}, z: i32} = {.x = 1, .y = 2, .z = 3}; var b_struct: {.x: i32, .y: i32, .z: i32} = a_struct; // Similar in classes: class BaseT { var x: i32; var y: i32; } class ChildT { extend base: BaseT; var z: i32; } var a_class: ChildT = {.x = 1, .y = 2, .z = 3}; var b_class: {.x: i32, .y: i32, .z: i32} = a_class; class FlatT { var x: i32; var y: i32; var z: i32; } var c_class: FlatT = a_class; // Similar in tuples: var a_tuple: ((i32, i32), i32) = (1, 2, 3); var b_tuple: (i32, i32, i32) = a_tuple;
Regarding the with
keyword in var xyz: auto = xy with {.z = 3};
, I think that is more explicit, and so doesn't have the same concerns as implicit casts, although name ambiguities may still be odd to resolve [e.g. var a: {{.base: {.x: i32}, .x: i32} = val with {.base = {}, .x = 3};
].
from carbon-lang.
FWIW, I also like option 4.
Options 1-3 seem likely to end up with some amount of friction, whereas option 4 seems to really elegantly express the range of things we want in struct literals and provide important functionality for initializing base classes.
We can always revisit option 5 if/when we have motivation and some ways to deal with both the issues raised in the original description and by Jon.
from carbon-lang.
I think option 4 is my preference here -- it gives a consistent behavior for the name base
in classes and structs, seems (at least a little) useful for structs independent of the class initialization use case, and fits nicely into the class initialization use case.
Option 5 seems a little too implicit and do-what-I-mean-ish to me, and a more explicit struct update syntax seems like a better fit for Carbon's design aesthetic.
from carbon-lang.
Options 4 and 5 are the most tempting to me
from carbon-lang.
I think we are only really supporting struct -> class conversions, not the other direction.
from carbon-lang.
Let's call this decided with option 4 -- seems we have enough consensus among leads and no strong objections. And the comments above have lots of good points of rationale, including from leads.
from carbon-lang.
Related Issues (20)
- Dependent name lookup in base class templates HOT 6
- Exponential blow-up in `impl` matching HOT 2
- Which contexts support implied constraints? HOT 3
- Fuzzer found crash HOT 3
- value representation for structs and tuples HOT 1
- Cant compile HOT 3
- Missing `addr` in self pointer pattern causes crash when called
- are `abstract` / `base` specifiers permitted or required on class forward declarations? HOT 2
- Bazel installation for Debian Does not work
- Does the return of a template function have template phase? HOT 4
- Notation of type value arguments HOT 5
- Making `#` free HOT 14
- Allow `[]` as a part of control flows HOT 1
- Macros and user-defined control flows HOT 2
- Declaring lambdas with `fn` keyword HOT 2
- Logo is really bad HOT 2
- FATAL failure at explorer/syntax/prelude.cpp:22 HOT 3
- Bazel explorer installation error HOT 1
- Explorer crashes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbon-lang.