From 608dafaf85b0716693274a83ab720e9f9d7fd991 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sun, 24 May 2020 19:13:09 +0200 Subject: [PATCH] complete --- README.md | 2 +- lessons/Properties.md | 61 +++++++++++++++++++++++++++++++++++++++++++ rust/Inheritance.md | 8 +----- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ffc8ec9..246a251 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Lessons: - [constructors](./lessons/Constructors.md) - [encapsulation](./lessons/Encapsulation.md) - [inheritance](./lessons/Inheritance.md) -- [properties](./lessons/Properties.md) - +- [properties](./lessons/Properties.md) - [polymorphism](./lessons/Polymorphism.md) Work in progress: diff --git a/lessons/Properties.md b/lessons/Properties.md index e69de29..df868a8 100644 --- a/lessons/Properties.md +++ b/lessons/Properties.md @@ -0,0 +1,61 @@ +# Properties + +Properties are a way to restrict read/write access to a field and to add +additional verifications, computations or function calls to accessing a field. + +To define a property you need one private field that stores your data and the +getter/setter for it. If you don't want it to be readable or writable, you can +leave out either of the accessor functions. + +None of the languages featured here have a full implementation of properties. +Languages like c# and Delphi do though. + +Delphi has the simplest syntax for properties I've seen so far. + +```pas + // this property will read from the field FFoo and write using the function set_foo + property Foo read FFoo write set_foo; +``` + +C# is a bit more complicated, but still better than the languages here + +```cs + public string Foo + { + get => _foo; + set { + _foo = do_something(value); + } + } +``` + +## Typescript + +Typescript allows a somewhat property implementation. In the class itself you +define getter and setter functions using the get and set keywords + +```ts +public get foo(): string { + return this._foo; +} + +public set foo(val: string): void { + this._foo = do_something(val); +} +``` + +From the outside, the property can be read and written like in c# and Delphi, +just like working with a variable. + +## C++, Java and Rust + +Those languages don't provide a property syntax at all, instead you have to +create public functions like set_foo and get_foo to replace them. + +## Additional note to rust + +Properties don't exist in rust either. But replacing them with getters and +setters is also discouraged. Quoting from a reddit post +`Expose behavior, not state`. That means you should present functions to outside +viewers and not your plain data. An object should be able to do all its intended +functions itself without having an outside observer read or write its data. diff --git a/rust/Inheritance.md b/rust/Inheritance.md index 512a6e5..608ec1c 100644 --- a/rust/Inheritance.md +++ b/rust/Inheritance.md @@ -1,12 +1,6 @@ -# Inheritance, Properties and Polymorphism +# Inheritance and Polymorphism Rust does not allow extending classes. Instead the base class is stored as a field in the extending class. Polymorphism can then be achieved by passing the field instead of the whole object to a function. Although casting back to the advanced class or overriding functions is not possible. - -Properties don't exist either, like in java. But replacing them with getters and -setters is also discouraged. Quoting from a reddit post 'Expose behavior, not -state'. That means you should present functions to outside viewers and not your -plain data. An object should be able to do all its intended functions itself -without having an outside observer read its data.