Skip to content

Fixes around array.Any translation - #1556

Merged
roji merged 1 commit into
npgsql:mainfrom
roji:AnyLikeOverColumn
Nov 3, 2020
Merged

roji merged 1 commit into
npgsql:mainfrom
roji:AnyLikeOverColumn

Conversation

@roji

@roji roji commented Nov 2, 2020

Copy link
Copy Markdown
Member

Closes #1554
Closes #1555

@roji
roji requested a review from YohDeadfall as a code owner November 2, 2020 20:07

@YohDeadfall YohDeadfall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, except style changes.

static readonly MethodInfo ILike2MethodInfo =
typeof(NpgsqlDbFunctionsExtensions)
static readonly MethodInfo ILike2MethodInfo
= typeof(NpgsqlDbFunctionsExtensions)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any reason to change the style? As I remember we leave assignment on the first line everywhere because of indentation. Right now, instead of 4 characters offset, for such lines it's 6.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Mainly for consistency with expression-bodied methods, where we put the => on the next line.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think putting => on the first line is better too for the same reason plus for sharing styling with .NET runtime.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The EF Core standard (and this is an EF Core provider) is to put it on the next line (usually... sometimes a bit inconsistent :))

Why is a 4 char offset better than 6 char?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Because of alignment with non-expression bodied methods. For example, prefixed commit messages (don't remember how that convention is called) is bad too due to how people read text. Monospace fonts were made to help human beings to scan files vertically without jumps vertically, but inconsistent indentation breaks that achievement.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I really don't see any reason for expression-bodied methods to start at the same offset as non-expression-bodied methods... They're different methods...

One good argument for putting it on the next line, is that it makes it immediately clear if it's a field initialization or a property with a getter only. This crucial difference can easily get lost if it's the arrow or assignment is far to the right on the first line.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not a hill I want die for. I said my opinion, but leaving it up to you.

{
if (expression is BinaryExpression binary)
{
(left, right) = (binary.Left, binary.Right);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While I use tuple assignments often, I don't think they looks well outside of expression bodied methods.

@roji roji Nov 2, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Embrace the Python style :) One line instead of two!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's leave two line instead of one, really hard to read. If there is a comma operator allowing to write a single line instead of three, I will be happy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Even the conservative guys in EF have agreed to me doing this... Seriously though - I know it's new syntax, it just requires getting used to...


if (methodCall.Method.Name == nameof(object.Equals) && methodCall.Arguments.Count == 1)
{
(left, right) = (methodCall.Object, methodCall.Arguments[0]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this method produces sorted results already, so why not to determine ordering for the static equality method and for binary expressions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't understand... The only job of TryMatchEquality is to provide the two side of the equality - there's no sorting/ordering involved...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I meant this:

                    var item = left == wherePredicate.Parameters[0]
                        ? right
                        : right == wherePredicate.Parameters[0]
                            ? left
                            : null;

It can be made simpler probably if it will combined with that method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TryMatchEquality is just about identifying equality and returning its parameters - it could be extracted and used elsewhere too. But if you think code can be really simplified here, do you want to submit a full code proposal?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not today, you caught me while I was heading to bed (:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No problem for it to come after merge too :)

Comment on lines +297 to +298
(left, right) = (null, null);
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If there would be a way to express such a thing in one line, it would be fine, but not now. In C++ which you dislike it's possible thankfully to comma operator, so here is an idea for a proposal for C# language team:

return (left = null, right = null, false); // only the last value used as the result

In that case you would be able to rewrite the whole method as an expression. Do you like it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The problem is that your expression is already legal in C#, and means something else:

public (string, string, string) Bar(ref string left, ref string right)
{
    return (left = null, right = null, "hello");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the left side expects less values than the right, it should work:

public string Brar(ref string left, ref string right) =>
    (left = null, right = null, "hello");

Otherwise, it would be cool to specify return keyword to solve the problem:

public string Brar(ref string left, ref string right) =>
    (left = null, right = null, return "hello");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IMHO that is super ambiguous with the tuple syntax... There's already a (a, b, c) which means something very clear. In your first example, you cannot know the type of that expression without looking at the method return type, which I think is a no-no in language design (even if we have target-typed assignments now)...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In case of expression based methods it could be fine since the type is nearby, and last example includes return.

@roji
roji merged commit 8a8bfa0 into npgsql:main Nov 3, 2020
@roji
roji deleted the AnyLikeOverColumn branch November 3, 2020 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translate array.Any(p => p == x)) as Contains Potential bug when using (I)LIKE with array columns

2 participants