Skip to content

Commit 4034556

Browse files
committed
Add target-typed static member access examples
1 parent afad497 commit 4034556

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed

proposals/target-typed-static-member-access.md

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ This reduces construction and consumption verbosity for factory methods, nested
1313
```cs
1414
type.GetMethod("Name", .Public | .Instance | .DeclaredOnly); // BindingFlags.Public | ...
1515
16-
control.ForeColor = .Red; // Color.Red
17-
entity.InvoiceDate = .Today; // DateTime.Today
18-
ReadJsonDocument(.Parse(stream)); // JsonDocument.Parse
16+
if (someString.Equals("Value", .OrdinalIgnoreCase)) // StringComparison.OrdinalIgnoreCase
17+
...
18+
19+
control.ForeColor = .Red; // Color.Red
20+
entity.InvoiceDate = .Today; // DateTime.Today
1921
2022
// Production (static members on Option<int>)
2123
Option<int> option = condition ? .None : .Some(42);
@@ -29,6 +31,9 @@ return result switch
2931
.Success(var val) => val,
3032
.Error => defaultVal,
3133
};
34+
35+
[AttributeUsage(.Class | .Struct | .Interface | .Enum | .Delegate)]
36+
class MyAttribute : Attribute;
3237
```
3338

3439
## Motivation
@@ -461,3 +466,85 @@ We can follow the approach already taken for the similar ambiguity in collection
461466
Alternatively, target-typed static member access could be always disallowed within the first branch of a conditional expression unless surrounded by parens: `expr ? (.Name) : ...`. The downside is that this puts a usability burden onto users, since the compiler can work out the ambiguity by looking ahead for the `:` as with collection expressions.
462467

463468
**Recommendation:** Allow `expr ? .Name :` by looking ahead for `:`, just as with collection expressions.
469+
470+
## Examples
471+
472+
### Pattern matching enums
473+
474+
```cs
475+
public static string GetTypeString(ContractTransactionMode mode)
476+
{
477+
return mode switch
478+
{
479+
.Preproduction => Preproduction,
480+
.MoltPreproduction => MoltPreproduction,
481+
.Production => ProductionFixedWeekly,
482+
.EggProduction => ProductionPerGradeADozen,
483+
.LayerBonus => LayerBonus,
484+
.PulletGrower or .PulletGrowerFinal or .PulletGrowingFinal
485+
or .PulletGrowingAP or .PulletGrowingAR or .PulletGrowerBonus => PulletGrowerOrGrowingPayment,
486+
};
487+
}
488+
```
489+
490+
```cs
491+
if (alias.ItemAliasType is not (.Vendor or .User)) ...
492+
```
493+
494+
### Returning enums
495+
496+
```cs
497+
private static ContractTransactionMode GetMode(string paymentType)
498+
{
499+
return paymentType switch
500+
{
501+
Preproduction => .Preproduction,
502+
MoltPreproduction => .MoltPreproduction,
503+
ProductionFixedWeekly => .Production,
504+
ProductionPerGradeADozen => .EggProduction,
505+
PulletGrowerOrGrowingPayment => .Pullet,
506+
LayerBonus => .LayerBonus,
507+
};
508+
}
509+
```
510+
511+
### Translating one enum to another
512+
513+
```cs
514+
LineItemType lineItemType = transactionType switch
515+
{
516+
.Invoice => .Sale,
517+
.CreditMemo => .Return,
518+
};
519+
```
520+
521+
### Using System.Reflection enums
522+
523+
All the focus is on the meaning and not on repeating the containing enum name four and five times:
524+
525+
```cs
526+
foreach (var property in type.GetProperties(.Public | .NonPublic | .Static | .Instance | .DeclaredOnly))
527+
{
528+
if (property.SetMethod is not { } setter)
529+
{
530+
continue;
531+
}
532+
533+
if ((setter.Attributes & .MemberAccessMask) is not (.Public or .Family or .FamORAssem))
534+
{
535+
continue;
536+
}
537+
538+
// ...
539+
}
540+
```
541+
542+
### Using generated interop (e.g. CsWin32)
543+
544+
It's typical to see generated enum member names be the same as the original constant, which is to say that the enum name itself is pretty redundant:
545+
546+
`FILE_ACCESS_FLAGS.FILE_GENERIC_READ | FILE_ACCESS_FLAGS.FILE_GENERIC_WRITE`
547+
548+
Instead, this could just mention the well-known constant names which are what you'd search anyway :
549+
550+
`.FILE_GENERIC_READ | .FILE_GENERIC_WRITE`

0 commit comments

Comments
 (0)