Skip to content
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

Add generic attributes proposal #4936

Merged
merged 2 commits into from
Jul 20, 2021

Conversation

RikkiGibson
Copy link
Contributor

Related to #124

@RikkiGibson RikkiGibson requested a review from a team as a code owner July 17, 2021 01:53
@YairHalberstadt
Copy link
Contributor

Would a generic attribute parameter be allowed to be a type argument, so long as it was instantiated with a constant?

E.g.

public class Attr<T> : Attribute
{
    public Attr(T t){}
}

[Attr<string>("hello world")]

@RikkiGibson
Copy link
Contributor Author

Yes, I expect that scenario to work. I'll try and make sure the feature branch has a test for it :)


> A type parameter cannot be used anywhere within an attribute.

This means that when a generic attribute is used, its construction needs to be fully "closed", i.e. not containing any type parameters, which means the following is still disallowed:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "closed" the correct term? "Opened" for generic types could refer to Type<>. I didn't think that Type<T> would be considered "open".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so: https://github.com/dotnet/csharplang/blob/main/spec/types.md#open-and-closed-types

I think Type<> is an unbound generic type while Type<T> is an open generic type.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with review pass (iteration 1)

@jcouv jcouv self-assigned this Jul 20, 2021

One important note is that the following section of the spec is *unaffected* when referencing the point of usage of an attribute, i.e. within an attribute list: https://github.com/dotnet/csharplang/blob/main/spec/types.md#type-parameters

> A type parameter cannot be used anywhere within an attribute.
Copy link
Contributor

@Joe4evr Joe4evr Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something about this line had me confused for a bit (as well as once before ~a year ago?), so I'm wondering if the spec needs to be rephrased to emphasize it refers to the attribute placement and not its definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem like it would be a little more clear to adjust the terminology to make attribute usages more distinct from their definitions.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 2)

@RikkiGibson RikkiGibson merged commit 9c2ac83 into dotnet:main Jul 20, 2021
@RikkiGibson RikkiGibson deleted the generic-attributes branch July 20, 2021 17:21
@Piusgodw
Copy link

Piusgodw commented Feb 4, 2022

Today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants