Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ITypedValueExpressionFactory<T> interface for our typed value expressions #3223

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

ArcturusZhang
Copy link
Member

@ArcturusZhang ArcturusZhang commented Apr 24, 2024

Fixes #3312

Majority of our typed value expressions will have the ctor taking a ValueExpression parameter, like this:

public record XExpression(ValueExpression Untyped) : TypedValueExpression(typeof(X), Untyped)

and we cannot add a constraint using generic syntax, this cannot compile:

public static MethodBodyStatement Declare<T>(string name, T value, out T variable) where T : TypedValueExpression, new(ValueExpression)

to have a generic method to catch all of the cases we could possibly support, we could use this newly introduced interface as the constraint:

public static MethodBodyStatement Declare<T>(string name, T value, out T variable) where T : TypedValueExpression, ITypedValueExpressionFactory<T>

where ITypedValueExpressionFactory<T> works similarly as the made-up constraint above new(ValueExpression).

I believe this should simplify our code, and make it could automatically extend when a new expression is added.

@ArcturusZhang ArcturusZhang changed the title Add an ITypedValueExpressionFactory<T> interface for our typed value expressions http-client-csharp: Add an ITypedValueExpressionFactory<T> interface for our typed value expressions Apr 25, 2024
@ArcturusZhang
Copy link
Member Author

@m-nash @ShivangiReja @jorgerangel-msft could you please have a review? This is ready to review now.

@azure-sdk
Copy link
Collaborator

API change check

APIView has identified API level changes in this PR and created following API reviews.

Microsoft.Generator.CSharp

@bterlson bterlson changed the title http-client-csharp: Add an ITypedValueExpressionFactory<T> interface for our typed value expressions Add an ITypedValueExpressionFactory<T> interface for our typed value expressions May 10, 2024
@bterlson bterlson added emitter:client:csharp feature New feature or request labels May 10, 2024
@@ -36,5 +36,8 @@ public static BinaryDataExpression FromObjectAsJson(ValueExpression data)

public static BinaryDataExpression FromString(ValueExpression data)
=> new(InvokeStatic(nameof(BinaryData.FromString), data));

static BinaryDataExpression ITypedValueExpressionFactory<BinaryDataExpression>.Create(ValueExpression untyped)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we don't add explicit cast operators here instead?

Also should this fail if the ValueExpression given is not a BinaryDataExpression? How would we check that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is supposed to wrap a untyped expression to this specific typed expression, works as the same as its ctor.
I use this to work as a constraint of "it has a ctor with parameter of valueexpression"

Also should this fail if the ValueExpression given is not a BinaryDataExpression? How would we check that?

No, our typed expressions do not work in this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the case "it has a ctor with parameter of valueexpression", why not just let the user call new BoolExpression(valueExpression) vs BoolExpression.Create(valueExpression). in dotnet land the new ctor syntax is much more discoverable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole point is not for how our use would construct this, despite this may have affects on that part.
This is trying to introduce a "generic constraint" so that we could have:

MethodBodyStatement Declare<T>(string name, T expression, out T variable)

as I have written in the description.
Adding this does not remove the ctor though. And I kind of do not want users (plugin writers) to call this method therefore I always explicitly implement those methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[http-client-csharp] Strong typed snippet methods
5 participants