Заглавная страница Избранные статьи Случайная статья Познавательные статьи Новые добавления Обратная связь FAQ Написать работу КАТЕГОРИИ: АрхеологияБиология Генетика География Информатика История Логика Маркетинг Математика Менеджмент Механика Педагогика Религия Социология Технологии Физика Философия Финансы Химия Экология ТОП 10 на сайте Приготовление дезинфицирующих растворов различной концентрацииТехника нижней прямой подачи мяча. Франко-прусская война (причины и последствия) Организация работы процедурного кабинета Смысловое и механическое запоминание, их место и роль в усвоении знаний Коммуникативные барьеры и пути их преодоления Обработка изделий медицинского назначения многократного применения Образцы текста публицистического стиля Четыре типа изменения баланса Задачи с ответами для Всероссийской олимпиады по праву Мы поможем в написании ваших работ! ЗНАЕТЕ ЛИ ВЫ?
Влияние общества на человека
Приготовление дезинфицирующих растворов различной концентрации Практические работы по географии для 6 класса Организация работы процедурного кабинета Изменения в неживой природе осенью Уборка процедурного кабинета Сольфеджио. Все правила по сольфеджио Балочные системы. Определение реакций опор и моментов защемления |
Explicit parameter type inferencesСодержание книги
Поиск на нашем сайте
An explicit parameter type inference is made from an expression E to a type T in the following way: · If E is an explicitly typed anonymous function with parameter types U1…Uk and T is a delegate type or expression tree type with parameter types V1…Vk then for each Ui an exact inference (§7.5.2.8) is made from Ui to the corresponding Vi. Exact inferences An exact inference from a type U to a type V is made as follows: · If V is one of the unfixed Xi then U is added to the set of exact bounds for Xi. · Otherwise, sets V1…Vk and U1…Uk are determined by checking if any of the following cases apply: · V is an array type V1[…] and U is an array type U1[…] of the same rank · V is the type V1? and U is the type U1? · V is a constructed type C<V1…Vk> and U is a constructed type C<U1…Uk> If any of these cases apply then an exact inference is made from each Ui to the corresponding Vi. · Otherwise no inferences are made. Lower-bound inferences A lower-bound inference from a type U to a type V is made as follows: · If V is one of the unfixed Xi then U is added to the set of lower bounds for Xi. · Otherwise, if V is the type V1? and U is the type U1? then a lower bound inference is made from U1 to V1. · Otherwise, sets U1…Uk and V1…Vk are determined by checking if any of the following cases apply: · V is an array type V1[…]and U is an array type U1[…] (or a type parameter whose effective base type is U1[…]) of the same rank · V is one of IEnumerable<V1>, ICollection<V1> or IList<V1> and U is a one-dimensional array type U1[](or a type parameter whose effective base type is U1[]) · V is a constructed class, struct, interface or delegate type C<V1…Vk> and there is a unique type C<U1…Uk> such that U (or, if U is a type parameter, its effective base class or any member of its effective interface set) is identical to, inherits from (directly or indirectly), or implements (directly or indirectly) C<U1…Uk>. (The “uniqueness” restriction means that in the case interface C<T>{} class U: C<X>, C<Y>{}, then no inference is made when inferring from U to C<T> because U1 could be X or Y.) If any of these cases apply then an inference is made from each Ui to the corresponding Vi as follows: · If Ui is not known to be a reference type then an exact inference is made · Otherwise, if U is an array type then a lower-bound inference is made · Otherwise, if V is C<V1…Vk> then inference depends on the i-th type parameter of C: · If it is covariant then a lower-bound inference is made. · If it is contravariant then an upper-bound inference is made. · If it is invariant then an exact inference is made. · Otherwise, no inferences are made. Upper-bound inferences An upper-bound inference from a type U to a type V is made as follows: · If V is one of the unfixed Xi then U is added to the set of upper bounds for Xi. · Otherwise, sets V1…Vk and U1…Uk are determined by checking if any of the following cases apply: · U is an array type U1[…]and V is an array type V1[…]of the same rank · U is one of IEnumerable<Ue>, ICollection<Ue> or IList<Ue> and V is a one-dimensional array type Ve[] · U is the type U1? and V is the type V1? · U is constructed class, struct, interface or delegate type C<U1…Uk> and V is a class, struct, interface or delegate type which is identical to, inherits from (directly or indirectly), or implements (directly or indirectly) a unique type C<V1…Vk> (The “uniqueness” restriction means that if we have interface C<T>{} class V<Z>: C<X<Z>>, C<Y<Z>>{}, then no inference is made when inferring from C<U1> to V<Q>. Inferences are not made from U1 to either X<Q> or Y<Q>.) If any of these cases apply then an inference is made from each Ui to the corresponding Vi as follows: · If Ui is not known to be a reference type then an exact inference is made · Otherwise, if V is an array type then an upper-bound inference is made · Otherwise, if U is C<U1…Uk> then inference depends on the i-th type parameter of C: · If it is covariant then an upper-bound inference is made. · If it is contravariant then a lower-bound inference is made. · If it is invariant then an exact inference is made. · Otherwise, no inferences are made. Fixing An unfixed type variable Xi with a set of bounds is fixed as follows: · The set of candidate types Uj starts out as the set of all types in the set of bounds for Xi. · We then examine each bound for Xi in turn: For each exact bound U of Xi all types Uj which are not identical to U are removed from the candidate set. For each lower bound U of Xi all types Uj to which there is not an implicit conversion from U are removed from the candidate set. For each upper bound U of Xi all types Uj from which there is not an implicit conversion to U are removed from the candidate set. · If among the remaining candidate types Uj there is a unique type V from which there is an implicit conversion to all the other candidate types, then Xi is fixed to V. · Otherwise, type inference fails. Inferred return type The inferred return type of an anonymous function F is used during type inference and overload resolution. The inferred return type can only be determined for an anonymous function where all parameter types are known, either because they are explicitly given, provided through an anonymous function conversion or inferred during type inference on an enclosing generic method invocation. The inferred result type is determined as follows: · If the body of F is an expression that has a type, then the inferred result type of F is the type of that expression. · If the body of F is a block and the set of expressions in the block’s return statements has a best common type T (§7.5.2.14), then the inferred result type of F is T. · Otherwise, a result type cannot be inferred for F. The inferred return type is determined as follows: · If F is async and the body of F is either an expression classified as nothing (§7.1), or a statement block where no return statements have expressions, the inferred return type is System.Threading.Tasks.Task · If F is async and has an inferred result type T, the inferred return type is System.Threading.Tasks.Task<T>. · If F is non-async and has an inferred result type T, the inferred return type is T. · Otherwise a return type cannot be inferred for F. As an example of type inference involving anonymous functions, consider the Select extension method declared in the System.Linq.Enumerable class: namespace System.Linq Assuming the System.Linq namespace was imported with a using clause, and given a class Customer with a Name property of type string, the Select method can be used to select the names of a list of customers: List<Customer> customers = GetCustomerList(); The extension method invocation (§7.6.5.2) of Select is processed by rewriting the invocation to a static method invocation: IEnumerable<string> names = Enumerable.Select(customers, c => c.Name); Since type arguments were not explicitly specified, type inference is used to infer the type arguments. First, the customers argument is related to the source parameter, inferring T to be Customer. Then, using the anonymous function type inference process described above, c is given type Customer, and the expression c.Name is related to the return type of the selector parameter, inferring S to be string. Thus, the invocation is equivalent to Sequence.Select<Customer,string>(customers, (Customer c) => c.Name) and the result is of type IEnumerable<string>. The following example demonstrates how anonymous function type inference allows type information to “flow” between arguments in a generic method invocation. Given the method: static Z F<X,Y,Z>(X value, Func<X,Y> f1, Func<Y,Z> f2) { Type inference for the invocation: double seconds = F("1:15:30", s => TimeSpan.Parse(s), t => t.TotalSeconds); proceeds as follows: First, the argument "1:15:30" is related to the value parameter, inferring X to be string. Then, the parameter of the first anonymous function, s, is given the inferred type string, and the expression TimeSpan.Parse(s) is related to the return type of f1, inferring Y to be System.TimeSpan. Finally, the parameter of the second anonymous function, t, is given the inferred type System.TimeSpan, and the expression t.TotalSeconds is related to the return type of f2, inferring Z to be double. Thus, the result of the invocation is of type double.
|
||||
Последнее изменение этой страницы: 2016-08-10; просмотров: 289; Нарушение авторского права страницы; Мы поможем в написании вашей работы! infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.116.24.238 (0.009 с.) |