Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why don't you simply replace <code>T1</code> and <code>T2</code> with a single generic type parameter? Then the second parameters type needn't be inferred - which it doesn't have to be anyhow since it must be identical to that of the first parameter.</p> <p>That would look like this:</p> <h2>Variant 1</h2> <pre><code>public KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt; Marry&lt;T&gt;( Expression&lt;Func&lt;Task, T&gt;&gt; key, T value) { return new KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt;(key, value); } </code></pre> <p>I don't recommend this, but if you want the type-inference to "greedily" determine the type of the expression based solely on the first parameter (so that you get the error "cannot convert from 'string' to 'DateTime?'), you can curry the method:</p> <h2>Variant 2</h2> <pre><code>public static Func&lt;T, KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt; &gt; Marry&lt;T&gt;( Expression&lt;Func&lt;Task, T&gt;&gt; key) { return (value=&gt;new KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt;(key, value)); } //usage: Marry(t =&gt; t.NULLABLEDATETIME)("test"); //error: Delegate 'System.Func&lt;System.DateTime?,System.Collections.Generic.KeyValuePair&lt;System.Linq.Expressions.Expression&lt;System.Func&lt;UserQuery.Task,System.DateTime?&gt;&gt;,System.DateTime?&gt;&gt;' has some invalid arguments // - Argument 1: cannot convert from 'string' to 'System.DateTime?' </code></pre> <p>That error message essentially covers it. However, the API isn't too readable and the usage of heavily nested generic types makes things... less readable. A longer variant with essentially the same behavior but shorter error messages could look as follows:</p> <h2>Variant 3</h2> <pre><code>public class MarriagePartner&lt;T&gt; { readonly Expression&lt;Func&lt;Task, T&gt;&gt; key; public MarriagePartner(Expression&lt;Func&lt;Task, T&gt;&gt; key) { this.key = key; } public KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt; With(T value) { return new KeyValuePair&lt;Expression&lt;Func&lt;Task, T&gt;&gt;, T&gt;(key, value); } } public static MarriagePartner&lt;T&gt; Marry2&lt;T&gt;(Expression&lt;Func&lt;Task, T&gt;&gt; key) { return new MarriagePartner&lt;T&gt;(key); } //Usage: Marry2(t =&gt; t.NULLABLEDATETIME).With("test"); //error: The best overloaded method match for 'UserQuery.MarriagePartner&lt;System.DateTime?&gt;.With(System.DateTime?)' has some invalid arguments // - Argument 1: cannot convert from 'string' to 'System.DateTime?' </code></pre> <p>That error message is a little more specific. I think Variant 1's error message suffices and it's what I'd use (KISS and all) - but if you <em>Really</em> want that casting error message, Variant 3 has the most understandable (if longer) implementation and most friendly error message.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload