Generics. Part 3

Generic types

In addition to generic functions you can declare your own generic types in Jazz.These are custom structures that can work with any type, in a similar way to Array and Map.

This sections shows you how to write a generic type Point . Point usually means two values x and y.You can use this type in math calculation.

Here’s how to write a nongeneric version of a point in this case for a point of int values:

struct Point {
var x: int;
var y: int;
init(x: int,y: int) {
this.x = x;
this.y = y;
}
mut fun setX(x: int) {this.x = x;}
mut fun setY(x: int) {this.y = y;}
}

Note how the generic version of Point is essentially the same as the nongeneric version, but with a type parameter called T instead of an actual type of int. This type parameter is written within a pair of angle brackets (<T>) immediately after the structure’s name.

struct Point<T> {
var x: T;
var y: T;
}