3 ms·
I see two use cases, Swift is a precompiled language unlike C# or Java so a call to a method through a protocol (an interface) can not be inlined. Let suppose y
by _old_dude_ 8y ago
I see two use cases, Swift is a precompiled language unlike C# or Java so a call to a method through a protocol (an interface) can not be inlined.
Let suppose you have an interface IList and an IEnumerator,
interface IList<T> {
IEnumerable<T> iterator();
}
it means that despite the fact that you may know the exact implementation of the IList, you have no idea of the exact type of the method iterator() at compile time because it's hidden behind an interface.
Enter the existential types,
interface IList<T> {
type IEnumerator<T> Iterator; // this is a type declaration
Iterator iterator();
}
now if you have an implementation of IList, you will have
class FunnyList<T>: IList<T> {
type FunnyListEnumerator<T> Iterator;
Iterator iterator();
}
so at compile time, when you use a FunnyList, the compiler knows the exact type of the method iterator().
The other usual use case, which is a restricted version, is to be able to specified the variance at use site, like the wildcards in Java (List<? extends Foo>) which is a way to specify that you don't want a method to be parameterized but you want a type of a parameter to be covariant
void foo<T:Foo>(List<T> list) // the method is parameterized
void foo(List<some Foo> list) // the type is parameterized
C# doesn't have variance at use site, only at definition site with 'in' and 'out', Kotlin has both. Scala and C++ with type name form.
- pjmlp 8y agoC# and Java also have AOT compilers available. In fact that is their execution model in UWP, Unity (Consoles, iOS, Xamarin (iOS), Android and some embedded deployments.