Generics ======== ```Example from 1 languages: Java List<String> v = new ArrayList<String>(); v.add("test"); Integer i = v.get(0); // (type error) compilation-time error ``` ```Example from 1 languages: TypeScript function identity<T>(arg: T): T { return arg; } ``` ```Example from 1 languages: C# // Declare the generic class. public class GenericList<T> { public void Add(T input) { } } class TestGenericList { private class ExampleClass { } static void Main() { // Declare a list of type int. GenericList<int> list1 = new GenericList<int>(); list1.Add(1); // Declare a list of type string. GenericList<string> list2 = new GenericList<string>(); list2.Add(""); // Declare a list of type ExampleClass. GenericList<ExampleClass> list3 = new GenericList<ExampleClass>(); list3.Add(new ExampleClass()); } } ``` ```Example from 1 languages: Ada generic Max_Size : Natural; -- a generic formal value type Element_Type is private; -- a generic formal type; accepts any nonlimited type package Stacks is type Size_Type is range 0 .. Max_Size; type Stack is limited private; procedure Create (S : out Stack; Initial_Size : in Size_Type := Max_Size); procedure Push (Into : in out Stack; Element : in Element_Type); procedure Pop (From : in out Stack; Element : out Element_Type); Overflow : exception; Underflow : exception; private subtype Index_Type is Size_Type range 1 .. Max_Size; type Vector is array (Index_Type range <>) of Element_Type; type Stack (Allocated_Size : Size_Type := 0) is record Top : Index_Type; Storage : Vector (1 .. Allocated_Size); end record; end Stacks; ``` ```Example from 1 languages: Felix // generics fun g (x) => f (f x); println$ g 1, g "hello"; println$ _map f (1,"hello",2.0); ``` ```Example from 1 languages: Jule fn generic_function[T](s: []T) { // ... } ``` * Languages *with* Generics include Java, TypeScript, C#, Ada, Chapel, Felix, Jule * Languages *without* Generics include progsbase * View all concepts with or missing a *hasGenerics* measurement http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasGenerics&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22null%22%2C%22data%22%3A%22hasGenerics%22%2C%22origData%22%3A%22hasGenerics%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D missing http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasGenerics&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22!null%22%2C%22data%22%3A%22hasGenerics%22%2C%22origData%22%3A%22hasGenerics%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D with * Read more about Generics on the web: 1. https://en.wikipedia.org/wiki/Generic_programming 1. Built with Scroll v178.2.3