Git Product home page Git Product logo

Comments (10)

jmarolf avatar jmarolf commented on July 17, 2024

Could you give an example of how this would be useful? Unless I am misunderstanding, don't constructors fill this role today?

from csharplang.

manish avatar manish commented on July 17, 2024

What would be other significant use of this syntax apart from method chaining?

from csharplang.

biqas avatar biqas commented on July 17, 2024

Maybe this one is giving more examples:
http://roslyn.codeplex.com/discussions/562656

from csharplang.

biqas avatar biqas commented on July 17, 2024

@manish
that you can type safe method chain of same reference or derived ones!

from csharplang.

SolalPirelli avatar SolalPirelli commented on July 17, 2024

Duplicate of #155, I think. An equivalent of Scala's this.type would be welcome indeed.

from csharplang.

McZosch avatar McZosch commented on July 17, 2024

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.

biqas avatar biqas commented on July 17, 2024

@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.

Thaina avatar Thaina commented on July 17, 2024

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.

Shadowblitz16 avatar Shadowblitz16 commented on July 17, 2024

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.

biqas avatar biqas commented on July 17, 2024

@Shadowblitz16
this would defeate the purpose of the proposal.

from csharplang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.