add type alias as a type member #4247
-
This is a proposal to add the public interface IRecord<T>
{
public T Id { get; set; }
}
public interface ITable<TRecord, TId> where TRecord : class, IRecord<TId>
{
IEnumerable<TRecord> Records { get; }
}
public class Table<TRecord, TId> : ITable<TRecord, TId>
where TRecord : class, IRecord<TId>
{
private readonly IDictionary<TId, TRecord> _records;
public IEnumerable<TRecord> Records { get; }
} The above example define a table type and a record type, the record type define a generic argument which is the identity type. When we define the Now in order for The typedef solutionthe
the first syntax is type definition which is to define an alias for a type and it is allowed in any struct, class and interface, the second syntax is to declare an alias which later must be defined and this syntax is allowed in interface and abstract classes. The above example can now be written as the following: public interface IRecord
{
typedef IdType; // --> type alias declaration
IdType Id { get; }
}
public interface ITable<TRecord> where TRecord : class, IRecord
{
IEnumerable<TRecord> Records { get; }
}
public class Table<TRecord> : ITable<TRecord>
where TRecord : class, IRecord
{
private readonly IDictionary<TRecord.IdType, TRecord> _records;
public IEnumerable<TRecord> Records { get; }
}
public class PersonRecord : IRecord
{
typedef Guid IdType; // --> type alias definition
public IdType Id { get; }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This cannot be achieved under current CLR. Interface only contains vtable. |
Beta Was this translation helpful? Give feedback.
This cannot be achieved under current CLR. Interface only contains vtable.
With CLR modification, this exact idea falls into the scope of type classes.