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 support for target typed new expressions #990

Draft
wants to merge 5 commits into
base: draft-v9
Choose a base branch
from

Conversation

RexJaeschke
Copy link
Contributor

The MS proposal contains a "Miscellaneous" section, which contains a list of "consequences of the specification." These have not been added to this PR! Someone should verify that these are covered sufficiently by normative text, and even if they all are, it might be worth adding some Notes to that effect.

@RexJaeschke RexJaeschke added the type: feature This issue describes a new feature label Nov 12, 2023
@RexJaeschke RexJaeschke added this to the C# 9.0 milestone Nov 12, 2023
@RexJaeschke RexJaeschke marked this pull request as draft November 12, 2023 20:24

There is an implicit ***object-creation conversion*** from a *target_typed_new* expression ([§12.8.16.2](expressions.md#128162-object-creation-expressions)) to every type.

Given a target type `T`, if `T` is an instance of `System.Nullable`, the type `T0` is `T`'s underlying type. Otherwise `T0` is `T`. The meaning of a *target_typed_new* expression that is converted to the type `T` is the same as the meaning of a corresponding *object_creation_expression* that specifies `T0` as the type.
Copy link
Contributor

Choose a reason for hiding this comment

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

System.Nullable is a type but not a generic type. How is this worded elsewhere — System.Nullable<> or System.Nullable<T>?

@@ -2296,6 +2301,10 @@ object_or_collection_initializer

The *type* of an *object_creation_expression* shall be a *class_type*, a *value_type*, or a *type_parameter*. The *type* cannot be a *tuple_type* or an abstract or static *class_type*.
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I suppose this disallows dynamic and pointer types as type even in target-typed new.

@@ -2296,6 +2301,10 @@ object_or_collection_initializer

The *type* of an *object_creation_expression* shall be a *class_type*, a *value_type*, or a *type_parameter*. The *type* cannot be a *tuple_type* or an abstract or static *class_type*.

If `type` can be inferred from usage, it can be omitted, as allowed by *target_typed_new*. It is a compile-time error to omit `type` if the type cannot be inferred. A *target_typed_new* expression has no type. However, there is an implicit object-creation conversion (§imp-obj-creation-conv) from a *target_typed_new* expression to every type. It is a compile-time error if a *target_typed_new* is used as an operand of a unary or binary operator, or if it is used where it is not subject to an object-creation conversion.
Copy link
Contributor

Choose a reason for hiding this comment

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

Together with C# 9 target-typed conditional expressions, this will then allow

class C {
    char M() {
        return new() ? new() : new();
    }
}

in which the target types will be inferred as

class C {
    char M() {
        return new bool() ? new char() : new char();
    }
}

It might be clearest to merge that feature before this one, so that there is no doubt about why target-typed new is allowed in operands of the ternary operator.

@gafter gafter self-assigned this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature This issue describes a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants