-
Notifications
You must be signed in to change notification settings - Fork 4.1k
update: improve Classes page #4786
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
base: master
Are you sure you want to change the base?
Conversation
b4d1916
to
85c4e7f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main question is: what are the things we want the reader to know, after they've finished reading the updated "Classes" page? Is it basic syntax for classes, details about constructors, details about initialization, smth else? Right now the section is a bit all over the place, I feel.
In other words, when we set to improve the page, what did we want to achieve?
docs/topics/classes.md
Outdated
and some other things), and the class body surrounded by curly braces. Both the header and the body are optional; if the | ||
class has no body, the curly braces can be omitted. | ||
The class declaration consists of: | ||
* **Class header**, including: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it "including" or "including but not limited to"? Because class header also includes, for example, supertype list.
docs/topics/classes.md
Outdated
|
||
```kotlin | ||
class Person constructor(firstName: String) { /*...*/ } | ||
// Class with only a name and no body | ||
class Person |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better example would be a class with a primary constructor and some properties, but no body
docs/topics/classes.md
Outdated
If the primary constructor does not have any annotations or visibility modifiers, the `constructor` keyword can be omitted: | ||
## Constructors and initializer blocks | ||
|
||
A class in Kotlin has a [_primary constructor_](#primary-constructor) and, optionally, one or more [_secondary constructors_](#secondary-constructors). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we correctly said primary ctor is optional. Here I'm reading as if it is mandatory. I would suggest trying to find a better phrasing for the idea "primary ctor is optional, but we believe in most cases your classes should have one".
docs/topics/classes.md
Outdated
} | ||
``` | ||
|
||
See classes in action. Considering the same code example, if you [create an object](#creating-objects-of-classes) of the `Person` class by passing values for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"See classes in action"?
docs/topics/classes.md
Outdated
) { /*...*/ } | ||
``` | ||
|
||
Using the trailing comma example, if you [create an object](#creating-objects-of-classes) of the `Person` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not fully get what we say via "using the trailing comma example" here
docs/topics/classes.md
Outdated
> Kotlin does not have a `new` keyword. | ||
> | ||
{style="note"} | ||
Typically, you create objects within the `main()` function because it serves as the entry point of your Kotlin program. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement is not true, you create objects whenever you need to do that, and not only within the main
function
docs/topics/classes.md
Outdated
You don't need to annotate abstract classes or functions with the `open` keyword because they are implicitly | ||
open and designed to be inherited. | ||
|
||
Similarly, you can override a non-abstract `open` member with an abstract one: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh, I do not think this example adds anything useful for one reading the documentation. Such pattern of overriding open
with abstract
is very strange and should not be used unless there are very specific circumstances.
docs/topics/classes.md
Outdated
|
||
You can override a non-abstract `open` member with an abstract one. | ||
## Open keyword |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say this section makes more sense in the Inheritance
part of the documentation
docs/topics/classes.md
Outdated
|
||
Even more specifically, if you declare a [companion object](object-declarations.md#companion-objects) inside your class, | ||
If you need to write a function that can be called without having a class object but still needs access | ||
to the internals of the class, you can define it inside `companion object` within that class: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you define "internals of the class" here? A companion object can access its own internals, but class itself does not have any internals until you have created an instance of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, here the terminology begins being a bit confusing, as "class object" and "companion object" look very similar, but mean significantly different things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you define "internals of the class" here?
This comes in the current version of the docs and I didn't modify it, but I rephrased it. What do you think?
@@ -1,245 +1,747 @@ | |||
[//]: # (title: Classes) | |||
|
|||
Classes in Kotlin are declared using the keyword `class`: | |||
Like other object-oriented languages, Kotlin uses _classes_ to encapsulate data and behavior | |||
for reusable, structured code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm missing a short intro talking about different "parts" of a class, similar to what you have in one of the later sections. Smth like "Classes encapsulate data (properties) and behavior (functions). They are templates for objects, which you create via constructors."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also prefer to see a very simple example in the beginning of "class -> ctor -> created object instance" which we then iteratively expand on, by adding more properties, functions, ctors, init blocks or other things.
82db250
to
9fb5f07
Compare
3ce95e1
to
3871cf7
Compare
This PR revamps the content and code examples in the Classes page following external feedback.
Some of the key changes are:
Improve the whole page and revamp the code snippets.
Explain the use case for multiple init blocks.
Explain constructor chaining.
Add examples for primary constructors.
Clarify the open keyword.
Explain how to access a class property from a class member function.
Use the term "object" consistently.
Clarify what abstract classes are.
Provide example companion objects.