Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ private protected override void SetGetter(Delegate? getter)
else if (getter is Func<object, T> typedGetter)
{
_typedGet = typedGetter;
_untypedGet = getter is Func<object, object?> untypedGet ? untypedGet : obj => typedGetter(obj);
_untypedGet = getter is Func<object, object?> untypedGet ? untypedGet : obj => _typedGet(obj);
}
else
{
Func<object, object?> untypedGet = (Func<object, object?>)getter;
_typedGet = (obj => (T)untypedGet(obj)!);
_typedGet = (obj => (T)_untypedGet!(obj)!);
_untypedGet = untypedGet;
Copy link
Member

@stephentoub stephentoub Jul 10, 2025

Choose a reason for hiding this comment

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

Nit: it doesn't actually matter, because the delegate won't be invoked yet, but could we switch these two lines so that _untypedGet is set before it's referenced? I find it a bit easier to reason about. (You might also be able to remove the !, I'm not sure)

Copy link
Member

Choose a reason for hiding this comment

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

Hypothetically, if I called the getter to store a copy of the existing delegate, but then set a new delegate via the setter, my copy would start pointing to the new behaviour. I think that makes a subtle difference that could regress code that is heavy on contract customization.

Copy link
Member

Choose a reason for hiding this comment

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

Hypothetically, if I called the getter to store a copy of the existing delegate, but then set a new delegate via the setter, my copy would start pointing to the new behaviour. I think that makes a subtle difference that could regress code that is heavy on contract customization.

This is in reference to the PR, not to my nit, yes?

Copy link
Member

Choose a reason for hiding this comment

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

Yep

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this mean I should close this?

I don't mind either way. This is a drop in the bucket. I'm looking at a relatively big app (40 MB) where 15% of the app is System.Text.Json because of how many generics S.T.Json will create for every type used with Json. This looked like something we could do something about, the rest I don't know how to fix.

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean I should close this?

Yeah I think so. It doesn't change things much either way so I would err towards preserving the current behaviour.

}
}
Expand All @@ -69,12 +69,12 @@ private protected override void SetSetter(Delegate? setter)
else if (setter is Action<object, T> typedSetter)
{
_typedSet = typedSetter;
_untypedSet = setter is Action<object, object?> untypedSet ? untypedSet : (obj, value) => typedSetter(obj, (T)value!);
_untypedSet = setter is Action<object, object?> untypedSet ? untypedSet : (obj, value) => _typedSet(obj, (T)value!);
}
else
{
Action<object, object?> untypedSet = (Action<object, object?>)setter;
_typedSet = ((obj, value) => untypedSet(obj, value));
_typedSet = ((obj, value) => _untypedSet!(obj, value));
_untypedSet = untypedSet;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

}
}
Expand All @@ -100,12 +100,12 @@ private protected override void SetShouldSerialize(Delegate? predicate)
else if (predicate is Func<object, T?, bool> typedPredicate)
{
_shouldSerializeTyped = typedPredicate;
_shouldSerialize = typedPredicate is Func<object, object?, bool> untypedPredicate ? untypedPredicate : (obj, value) => typedPredicate(obj, (T?)value);
_shouldSerialize = typedPredicate is Func<object, object?, bool> untypedPredicate ? untypedPredicate : (obj, value) => _shouldSerializeTyped(obj, (T?)value);
}
else
{
Func<object, object?, bool> untypedPredicate = (Func<object, object?, bool>)predicate;
_shouldSerializeTyped = (obj, value) => untypedPredicate(obj, value);
_shouldSerializeTyped = (obj, value) => _shouldSerialize!(obj, value);
_shouldSerialize = untypedPredicate;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ private protected override void SetCreateObject(Delegate? createObject)
else if (createObject is Func<T> typedDelegate)
{
typedCreateObject = typedDelegate;
untypedCreateObject = createObject is Func<object> untypedDelegate ? untypedDelegate : () => typedDelegate()!;
untypedCreateObject = createObject is Func<object> untypedDelegate ? untypedDelegate : () => _typedCreateObject!()!;
}
else
{
Debug.Assert(createObject is Func<object>);
untypedCreateObject = (Func<object>)createObject;
typedCreateObject = () => (T)untypedCreateObject();
typedCreateObject = () => (T)_createObject!();
}

_createObject = untypedCreateObject;
Expand Down
Loading