Allow generic extension methods with 'this in T' and T struct to compile #8815
Replies: 8 comments
|
Duplicate of #3331 @tannergooding in that thread:
|
|
@jnm2 |
|
To be clear: In this case I have no direct problem regarding calling a method on a hidden copy of the struct. What I want is that should compile, like already compiles today. The generated IL code and native JIT code should be the same in both cases. Besides the difference in of course. This allows a much more smarter usage (true extension method) and is symetric to the simple static method (without this). A warning when operating on a hidden copy of the parameter struct will be wellcomed equally in both cases. |
|
Any news on this topic? |
News is posted on the form of comments. |
|
Stumbling again over this issue: While 'ref this' compiles fine The analogus with 'ref readonly this' is rejected by the compiler with To my opinion also the second is a completely valid and legal use case. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Overview
I want generic extension methods with
this in Tparameter to compile.The spec explicitly disallows this usage:
"On the other hand
inextension methods exist specifically to reduce implicit copying. However any use of anin Tparameter will have to be done through an interface member. Since all interface members are considered mutating, any such use would require a copy. - Instead of reducing copying, the effect would be the opposite. Therefore inthis Tis not allowed whenTis a generic type parameter regardless of constraints."But not every use of an
in Tparameter will have to be done through an interface member.in Tparameter.Unsafe.SizeOf<T>()orUnsafe.AsRef<T>()and converted to a managed pointer, which does not require any struct copy.Motivation
I have implemented an arbitrary precision floating point library designed for floats from 16 bytes up to a few hundred kbytes. The various floats are defined by a struct of desired size which must only implement the empty interface
IFpFloatReadonlyStruct. No struct methods are implemented directly (beside aToString()override).All float structs share the same generic library code.
The core functioniality is implemented in only 3 extension methods based on
These three base methods are using managed pointer API similar to the
System.Runtime.CompilerServcies.Unsafeclass.These base methods are consumed by higher level arithmetic functions like Add(), Multiply(), Log() and so on.
The JIT compiler really does an incredible good job with function inlining and generic code expansion.
Unintended struct copy does not occure during this usage. But any compiler warning - when doing so - would be strongly wellcomed!
Problem
At the moment every
in TFloatargument passing is done by usingref.But using
refinstead ofinhas following drawbacks:Current Behavior
Compiling
raises following error at the function definition
Desired Behavior
The above code compiles.
Breaking Change
No, because currently this is an error.
But a compiler warning - when unintential struct copy occurs - is strongly recommended.
Remarks
I can supply more detailed examples and use cases, when desired.
Please consider a change.
BR Gerhard
All reactions