-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Record Fields & Comment References #60452
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
Comments
See #60332 Basically from #60332 (comment):
|
Meaning, while we can't rename them (#53024), these references, even when possible (for named cases), are not useful currently. |
They don't. An anonymous class is still a class, and classes in Dart define a nominal type. If you define two anonymous classes with the same base class you've defined two distinct classes. That is, the instances of those two classes have different types. Record types, on the other hand, are structural types. Two records with the same number and types of fields (and the same names for named fields) have the same type. Whether the developer wants them to or not. For example, given void f((int, int) point, (int, int) range) {} The parameters This would also be true is the fields were named and just happened to have the same names. Some operations that make perfect sense for nominal types just don't make sense for structural types. For example, renaming the first field in a record type of the form |
It makes sense why some operations such as mass renaming does not make sense, as explained in the issues @FMorschel mentioned and based on what @bwilkerson just explained about structural typing.
I agree with this in regard to names specified for positional fields but not for fields formally identified through either (1) a custom name declared via curly brace syntax, or (2) the Records are a convenient alternative for grouping data when the functionality of classes are not needed. However there are still fields whose purpose needs describing or invariants need specifying. Being able to reference the fields helps make the documentation clear and link to relevant declarations. For example, if one uses a specifically structured record throughout a library they will likely have it as a typedef or even represented as a field, which may be discussed by other API. When it is discussed, the linking and formal specification enhances the documentation's utility. Even without the linking, being able to formally specify the identifiers in documentation comments and have them set apart from regular text, as seen in example 3 of the original post, is useful for semantic and syntactic clarity. |
I understand what you're saying, and I don't disagree that it's useful to add documentation about the way the record is expected to be used, but there are some questions we'd need to answer before we could consider supporting this. First, dartdoc currently allows you to use void f(List<int> p) {} the docs can refer to the parameter as void f(int Function(int x) g) {} the docs can refer to the parameter as I don't think we want to start artificially extending the "scope" used for documentation comments because it will result in portions of parameter types shadowing declarations from the enclosing scope, which would be a breaking change. For example, given int length = 3;
/// Print a message if the [length] of [p] is an odd number.
void f(List<int> p) {} the reference to Second, we'd have to decide how to deal with conflicts. For example, given void f((String first, String last) name, (String first, String last) productPreferences) {} Which record field would Or given void f(List<int> list, Map<String, int> map) {} would I'm just not convinced that any of the possible answers are clear enough to not create confusion in some cases. |
I don't think that for parameters we should allow this following your examples. If we do this, we should ask the user to identify: /// This refers to [list.length] and this to [map.length]
void f(List<int> list, Map<String, int> map) {} It would be a different conversation when talking about typedefs IMO. |
That's effectively allowing arbitrary expressions in doc references (as opposed to allowing identifiers, which is what's currently supported). Then the next question is, what kind(s) of expressions do we allow. This is a slippery slope.
Why? |
Agreed, this would be weird. If we ever agree to this, I'd say we should only allow this for structural types since it would mean the value is visible, no reason to reference
This request and #60332 are asking for the same thing (I think), to reference the values inside a This would also mean that considering (copied example from #60332 but with different names): /// - [a] BAD: The referenced name isn't visible in scope.
/// - [b] BAD: The referenced name isn't visible in scope.
/// - [T] GOOD: The referenced name is visible in scope.
typedef PositionalRecord<T> = (
int a,
String b,
);
/// - [a] BAD: The referenced name isn't visible in scope.
/// - [b] BAD: The referenced name isn't visible in scope.
/// - [T] GOOD: The referenced name is visible in scope.
typedef NamedRecord<T> = ({
int a,
String b,
}); The IMO, this is very similar to what we currently have with typedefs for |
I agree that would definitely be bad and should not be done.
Semantically /// [a.length] must be strictly less than [b.length].
void f(List<int> a, List<int> b)
{ assert(a.length<b.length, 'Obey the docs!')
} References such as Optionals would have to be considered to prevent ambiguity and keep syntax consistent but I believe it can end there. So /// [r.count] can be referenced, and so can [r.point.x]!
void f(({int count, ({int x, int y}) point})) r)
{}
/// [Alias.$1] is an integer and so is [Alias.b]!
typedef Alias=(int a, {int b}); |
It isn't an identifier in Dart, it's the invocation of a getter, which is an expression. And I don't think we want to allow expressions in reference blocks.
It is a static reference, and because there's no static member named |
You don't need to allow any expression, just Today I have to write something like If DartDoc knows the static type of (Is there any way to make a DartDoc link to a declaration and control the link text? |
As far as I'm aware, no. Not without an ugly hack based on figuring out the link that DartDoc would have produced and using it explicitly, but that's too fragile so I wouldn't recommend it. |
Dart SDK version: 3.8.0-243.0.dev (dev) (Sat Mar 29 05:02:04 2025 -0700) on "macos_x64"
Documentation comment references to record fields are not recognized. It would be useful to have them recognized. This includes the autogenerated names for positional fields e.g.
$1
.Example 1 -
typedef
My conception of records is that they function like an anonymous class with final fields. I expect it to be treated that way with documentation comments in that the fields can be directly referenced through a type definition of it.
Example 2 - Record Field
I expect to be able to identify the record field via the extension type field.
Example 3 - Function Return Types
I got this directly from @stereotype441's issue (#59526), copying and pasting their examples here for convenience.
Example 3a
Example 3b
All the references of their examples should be valid.
The text was updated successfully, but these errors were encountered: