Open tripleo1 opened 2 years ago
The first example is the interface for the type:
type colored_text like text with
foreground : color
background : color
What this means is that when you use the colored_text
type, you are allowed to use two fields called foreground
and background
, each having the color
type. Therefore, the following code is valid:
CT : colored_text
if CT.foreground = CT.background then print "Will be hard to read"
This is irrespective of how the fields foreground
and background
are actually implemented (i.e. getter/setters or data fields or whatever).
The second example is an implementation of that interface using data inheritance, which is very much like inheritance in C++ where you have the original data with the new data added after it:
type colored_text is text with
background : color
foreground : color
What this means is that the colored_text
type is implemented as a text
base value followed by two background
and foreground
data fields.
As the next section explains, another possible implementation could have a totally different internal structure:
type colored_text is matching colored_text(fg : rgb_color; bg: rgb_color; text_data: byte_stream)
Of course, if you decide that this is what the implementation looks like, then you need to explicitly fulfill the interface contract. In particular, since you need to be able to convert to text
to fulfill the like text
part of the interface, you need to provide a conversion function, for example (assuming there is an explicit conversion from text_data
to text
):
T: colored_text as text is text(T.text_data)
That means you can now use a colored_text
like a text
:
foo T:text is print "foo ", T
bar C: colored_text is foo C // OK: can pass C using implicit conversion above
http://c3d.github.io/xl/#data-inheritance
I don't understand the difference between the two examples presented.
Also, what is data inheritance (or am I looking too deep)?