An interface in Gambas is an interface between a class and the interpreter. Interfaces are currently only available for native classes - classes that are not written in gambas. In addition, only Gambas developers can use interfaces and very few classes implement interfaces.
The description of the effects when using interfaces should be shown with the example of the class Matrix, because the class Matrix (gb. gsl) internally uses both the _convert interface and the _operator interface.
Private Sub CreateAndShowMatrix() mMatrix = New Matrix(Columns.Value, Rows.Value, True) ' Spalten, Zeilen mMatrix = [[1, 1, 1, 7 + 2i, 8], [2, 2, 2, 4, 5], [1, 0, 3, 9, 5], [-1, -2i, -3, 4, 5 + 1i]] *** ' Matrix-Elemente in der TableView anzeigen MatrixToTableView(mMatrix) End ' CreateMatrix()
The _convert interface allows you to perform automatic data type conversions. A class that implements the _convert interface accepts certain data types as convertible and provides a method for converting suitable data. In this case, the array of arrays (in the background) is converted to a matrix and the assignment (3*) succeeds. The _operator interface specifies the behavior of objects when operators are applied to them. Whenever two objects (or an object and a value of native data type) are linked to an operator, the _operator interface is in play.
The Gambas user is only interested in this fact, that you can write matrix = [[z1, z2],[z3, z4]] and the interpreter recognizes this as matrix in certain assignment contexts.
The class Matrix (gb. gsl) also partially implements the _operator interface, which controls the applicability of certain operators to matrices. This is sure to succeed
Public Sub btnMatrixMatrixProdukt_Click() Dim mMatrix As New Matrix(3, 3, False) ' Nur reelle Koeffizienten Dim mResult As Matrix mMatrix = [[1, 5, 6], [4, -3.3, -9], [7, 2, 8.8]] Print mMatrix.ToString(True) ' True → Lokale Spracheinstellung aktiv mMatrix = 2 * mMatrix ' Skalar-Matrix-Produkt Print mMatrix.ToString(True) mResult = mMatrix * mMatrix.Trans() ' Matrix-Matrix-Produkt Print mResult.ToString(True) End
The display in the console of the Gambas IDE shows these results:
[[1 5 6][4 -3,3 -9][7 2 8,8]] [[2 10 12][8 -6,6 -18][14 4 17,6]] [[248 -266 279,2][-266 431,56 -231,2][279,2 -231,2 521,76]]
There are two other specialized interfaces - Paint and PaintMatrix - that have something to do with drawing operations on selected classes.