-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Extension lowering #7771
Comments
This comment was marked as outdated.
This comment was marked as outdated.
That means you can't do something like this, which is unfortunate: public extension TeacherId for Guid;
public extension StudentId for Guid;
public Person? Load(TeacherId id)
{
return dbContext.Teachers.FirstOrDefault(x => x.Id == id);
}
public Person? Load(StudentId id)
{
return dbContext.Students.FirstOrDefault(x => x.Id == id);
} Instead you'd have to do this: public extension TeacherId for Guid;
public extension StudentId for Guid;
public Person? LoadTeacher(TeacherId id)
{
return dbContext.Teachers.FirstOrDefault(x => x.Id == id);
}
public Person? LoadStudent(StudentId id)
{
return dbContext.Students.FirstOrDefault(x => x.Id == id);
} (I'm also assuming The thing I think/thought I will be using public readonly record struct PersonId(Guid Value)
{
public override string ToString()
{
return Value.ToString();
}
// Override `Parse`, `TryParse`, and add some Json serialization attributes, and so on...
} |
I really wonder how this can be properly done without runtime support. public void Do<T, U>(Dictionary<T, U> dict) where T : ISomeInterface1 where U : ISomeInterface2
{
typeof(T/U) // - what is it here? Is it int or a wrapper?
typeof(T/U).GetMethod("MyMethodFromInterface") // - and how will this work?
dict[default(T)] = default(U); // - what happens here if Resize is called for example? From runtime point of view Wrapper[] and int[] are different types.
}
// extend int with ISomeInterface1 and ISomeInterface2
Do(new Dictionary<int, int>()); |
Adding to what @En3Tho and I said yesterday. I consider being unable to overload based on extensions to be a fairly huge problem, so I really hope this will be considered when working out the implementation details. I think it's time to consider updating the IL metadata. Alongside this issue (extensions) - which I presume would be able to bypass the "According to IL it is the underlying type, so you can't overload based on them" problem with an updated IL representation - there are a few other language features that I believe would benefit from an update to the IL. dotnet/runtime#89730 being the first one that came to mind, but there are a few others that might, like dotnet/runtime#94620 and #5556, and I'm sure many more. I can't really think of an example of Personally - and this might be taking it to the extreme - I would want to be able to disable implicit upcasting of explicit extensions (say that three times fast..), such that the following code would be illegal (I believe it currently is legal): public extension TeacherId for Guid;
public Person? LoadPerson(Guid id)
{
return dbContext.Persons.FirstOrDefault(x => x.Id == id);
}
TeacherId teacherId = ...;
LoadPerson(teacherId); // Personally I want this to error.
Guid guid = (Guid)teacherId; // Explicit casting should be allowed though.
LoadPerson(guid); I realize this can be achieved through Roslyn Analyzers, but I want the language designers to be aware of this use-case. Additionally, I hope the following is illegal by default, because if it isn't, then this feature is practically worthless for my use-case: public extension TeacherId for Guid;
public extension StudentId for Guid;
public Person? LoadPerson(Guid id)
{
return dbContext.Persons.FirstOrDefault(x => x.Id == id);
}
public Person? LoadStudent(StudentId id)
{
return dbContext.Students.FirstOrDefault(x => x.Id == id);
}
TeacherId teacherId = ...;
LoadStudent(teacherId); // No "sideways casting" allowed.
Guid guid = ...;
LoadStudent(guid); // No downcasting allowed. ¹ I am in no way saying there aren't any, just that I can't think of any. |
At that point, just wrap the type manually with a new nominal type. This can also be done easily and cheaply The point of an extension is that it's new functionality, but the same type (just like an extension method). It's just broadening that to all members. If you actually want me types, that's the easy thing that is already supported today :-) |
The point is that that brings a lot of baggage, like requiring custom JsonSerializers,
I agree that |
All that could be done with generators. :-) If you want distinct types, that's the easy case. The challenge has always been in adding functionality to existing types. |
It definitely does. It just doesn't allow things like overloading. But so what? Just name the methods differently. :-) |
Assuming you still need nominal types to do real differentiation, then I fail to see what |
As extensions are themselves nominal types, it seems unfortunate to not be able to treat them as nominal types and to do stuff like overloading on them. Not being able to do so I think makes it awkward to use foo.LoadStudent((StudentId) id);
foo.LoadPerson((PersonId) id); The above proposal doesn't mention |
This discussion has me convinced that people want two different features and are calling both of them "extensions." If you want the thing called "type classes", then the concept of being able to "overload" on extension in non-sensical. The definition of a type class is to define an interface or signature and then define how a type conforms to that interface. Giving a name to that particular implementation has some meaning, although now you are defining something closer to ML modules, but defining a new type is nonsensical. An implementation is, by definition, not a type. A type class is a type (or constraint bound). To say that a consumer of a type class can overload on implementation definitions is as nonsensical as saying that a method taking an interface gets to decide which interface implementation gets chosen. The whole point of the abstraction is that the caller has the flexibility, not the callee. If a new type is defined, then the extensions feature isn't really about type classes at all, it's about subtyping. That is, an extension is really just a new type that is inheriting the components of the base type. While we could have a single base syntax that allows for both features, I'm not sure that's a good idea. |
I think the term In my domain, I don't think anybody would call what I just described as "an extension to Guid", as I'm more likely to change the I don't know what I would call what I want to achieve however¹, but Still confused as to what an ¹ Type classes? @agocke mentioned that, but my googling gave me the most abstract answer I've ever seen, so I've no idea if that's it or not. |
If these aren't intended to be actual types, it also makes absolutely no sense to be able to define variables or parameters of them either, yet that's exactly how you're expected to use explicit extensions. Given the enormous amount of overlap I don't think it makes any sense to make them two completely different language features either. That feels like making them separate features for the virtue of adhering to some strict textbook definition of "type class" rather than providing solutions to solve problems. |
I think the problem is that "explicit extensions" are already a gray area. I'm not super versed on typeclasses, what little understanding I have of them is through Scala which probably takes some liberties in order to make them make sense in the JVM. In Scala, the given implementation or witness is not a nominal type and thus can't be used as a nominal type. As long as the given implementation is in scope, the members of the typeclass are available on the underlying type directly, and via generics you'd only ever reference the witness implementation through the typeclass itself. This also appears to be the case with Rust traits and Swift protocols, where the implementation is either only named for the sake of scoping, or not named at all, respectively. Explicit extensions feel like something completely different in that the witness is a nominal type that you can use as a type in local variables and method signatures. If they aren't a part of the type system, I would argue that it doesn't make sense to allow that. If it's unpalatable to make them a part of the type system because they're called "extensions", then I would argue that maybe we should change "implicit extension" to just "extension" and "explicit extension" to "type". The recent changes to aliasing in C# generated a lot of feedback from people wanting a way to define strict aliases in the language that behaved like types. It feels like extensions gets us something like 95% of the way there, both in terms of syntax and capabilities. |
I have to say that I find this confusing: This issue is already apparent in the declaration: Aside: even with that definition I'm not sure how the extension name would be used, e.g. how to call an instance extension property where it would be otherwise ambiguous? The fact that it can also implement interfaces is where things get conflated in my mind. I'm looking at |
Is the "for" syntax a placeholder? It feels like it wouldn't be too much hard to just reuse the existing base class syntax i.e. public extension MyExt : ThingImExtending this is also later expandable to interfaces without it looking alien i.e. public extension MyExt : ThingImExtending, IMyExtraThingImImplementing which in my opinion looks more familiar than this public extension MyExt for ThingImExtending : IMyExtraThingImImplementing as this could be read as "Implement MyExt for ThingImExtending where ThingImExtending implements IMyExtraThingImImplementing" which, while this isn't a concept expressible now or in the near future in the C# language, from the perspective of someone who spends a lot of time in Rust, this is a mistake that could be made from other languages that have the "impl ... for" pattern and will definitely be confusing once C# has a richer type system more akin to these languages. Obviously I recognise this isn't a work item now, but some agreement on how this could be done in the future is required to ensure the syntax doesn't clash or create confusion with future features. |
@Perksey all syntax is a strawman placeholder for now. |
@Perksey The reason for the public extension BaseExt for Guid;
public extension SuperExt for Guid : BaseExt; Add in interfaces and they're more confusing public extension BaseExt for Guid : IFirst;
public extension SuperExt for Guid : BaseExt, ISecond; Add in partial implementation and they're even more confusing. public extension BaseExt for Guid;
public partial extension SuperExt for Guid : BaseExt, ISecond;
public partial extension SuperExt for Guid : IThird; // I believe you're allowed to omit the base-class in partial declarations. How would this third case work without the public extension BaseExt : Guid;
public partial extension SuperExt : Guid, BaseExt, ISecond; // Is this implementing an interface `BaseExt`?
public partial extension SuperExt : IThird; // Is this extending `IThird` all of a sudden? I thought it was extending `Guid`? .. but it is as @CyrusNajmabadi said; Syntax is secondary, but this is the reason for the current choice, and |
public extension SuperExt for Guid : BaseExt; Surely the
Likely the same way that partial works today, provided my above comment rings true. |
Definitely not: Consider something as simple as:
Then you do:
I would commonly expect this sort of extension hierarchy to occur. |
Ahhhhhhh damn, foiled by OOP again! |
Perhaps public extension<string> StringExtensions : ObjectExtensions as this reads as "public extension of string extending the ObjectExtensions extension". This is noisier, but is also akin to the existing type declaration syntax. The only downside with this one is that it looks extremely strange with generics in the loop. The main reason I'm interested in the syntax is that I have absolutely no doubt that in its current form, most of the discussions around this proposal will be around the intricacies of how it's lowered and expressed whereas I only care about how it looks in my code and how I use it, in its current form with its current constraints I don't see a world in which I'll be surprised with whatever functional decisions are made. I've been surprised before, but this is why I'm asking about the syntax (which is sort of like the final product) rather than the lowering itself (which are sort of the steps along the way) |
Hammering out syntax will come after we're happy with semantics.
That's literally this discussion though :) |
Ah missed that one (just went straight to the latest issue), thanks. |
@HaloFour You may not be very well versed in type classes but you summarized fairly well. Let me give a quick run down of what problem they're meant to solve, and how. Type classes where created to help with "ad-hoc polymorphism". Ad-hoc polymorphism was defined as follows by Wadler[1]:
One historical problem with ad-hoc polymorphism solutions is that they don't integrate well with parametric polymorphism (generics). You can see this is pre-generic-math C# where multiplication couldn't be used on generic type parameters because multiplication was not defined for anything that could be used as a constraint. Even now, if you were to try to define a new operation for Int32, you couldn't do it because that functionality would have to be added to the definition of Int32. Existing ad-hoc polymorphism solutions also don't interact well with generic types. Let's say you wanted to implement deep-equals functionality for C#. You would start by defining an interface interface IDeepEquatable<in T> {
bool DeepEquals(T t);
} Now let's say you want to define it for So you need some mechanism for "conditional implementation." Type classes is one such option. And it appears that wrapping could be one such implementation. The problem is the composite types. If public struct IntWrap(int value) : IDeepEquatable<int> {
public bool DeepEquals(int other) => value == other;
}
public struct ListWrap<T>(List<T> value) : IDeepEquatable<List<T>>
where T : IDeepEquatable<T>
{
public bool DeepEquals(List<T> other) => ...;
} So to construct a public void M(List<int> list) {
if (list is null) { ... }
} Now that always returns false because the wrapper is never null. Repeat for all other pattern checks ad nauseam. The problem is that type classes are meant to describe functions on existing types. They're not meant to introduce new types. Introducing new types introduces new semantics. [1] Wadler, Blott. How to make ad-hoc polymorphism less ad hoc. 1988 |
Hmmm. var list = new List<E>();
var list2 = (List<U>)list; // can this work somehow? Fundamentally |
I'll take that as a win. 😁 I can see where generics makes this a problem, and it's probably why type classes in Scala have to adhere to a particular convention of trait. They don't define the behavior of the type, they define the behavior of the witness working with the type. The witness is entirely separate, and needs to be passed as an implicit parameter. Scala 3 hides a lot of that complexity, but I think it ultimately desugars down to the same thing. Either way, neither Trying to bridge that with normal interface implementation sounds like a runtime conundrum. Type erasure may simplify the result, but it still feels like a lot of runtime work to really make this work. Trouble is, if you have a method with the signature Admittedly I'm a bit out of my league and can see where trying to get this to work at all can create warts and friction points elsewhere. Just, wow: SharpLab using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace TestTypeClasses {
public interface IDeeplyEquatable<in T> {
bool DeepEquals(T other);
}
public struct Int32DeeplyEquatable : IDeeplyEquatable<Int32DeeplyEquatable> {
private int _val;
bool IDeeplyEquatable<Int32DeeplyEquatable>.DeepEquals(Int32DeeplyEquatable other) => _val == other._val;
}
public struct ListDeeplyEquatable<T> : IDeeplyEquatable<ListDeeplyEquatable<T>> where T : IDeeplyEquatable<T> {
private List<T> _val;
bool IDeeplyEquatable<ListDeeplyEquatable<T>>.DeepEquals(ListDeeplyEquatable<T> other) {
if (_val == other._val) {
return true;
}
if (_val == null || other._val == null) {
return false;
}
int count = _val.Count;
if (other._val.Count != count) {
return false;
}
for (int i = 0; i < count; i++) {
T left = _val[i];
T right = other._val[i];
if (left is null) {
if (right is not null) {
return false;
}
}
else if (!left.DeepEquals(right)) {
return false;
}
}
return true;
}
}
internal class Program {
static void Main(string[] args) {
List<int> list1 = [1, 2, 3];
List<int> list2 = [1, 2, 3];
ref ListDeeplyEquatable<Int32DeeplyEquatable> tc1 = ref Unsafe.As<List<int>, ListDeeplyEquatable<Int32DeeplyEquatable>>(ref list1);
ref ListDeeplyEquatable<Int32DeeplyEquatable> tc2 = ref Unsafe.As<List<int>, ListDeeplyEquatable<Int32DeeplyEquatable>>(ref list2);
bool deeplyEquals = AreDeeplyEquals(tc1, tc2);
Console.WriteLine($"Equals: {deeplyEquals}");
}
static bool AreDeeplyEquals<T>(T left, T right) where T : IDeeplyEquatable<T> {
return left.DeepEquals(right);
}
}
} |
Hi @HaloFour |
oops, that's a bug in my implementation, I'll update my comment to reflect the working version: for (int i = 0; i < count; i++) {
T left = _val[i];
T right = _val[i]; // oops! should be: T right = other._val[i];
if (left is null) {
if (right is not null) {
return false;
}
}
else if (!left.DeepEquals(right)) {
return false;
}
} The approach works, although there are warts. The type of |
@HaloFour But BTW, I'm impressed by the power of the type system/memory management of C# to allow "casting" a |
I don't understand what you're wrote, I can cast it: And also, why the
Why not simply : var tc1 = Unsafe.As<List<int>, ListDeeplyEquatable<Int32DeeplyEquatable>>(ref list1);
var tc2 = Unsafe.As<List<int>, ListDeeplyEquatable<Int32DeeplyEquatable>>(ref list2); |
Right, I think you'd use the |
Possible through unsafe hacks that exploit implementation details but likely to cause things to explode at runtime? Yes. Supported? Definitely not. |
A problem here is that Ideally, the following things should be supported:
|
I like the idea of allowing overloads for extensions, but only if the null checking problem is solved. static bool Method(Extension e)
{
return e is null; //This must be able to return true.
} There could be special casing (similar to Edit: I'm very excited for the extensions feature. Huge thanks to the LDT for working on it. Happy holidays! |
@HaloFour Right, so let's dive into type witnesses. From the previous description of type classes you can see that the module system basically creates a dichotomy between types and classes. Types are data. Classes are behavior (functions). If you start messing with the data, you need a different type. But behavior is fluid, flexible, context-dependent. The entire concept is predicated on the idea that types are fundamental, but how you view a type changes depending on the context. Sometimes a List can be compared with another List, sometimes it can't. Sometimes a List is serializable, sometimes it isn't. You want to be able to swap the available behavior on types in and out, automatically, depending on the context. This starts to suggest certain implementation decisions. And this is where type witnesses come in. In type theory, a witness just means that the system gains evidence that a certain evaluation is sound, where it might not be so automatically. Implementation-wise, we might as well also carry the implementation of said evaluation in the witness, since we're carrying it around anyway. And this whole thing implies a fundamental design decision: types and classes (read: interfaces) are stored separately. If your function takes an argument with some sort of interface constraint, the type and the interface implementation would be stored separately. That allows the caller to swap out the appropriate implementation based on context. This is what languages like Scala call a "type witness." The trouble with this approach in C# is, like you've noted, the interface definitions in C# implicitly produce instance invocations. Even though there's nominally an implicit The other observation is that the witness doesn't carry any state. As established, state is part of the type. Only the behavior is part of the class. So all witness are function-definitions-only, no state. This is important in two ways. First, it explains my hesitancy at mixing "new type" with "type class." In conventional type class implementations, there's an important divide between the two. Second, it carries some implementation considerations. When I mentioned that the type witness is an extra argument, one way to do that would be an actual extra function argument. This is the way Scala implements things with implicit parameters. The other way is as a type argument, which is how "type witness" is usually constructed in type theory. Since we know the witness is stateless, there's no need to carry around an extra parameter. We only need type information (dispatch information). You could imagine this as being an empty struct in C#. Consider, interface IDeepEquals<in T> { static bool Equals(T self, T other); }
struct IntWitness { public static bool Equals(int self, int other) => self == other; }
bool M<TEq, TWitness>(TEq left, TEq right) where TWitness : struct, IDeepEquals<TEq>
=> default(TWitness).Equals(left, right); Now we're carrying the witness along as a type argument instead of a function argument. But we still have the problem that the signatures are being defined in terms of the witness type. So maybe some sort of runtime-transparent wrappers are the answer here. Or maybe some specialty type arguments that let you override the implicit receiver. But it all still comes down to what problem we're trying to solve, and what kind of module system we want to attach. Some of these implementation choices are perfectly fine if we want to narrowly solve some specific type-class-focused problems. But once the scope starts expanding, potential problems appear quickly. |
Not to mention trying to make it work with a 22 year old ecosystem. Either way, I think the only real concern I have is what "explicit extensions" are intended to mean, given that they are nominal types in some way. There have been numerous requests for type-safe aliases in the language and it's been suggested that explicit extensions might be a way to accomplish that (if only by me?). Perhaps type erasure doesn't preclude that functionality, but it might create warts. It's not terribly uncommon to run into this with Java and generics, and it's super annoying. |
Posting another scenario here too in case anyone is able to share how would something like this (hypothetically speaking) work. I'd be interested in understanding what possible ways could there be to make this work when lowered. Consider this: struct Foo : IFoo
{
public void DoStuff()
{
}
}
interface IFoo
{
void DoStuff();
}
interface IBar<T>
{
static abstract void Baz(ref T x);
}
implicit extension FooExtension for Foo : IBar<Foo>
{
public static void Baz(ref Foo x)
{
}
}
static void Test<T>(in T x)
where T : IFoo, IBar<T>
{
T.Baz(ref x);
x.DoStuff();
} And then you'd call it like so: Test(new Foo()); The part I'm not really following is:
How would that new interface and implementation for For context, this is exactly the kind of setup I have in ComputeSharp, and the idea would be that once implicit extensions became a thing, assuming this "static interface implementation through an extension" scenario was possible, I could have my source generator emit these implicit extension types for all user defined types, so that they would then be able to call all APIs constrained to both those interfaces (but users would only manually implement the first one). This is pretty much the same kind of setup that eg. @eiriktsarpalis mentioned he'd like to have in System.Text.Json as well. Just trying to get a better understanding at what would possible lowering strategies for this case look like 😄 |
I suppose one way of doing that could be the following:
|
This would regress the runtime performance for all interface IBar { }
extension FooExtension for Foo : IBar { }
F(new Foo());
void F(IBar bar) => Console.WriteLine(bar.GetType()); There's no way for the compiler to distinguish whether an argument passed into the |
@hez2010 I think they hope to be able to do some magic which allows this to work at no cost. |
Just to clarify, in my example I was specifically only talking about extensions implementing static interfaces (ie. only static members), because my understanding is that could be simpler to do than having extensions implementing interfaces with instance members as well. Eg. in the case of just static members, you should be able to get all the info you need just via eg. some special generic type context. Not saying this makes it easy, but just saying it should at least be a subset of the more generalized "some interface" case, and possibly (hopefully?) with a simpler implementation 🤔 |
This question involves a broader scope. Basically there are 2 different scenarios for extension with interfaces: one scenario is calling a non-generic method that simply takes the interface as parameter, and the other is calling a generic method with a generic parameter constrained to the interface. For the first scenario, there are still many questions unanswered. First of all, boxing is unavoidable here, which means that the extension and the underlying instance are going to be two different objects. And if they are different objects, what should For the generic method scenario, it can be done without boxing, which means that extension and underlying object have the same memory layout. But the behaviour of |
Not sure if it's been asked yet, but is there any reason we're not using custom modifiers to enable overloading by extension type (for public extension StudentId for Guid
{
}
public extension TeacherId for Guid
{
}
public static string GetName(StudentId id) => ...;
public static string GetName(TeacherId id) => ...; -> .method public hidebysig static string GetName(valuetype [System.Runtime]System.Guid modopt(valuetype StudentId) id) cil managed
{
...
}
.method public hidebysig static string GetName(valuetype [System.Runtime]System.Guid modopt(valuetype TeacherId) id) cil managed
{
...
} or similar. |
I am not so sure about current use case of I think Suppose we have public class Person
{
public DateTimeOffset birthDay;
}
public implicit extension PersonExt for Person
{
public TimeSpan Age => DateTimeOffset.Now - birthDay; // everyone has age
}
public explicit extension Adult for Person // not everyone is adult
{
public static bool operator is(Person person) => person.Age.TotalYears >= 18;
public bool HasJob => false;
public bool Occupation => "College Student";
}
////
Person person0 = LoadFromDB(pid0);
if(person0 is Adult adult0) // check with operator is
Console.WriteLine(adult0.Occupation);
var adult1 = (Adult)LoadFromDB(pid1) // null if operator is return false Alternative syntax might be public explicit extension Adult for Person when (Age.TotalYears >= 18)
{
public bool HasJob => false;
public bool Occupation => "College Student";
} |
I would expect |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Lowering of extensions
Extensions are "transparent wrappers", that allow types to be augmented with additional members and (eventually) interfaces.
This outlines how we can implement extensions by lowering them to structs, and applying an erasure approach to signatures and generic instantiation.
In this document, base extensions and interfaces are ignored, except for brief consideration at the end.
Extension declarations
An extension declaration like this:
is lowered to a struct declaration, which contains a private field of the underlying type, as well as the function members from the extension declaration, modified to indirect through the field as necessary:
In addition an attribute or other marker may be used to designate that the struct represents an extension.
Extensions in generic instantiations
Extensions used as type arguments or array element types are erased to their underlying type:
is lowered to
This is the main mechanism ensuring that extensions and their underlying types are interchangeable, even through generic instantiation. In this way, a collection of the underlying type can be freely reinterpreted in terms of an extension, and the elements thus gain the extra members afforded by the extension.
Within member bodies, the compiler can keep track of the fact that a
List<U>
is "really" aList<E>
, and provide appropriate conversions.Extensions in signatures
Extensions are erased from signatures, and are instead communicated through metadata (attributes).
is lowered to something like
The exact encoding scheme for the attributes is TBD, but will probably resemble those for nullable reference types and tuple element names, which are similarly type elements that are erased by the compiler.
This encoding scheme means that methods cannot be overloaded by different extensions for the same underlying type.
Extension member access
In order to provide access to extension members, the compiler is able to freely convert between the extension type and its underlying type.
This conversion relies on the fact that an extension always has exactly the same shape in memory as the underlying type. This makes it safe for the compiler to utilize the
Unsafe.As(...)
method.The method
is lowered to
Conversions to and from extensions
The bi-directional implicit identity conversion between extensions and their underlying type can likewise be supported through the use of
Unsafe.As
:which lowers to
If the underlying type is a value type, the user may need to make
e
aref
tou
, so that the value isn't copied in the conversion, and mutations occurring within extension members apply back to the underlying value:which lowers to
Implicit extensions
Implicit extensions are automatically used as "fallback" extensions for their underlying type in a given static scope. The compiler uses lookup machinery similar to today's extension methods to find where extension members are invoked, and implicitly inserts a conversion to the appropriate extension type. This is described in detail in the Extensions specification.
From there, the lowering proceeds as described above.
Base extensions
It is TBD how base extensions are encoded in the lowered extension declaration. When converting to base extensions, the compiler can use the same approach as between extensions and underlying types, since the underlying representation in memory is unchanged.
Extensions with interfaces
The lowering of an extension declaration which implements interfaces is in and of itself simple: Simply add the interfaces to the lowered struct declaration.
However, semantics become complicated. In particular, the implicit conversions between generic instantiations over extensions vs underlying types may no longer apply when the extension implement interfaces, and those interfaces are material to satisfying constraints in the generic instantiation itself.
A compiler-only approach to this would be to simply not have such conversions when an extension implements an interface, but that means adding an interface to an extension is highly breaking. A more permissive and situational approach would likely require significant new runtime feature work however.
The text was updated successfully, but these errors were encountered: