Comments (10)
Could you give an example of how this would be useful? Unless I am misunderstanding, don't constructors fill this role today?
from csharplang.
What would be other significant use of this syntax apart from method chaining?
from csharplang.
Maybe this one is giving more examples:
http://roslyn.codeplex.com/discussions/562656
from csharplang.
@manish
that you can type safe method chain of same reference or derived ones!
from csharplang.
Duplicate of #155, I think. An equivalent of Scala's this.type
would be welcome indeed.
from csharplang.
The idea is pretty simple, I guess.
If you have a immutable base class A and a derived class B, and modifications are made through members in class A, those members always return A. If you want to apply the same modifications to type B without having to do a lot of casts, you need to overload any method within B.
So, the method definition needs to say [type of this], i.e. return [A] when instance is typed [A] and [B] when instance is typed [B].
Two problems:
First, if the method is only implemented in class A, it would need to be implemented type-agnostic to handle all thinkable derived types. It will quickly become a System.Activator.CreateInstance-party, with the compiler handling several variance issues. A performance nightmare coupled with unnecessary complexity.
Second, this feature is already delivered through extensions and type-constraints. This spreads your code over two classes, which is problematic for some coding styles. Maybe, we should be capable of using static class members as extensions.
from csharplang.
@McZosch
Extension methods can't be overridden.
@gafter
Returning "type of the current object" is semantically different to returning "this" reference.
public class BaseClass
{
public virtual this Foo()
{
return this;
}
}
public class DerivedClass1 : BaseClass
{
public override this Foo()
{
return this;
}
}
public class DerivedClass2 : BaseClass
{
public override this Foo()
{
// Is a valid call, because the "this" reference is returned.
return base.Foo();
}
}
Co-variant return types have different semantic.
class Compilation ...
{
virtual Compilation WithOptions(Options options)
{
// the proposal is not allowing new object creation.
return new Compilation();
}
}
class CSharpCompilation1 : Compilation
{
override CSharpCompilation1 WithOptions(Options options)
{
// the proposal is expecting CSharpCompilation as return type.
return new CSharpCompilation1();
}
}
class CSharpCompilation2 : CSharpCompilation1
{
override CSharpCompilation1 WithOptions(Options options)
{
// the proposal is expecting CSharpCompilation2 as return type.
return base.WithOptions(options);
}
}
from csharplang.
Well... after some times I think we could just workaround with generic extension method
public class OurStringBuilder
{
internal void Append();
}
public class OurStringBuilderExt
{
public S Put<S>(this S sb,string s) where S : OurStringBuilder
{
sb.Append(s);
return sb;
}
public S AppendLine<S>(this S sb,string s) where S : OurStringBuilder
{
sb.Append(s);
sb.Append(Environment.NewLine);
return sb;
}
}
new OurStringBuilder().Put("s").Put("t").Put("r").Put("i").Put("n").AppendLine("g");
This is satisfied our need. It also work with interface on struct and everything. Also we could implement most logic in extension method instead of its base class
from csharplang.
What about returning a variable types?
Note this is not the way I would do it but its just a example..
public class Node : I
{
internal Node _parent;
internal List<Node> _children = new List<Node>();
public _parent.GetType() GetParent()
{
return _parent;
}
public _children[n].GetType() GetChild(int n)
{
return _children[n]
}
Returning this only works if the method is in the current class
from csharplang.
@Shadowblitz16
this would defeate the purpose of the proposal.
from csharplang.
Related Issues (20)
- Open issues: Breaking changes HOT 29
- [Proposal]: Field and value as contextual keywords HOT 33
- [Proposal]: Extended identifier syntax
- [API Proposal]: IsNullableAttribute to flow nullability of generic type parameters into methods HOT 4
- Anonymous type optimization HOT 5
- Feature Request: Recursively Foreach
- [Proposal]: Relax `Add` requirement for collection expression conversions to types implementing `IEnumerable` HOT 2
- [Proposal]: Proposal for Allowing Single-Element Tuples in Type Definitions HOT 2
- [Proposal]: Params collections and older language versions HOT 1
- [Proposal]: Edit speclets to distinguish features already delivered from potential future enhancements HOT 3
- [Proposal]: `readonly` parameters in primary constructors for non-record types HOT 7
- [Issue]: Generic Type Specialization Not Working in C# HOT 38
- [Proposal]: AAAAAAAAAAAAAAAAAAAA
- Open questions for `field` and `value` as contextual keywords HOT 2
- Add collection literal support for `Memory<T>` and `ReadOnlyMemory<T>`. HOT 18
- Unexpected compiler behavior with records and implicit operators HOT 1
- YaoJunFeng
- Invalid implicit conversion from long to int HOT 6
- [Proposal]: Extending patterns to "as" HOT 19
- Proposal: Searched switch statement HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csharplang.