Replies: 13 comments
-
This looks like a duplicate of #738. |
Beta Was this translation helpful? Give feedback.
-
I look on FSharp implementation, not Java. In generated assembly we must find public implementation of type with interfaces only with automatic generated name or inline name declaration, like this: public interface ISample
{
int Value { get; }
string ToValue();
}
public interface ISample2
{
int Add(int a);
}
public class Creator
{
public Created CreateSample(int value)
{
return new Created : ISample, ISample2 {
int Value => value;
string ToValue() => value.ToString();
override ToString() => this.ToValue();
int ISample2.Add(int a) => Value + a;
};
}
} Compiled to: public class Creator
{
public class Created : ISample, ISample2
{
private readonly int _value;
public Created(int value)
{
_value = value;
}
public int Value => _value;
public string ToValue() => _value.ToString();
public override ToString() => this.ToValue();
public int ISample2.Add(int a) => Value + a;
}
public Created CreateSample(int value) => new Created(value);
} |
Beta Was this translation helpful? Give feedback.
-
@ijsgaus I assume You mean compiled to: |
Beta Was this translation helpful? Give feedback.
-
I really don't like this idea that the inline declaration would somehow become promoted to a public type above the scope in which it is defined. It goes against pretty much all of the established scoping rules in C#. |
Beta Was this translation helpful? Give feedback.
-
This can be regulated on declaration level, but what type can be returned in case of multiple implemented interfaces? public Created CreateSample(int value)
{
return new internal Created : ISample, ISample2 {
int Value => value;
string ToValue() => value.ToString();
override ToString() => this.ToValue();
int ISample2.Add(int a) => Value + a;
};
} May be generate: public class Created : ISample, ISample2
{
private readonly int _value;
internal Created(int value)
{
_value = value;
}
public int Value => _value;
public string ToValue() => _value.ToString();
public override ToString() => this.ToValue();
public int ISample2.Add(int a) => Value + a;
} The scope of method go to scope of class, declared scope after new go to class constructor |
Beta Was this translation helpful? Give feedback.
-
@ijsgaus Why not limit the scope of the proposal to single interfaces only? |
Beta Was this translation helpful? Give feedback.
-
The proposed syntax looks too close to object initializers to be a realistic way of doing this. |
Beta Was this translation helpful? Give feedback.
-
Without header of initialization - object initializer, with concrete object initializer. This is object initializer with concrete object type - i don't see any significant differеnces. |
Beta Was this translation helpful? Give feedback.
-
@ijsgaus Could you please try to rephrase that? It's too terse to be understood easily. If I got that right, you propose to disambiguate based on the type name: |
Beta Was this translation helpful? Give feedback.
-
I also think that anonymity here wouldn't buy you much except for making it hard to parse for both human and machines. What is wrong with a local type? public class Creator
{
public ISample CreateSample(int value)
{
class Local : ISample { .. }
return new Local();
}
} |
Beta Was this translation helpful? Give feedback.
-
@alrz Do we have local type definable in method? And if we could define type in method itself, that's mean we could make anonymous object anyway Anonymity make it scoped to specific method if it not be shared with any other thing in the class. It make thing clear that this thing was not be used anywhere else so it don't even need a name to be referenced to it. It is the same concept as encapsulation |
Beta Was this translation helpful? Give feedback.
-
Same for local type declarations. (we don't have local types yet). |
Beta Was this translation helpful? Give feedback.
-
@alrz Anonymous local object is even narrow scope than that. With the scope of one line it would not be able to reused even in the same method. With local class definition it still ambiguous that this class would be used many times in the same method. While we just want it in the place we want to return public IDoSomething GetIDoSomething(bool yes)
{
if(yes)
return new IDoSomething(){ IDoSomething.DoSomething() => LogicForYes(); }
return new IDoSomething() { public DoSomething() => LogicForNo(); }
}
//// with local class
public IDoSomething GetIDoSomething(bool yes)
{
if(yes)
return new DoYes(); // We need to go look at DoYes below for the actual implementation
return new DoNo();
class DoYes : IDoSomething { public DoSomething() => LogicForYes(); }
class DoNo : IDoSomething { public DoSomething() => LogicForNo(); }
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Enable create interfaces and abstract types as this:
Beta Was this translation helpful? Give feedback.
All reactions