Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Inheritance #52

Open
tripleo1 opened this issue Dec 31, 2021 · 1 comment
Open

Data Inheritance #52

tripleo1 opened this issue Dec 31, 2021 · 1 comment

Comments

@tripleo1
Copy link

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)?

@c3d
Copy link
Owner

c3d commented Dec 31, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants