Generic static extension member errs when used on a type parameter #9651
Unanswered
aradalvand
asked this question in
Language Ideas
Replies: 3 comments
-
Possible related and/or duplicate issue: |
Beta Was this translation helpful? Give feedback.
0 replies
-
Looks very odd to me, too. Especially because an explicit extension-member call would work as written by aradalvand: using System;
using System.Numerics;
var s = new Something<IntId>();
s.DoSomething();
/// <summary>
/// Represents an opaque unique identifier type.
/// </summary>
public interface IId<TSelf> : IEquatable<TSelf>, IEqualityOperators<TSelf, TSelf, bool>
where TSelf : struct, IId<TSelf>
{
abstract static TSelf From(Guid guid);
abstract static TSelf Parse(string guidAsString);
abstract static implicit operator Guid(TSelf self);
}
record struct IntId(Guid guid) : IId<IntId>
{
public static IntId From(Guid guid)
{
return new(guid);
}
public static IntId Parse(string guidAsString){
return From(Guid.Parse(guidAsString));
}
public static implicit operator Guid(IntId self){
return self.guid;
}
}
public static class IdExtensions
{
extension<T>(T) where T : struct, IId<T>
{
public static T New() => T.From(Guid.CreateVersion7());
}
}
public sealed class Something<TId>
where TId : struct, IId<TId>
{
public void DoSomething()
{
//var newId = TId.New();
var newId = IdExtensions.New<TId>();
Console.WriteLine(newId);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
We're not planning to relax this restriction in C# 14: https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-04-16.md#extension-static-member-lookup-on-generic-types-does-not-work |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here's a realistic reproduction which yields:
There is no reason this should not be supported — as acknowledged by @CyrusNajmabadi here.
Expected Behavior:
The code should compile with no errors.
Actual Behavior:
It errs on
TId.New()
.Just lower the damn thing to
IdExtensions.New<TId>()
. What's so hard about that?Beta Was this translation helpful? Give feedback.
All reactions