TYPES
Generics
4 min
shift has strong generic support a generic type allows a user to define a template/container for a set of behavior that will operate based on a passed in type these passed in types are called type arguments the type system is fully generic aware so different instances of the generic type will be understood as different types list\<t> in c# is probably one of the most common examples in c# it is a list of some type it holds an indefinite number of items in a specific order, each of those items being guarenteed to be whatever type t is this saves us the programmer time, because we do not need to program a list type manually for every thing we may want to add to a list furthermore it's generally more safe than the non generic version such as list, which is just a list of anything rarely do you want to be able to construct a list of literally anything and work with that by specifying the type, you can convey intent and get the type safety benefits that the compiler can provide type arguments when declaring a type, you can specify a type's arguments at the same time service example\<targ1, targ2> { } when specifying the type's name you declare that there are type arguments via the < > arguments this takes in an array of identifiers that correspond to each type arguement this is the closest mechanism that shift has to what would be templates or containers in other languages targ1 and targ2 are both placeholders and references to what type will be specific at declaration time for an example variable here's what specifying this would look like var ex = new example\<int, string>(); example\<bool,bool> ex2; service foo { example\<int,int> buildexample() { return new example\<int,int>(); } example\<int,bool> genericonbothside(example\<bool, bool> exin) { var translated = translatorfunction(exin);//produces a example\<int,bool> return translated; } } the only new concept above is when referring a generic type, you have to supply the arguments on the definition side of the example service the type has a type argument as the placeholder this is often referred to as an open generic the generic's type is open to definition still, we have placeholders so it's not concrete yet all of the code that would use our service example must fully specify all of the type arguments thus "closing" the type referred to as a closed generic the most important thing to remember, is anywhere a type can be supplied a generic type is a valid option to pass variance
