Replies: 19 comments
-
This looks like a dupe of dotnet/roslyn#13 Is there a difference here between this proposal and that one? |
Beta Was this translation helpful? Give feedback.
-
Worth tracking here anyway since this repo now relates to language changes? I do think that anonymous and/or local implementations would be a nice feature and it's something that I use frequently enough in Java. Delegates/lambdas are great but can only represent a single action. If I'm building out the implementation of a fluent API it just adds to the boilerplate to then have to define a nested type and manually capture the appropriate data. For the anonymous implementation I'd much prefer the Java-style syntax where the type being instantiated is the interface name: var a = new ISomething {
void Do(int arg) { ... }
void Do2(int arg1, int arg2) { ... }
}; Of course going down this path opens numerous cans of worms, such as solving scope resolution ( |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi It's such a juicy and immediately useful feature that I still can't believe you aren't the first to champion it. So weird. |
Beta Was this translation helpful? Give feedback.
-
:) It's very uninteresting to me (and i came from a Java background). |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I agree that java-like syntax is better. And, of course, it's make things more clear (you see what interface was implemented). But I think, it should be optional. Explicit interfere declaration can be omitted if left part of assignment isn't
Declared anonymous/local class should always "win" in case of any ambiguity. Reason same as above - to minimize compiler errors after you do copy/paste. And you would do copy/paste when you would convert internal or private nested class to local/anonymous.
Should be allowed as in regular classes - i.e. you can declare default property value as you do in regular class. Developer also can "initialize" property values with values from captured closures. var v = "test string";
IInterface a = new {
string StringProperty { get; set;} = v; // we set up default value from captured closure
}
Anonymous class should have only parameterless constructor. Constructors with parameters shouldn't be allowed. Compiler would emit constructor with parameters behind the scene (to pass closure object and lambda parameters from factory function), but it's implementation details that developer should not know. Developer should be allowed to create only parameterless constructor. If base class (in the case of anonymous implementation of abstract class) haven't parameterless constructor, developer could pass captured closure values or lambda parameters to base class constructor. Compiler would care of correct values passing. var v = "value";
AbstractParent a = new {
this() // use "this" keyword for constructor name
:base(v) // pass captured value to base class constructor
{
}
};
Definitely yes! Anonymous implementation is the equivalent of private nested class. Just with captured closures and mangled compiler-generated type name. |
Beta Was this translation helpful? Give feedback.
-
I can see two use-cases for this:
var result = from thing in things
where thing.Foo == 1
select new { thing.Bar, thing.Baz }; Becomes: var result = from thing in things
where thing.Foo == 1
select new IThing { thing.Bar, thing.Baz }; and now |
Beta Was this translation helpful? Give feedback.
-
@DavidArno one major benefit of local/anonymous classes is closure support. It can save lots of lines of code. Built-in ability to get and update local variables values without even single additional line of code. Closures is major reason why lambda functions and local functions are so powerful. It's natural to reuse same power with anonymous/local classes. |
Beta Was this translation helpful? Give feedback.
-
I think local type declarations are more appropriate here, IFoo M() {
class Local : IFoo { .. }
return new Local();
} pros: needs a lot less syntax changes, supports closures, attributes, etc. more readable overall. PS: This new IThing {
public Bar Bar => thing.Bar;
public Baz Baz => thing.Baz;
}; |
Beta Was this translation helpful? Give feedback.
-
I'd see it as a mashup of existing anonymous type features and what's proposed in the OP. For the line: var x = new { thing.Bar, thing.Baz }; The resultant anonymous type has So for the line: var x = new IThing { thing.Bar, thing.Baz }; Then, as long as the signature of
then the anonymous type can implement that interface. This could then be extended by the ideas in this proposal, eg if public interface IThing
{
Bar Bar { get; }
Baz Baz { get; }
int Do(int index);
} Then the type declaration could be: var x = new IThing
{
thing.Bar,
thing.Baz;
int Do(int index) => Bar[index];
}; And if the properties don't match, then the usual anonymous type property matching can be used: var x = new IThing
{
Bar = someOtherThing.FooBar,
Baz = someOtherThing.FooBaz;
int Do(int index) => Bar[index];
}; |
Beta Was this translation helpful? Give feedback.
-
Local types would mollify me. |
Beta Was this translation helpful? Give feedback.
-
I have long wanted something like this; I agree with @HaloFour and @DavidArno's sketches that the instantiation should include the interface type's identifier, e.g. |
Beta Was this translation helpful? Give feedback.
-
@tuespetre // "Local" interface implementation inside method code
class LocalImplementation : ISomething
{
void Do(int arg) {
result = arg; // result variable is captured as closure!
}
void Do2(int arg1, int arg2) {
result = arg1 + arg2; // result variable is captured as closure!
}
} Sometimes it's excessive to declare interface explicitly when left side of assignment is well-known. ISomeInterface a = new {
// interface members
} -- for example Of course, you can write Let's give developer freedom to choose when explicitly declare interface/base class type of anonymous implementation and when omit it. |
Beta Was this translation helpful? Give feedback.
-
At the risk of appearing contrary, I should point out that I'd be happy with either syntax (ideally both): var foo = new IFoo { ... }
IFoo foo = new { ... } I just tend to use |
Beta Was this translation helpful? Give feedback.
-
Good points. Maybe there is some interplay with the shapes proposal to be considered here too. 🤔 |
Beta Was this translation helpful? Give feedback.
-
I could swear I commented about this, but maybe I just forgot to click the button. Anyway, |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr oh, nice! I hadn't looked at that one yet. |
Beta Was this translation helpful? Give feedback.
-
I support this but wonder if it need to belong in this repo or roslyn? |
Beta Was this translation helpful? Give feedback.
-
I hope this will be implemented soon. This is highly important for proper Android support. |
Beta Was this translation helpful? Give feedback.
-
It would be good to support true local interface (or abstract types) implementations, as it's supported in Java.
Beta Was this translation helpful? Give feedback.
All reactions