Inspectability #6965
Replies: 3 comments 5 replies
-
I'd be more interested in understanding the types of problems that a proposal like this seeks to resolve. Right now there is a serious problem with evolving expression trees alongside the languages and most language features since C# 6.0 can't be represented in expression trees, which would seemingly be a pretty big problem with the approach suggested here. |
Beta Was this translation helpful? Give feedback.
-
Thank you @HaloFour for your points about the evolution of LINQ expression trees alongside .NET languages, e.g., C#. As for the uses of such a proposal, I am finding inspectability, or lambda transparency, to be useful for a number of scenarios in a computational narratology project including: constraints, rules, and preconditions and effects on automated planning actions. In the sketches, in particular as developers are not supposed to manually extend public interface IInspectableMethod
{
public LambdaExpression Expression { get; }
public object? Invoke(object?[]? args);
} public interface IInspectableAction<T> : IInspectableMethod
{
public new Expression<Action<T>> Expression { get; }
public void Invoke(T arg);
} public interface IInspectableFunc<T, TResult> : IInspectableMethod
{
public new Expression<Func<T, TResult>> Expression { get; }
public TResult Invoke(T arg);
} Firstly, let us consider constraints. public interface IConstraint : INamed, IInspectableMethod
{
public string GetMessage(IFormatProvider? formatProvider);
public new bool Invoke(object?[]? args);
}
public interface IConstraint<T> : IConstraint, IInspectableFunc<T, bool> { } Here, constraints can be provided using named inspectable delegates and can provide messages for developers of use when a particular constraint is not satisfied. Secondly, let us consider rules. public interface IRule<TInput, TOutput> : INamed, IInspectableFunc<TInput, TOutput> { } Here, rules can be provided using named inspectable delegates. To clarify about constraints and rules, consider the following: public interface IReasoner
{
public IEnumerable<IConstraint<IQueryable<Statement>>> Constraints { get; }
public IEnumerable<IRule<IQueryable<Statement>, IQueryable<Statement>>> Rules { get; }
public IKnowledgebase Bind(IKnowledgebase source);
} where reasoners provide constraints and rules for knowledgebases, collections of statements. Thirdly, let us consider preconditions and effects on automated planning actions. public interface IOperator : INamespaceNamed
{
public IDomain Domain { get; }
public IEnumerable<IConstraint> Constraints { get; }
public bool CanInvoke(object?[]? args, [NotNullWhen(false)] out Exception? reason);
public IAction Invoke(object?[]? args);
public IEnumerable<ILambdaGenerator>? Preconditions { get; }
public IEnumerable<ILambdaGenerator>? Effects { get; }
}
public interface IAction
{
public IOperator Operator { get; }
public IReadOnlyList<object?> Arguments { get; }
public IEnumerable<IConstraint<IState>> Preconditions { get; }
public IEnumerable<IInspectableAction<IState>> Effects { get; }
} I'm finding these concepts to be useful in the aforementioned computational narratology project and, above, are indicated some of the AI-related uses for inspectable methods or metaprogramming capabilities. I hope that these examples show that this or another similar, related solution for .NET would be useful. |
Beta Was this translation helpful? Give feedback.
-
Why you don't use roslyn to "inspect" the delegates? It is much more powerful (and easier to use) than the old runtime Expression API.... |
Beta Was this translation helpful? Give feedback.
-
Hello. I would like to broach, for discussion, providing developers with the capability to declare methods as inspectable, or transparent, meaning that lambda expressions of the methods' implementations would be available, e.g., for metaprogramming purposes.
Brainstorming, we can consider extending
System.Reflection.MethodInfo
with:We can also consider the related topic of using metadata, attributes, to declare whether specific methods are intended to be inspectable, or transparent. For example:
We can also consider providing developers with useful inspectable delegates. In these regards, a
System.InspectableDelegate
might extendSystem.Delegate
to provide the functionality under discussion.We can consider providing the useful feature of strongly-typed inspectable delegates.
As envisioned, there would exist
System.InspectableAction<...>
andSystem.InspectableFunc<...>
inspectable delegates, resembling each of those existing delegates,System.Action<...>
andSystem.Func<...>
.If there is interest, I could write-up a lengthier proposal on these topics.
What do you think of these ideas?
Beta Was this translation helpful? Give feedback.
All reactions