From 3f59d8582cc86d0512dc562e5bf51d3f045444d8 Mon Sep 17 00:00:00 2001 From: Vlad Faust Date: Sun, 30 Aug 2020 22:24:49 +0300 Subject: [PATCH] Add another Complex Type example --- ...020-08-20-the-onyx-programming-language.md | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/posts/2020-08-20-the-onyx-programming-language.md b/posts/2020-08-20-the-onyx-programming-language.md index 744ce82..12f7c35 100644 --- a/posts/2020-08-20-the-onyx-programming-language.md +++ b/posts/2020-08-20-the-onyx-programming-language.md @@ -761,7 +761,25 @@ end ``` Luckily, no changes have to be made to the `do_draw()` function, because the compiler treats the argument solely as `Drawable2D`, and calling `draw()` on it always calls `Drawable2D:draw()`! -Again, changing the type from outside would not break a callee. +Thus, changing the type from outside would not break a callee. + +The `Point` type now has `draw2d` and `draw3d` methods. +Of course, calling `Point:draw()` would not work anymore, because we've moved the implementation. +But it is still possible to restrict a `Point` to the required trait and call `draw()` on it. +For example: + +```text +final p = Point() + +# p.draw() # Panic! Ambiguity + +p.draw2d() # OK, draw 2D +p~Drawable2D.draw() # OK, draw 2D + +p.draw3d() # OK, draw 3D +p~Drawable3D.draw() # OK, draw 3D +``` + Incapsulation at its finest! ### More Highlights