Generics#

Generic programming is a way of defining that something is applicable to a variety of potential types, without having to know these types before hand. The classic example would be a collection such as a list, which can be trivially customised to contain any type of data elements. Generics allow a Vala programmer to have these customisations done automatically.

Some of these are possible, which?

  • class Wrapper < T > : Object { … }

  • new Wrapper < Object > ( ) ;

  • BUG: class StringWrapper : Wrapper < string > ( ) { … }

  • FAIL: class WrapperWrapper < Wrapper < T > > : Object { … }

  • FAIL: new WrapperWrapper < Wrapper < Object > > ( ) ;

  • interface IWrapper < T > { … }

  • class ImpWrapper1 < T > : Object, IWrapper < T > { … }

  • BUG: class ImpWrapper2 : Object, IWrapper < string > { … }

Generics declaration#

Some of the syntax could be best placed in the class/interface/struct pages, but that might overcomplicate them…

In class declaration - In struct declaration - In interface declaration - In base class declaration - In implemented interfaces declaration - In prerequesite class/interface declaration.

Declaration with type parameters introduces new types into that scope, identified by names given in declaration, e.g. T.

qualified-type-name-with-generic:

  • qualified-class-name-with-generic

  • qualified-interface-name-with-generic

  • qualified-struct-name-with-generic

qualified-class-name-with-generic:

  • [ qualified-namespace-name . ] class-name type-parameters

qualified-interface-name-with-generic:

  • [ qualified-namespace-name . ] interface-name type-parameters

qualified-struct-name-with-generic:

  • [ qualified-namespace-name . ] struct-name type-parameters

type-parameters:

  • < generic-clause >

generic-clause:

  • type-identifier [ , generic-clause ]

  • qualified-type-name [ , generic-clause ]

type-identifier:

  • identifier

type-identifier will be the type-name for the parameterised type.

Deal is: in the class/interface/struct sections, replace qualified--name with qualified--name-with-generic.

Instantiation#

Only explanation here? Syntax should go with variable declaration statement?

When using generic for a type-name, only type-names can be used as type-parameters, not identifiers. NB. in scope of generic class, T etc. is a real type-name.

Examples#

Demonstrating…

 1using GLib;
 2
 3public interface With < T > {
 4        public abstract void sett(T t);
 5        public abstract T gett();
 6}
 7
 8
 9public class One : Object, With < int > {
10        public int t;
11
12        public void sett(int t) {
13                this.t = t;
14        }
15        public int gett() {
16                return t;
17        }
18}
19
20public class Two < T, U > : Object, With < T > {
21        public T t;
22
23        public void sett(T t) {
24                this.t = t;
25        }
26        public T gett() {
27                return t;
28        }
29
30        public U u;
31}
32
33public class Test : GLib.Object {
34
35        public static void main(string[] args) {
36                var o = new One ();
37                o.sett(5);
38                stdout.printf("%d
39", o.t);
40
41                var t = new Two < int, double? > ();
42                t.sett(5);
43                stdout.printf("%d
44", t.t);
45
46                t.u = 5.0f;
47                stdout.printf("%f
48", t.u);
49        }
50}