Skip to content

Commit

Permalink
Update documentation content
Browse files Browse the repository at this point in the history
  • Loading branch information
theEvilReaper committed Jan 24, 2024
1 parent b5ca047 commit acea669
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Writerside/docs.tree
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<toc-element topic="variables.md"/>
<toc-element topic="placeholders.md"/>
<toc-element topic="classes.md">
<toc-element topic="class.md"/>
<toc-element topic="Methods.md"/>
<toc-element topic="Mixin.md"/>
<toc-element topic="Extension.md"/>
<toc-element topic="class.md"/>
<toc-element topic="enum.md"/>
</toc-element>
<toc-element topic="contributing.md"/>
Expand Down
72 changes: 65 additions & 7 deletions Writerside/topics/Extension.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,73 @@
# Extension

Modern programming languages have a feature that allows the creation of additional methods to existing libraries.
Those methods acts like normal methods which are defined in the library itself. In the development process of an
application developers doesn't really know if the method they are using is an extension or not.
This functionality can be used in case where you need to add additional method but doesn't have the ability to change
the used library directly.
This feature allows the creation of additional methods, getters, setters to an existing class. This is a good case
when the use case requires such method but the library can't be modified. The usage of extension structures is the same
as for normal variants. In the development process a developer doesn't know if the used function is an extension or not.

DartPoet makes a different between functions and extension functions. The creation of extensions is achieved over the
`ExtensionSpec` and the builder structure.

> The current implementation only support functions and not getters and setters.
> {style="warning"}
### Create an extension method

DartPoet provides a dedicated spec object for the usage of extension structures.
For the creation of an extension it is required to know the class which should be extended and what the extension should
to. When the information presents, the creation of an extension is not very complex.

```kotlin
val extension = ExtensionSpec.builder("TestExtension", "String")
.function {
FunctionSpec.builder("hasSize")
.returns(Boolean::class)
.addCode(
CodeBlock.of(
"return this.length > 2;"
)
)
.build()
}
.build()
```

The generated code for this extension is the following:

```text
extension TestExtension on String {
bool hasSize() {
return this.length > 2;
}
}
```

### Generic extensions

Extensions can have a generic type for parameters. The generic type is bound on the static type of the structure that
the method calls. So it's not possible to create a generic function in a non-generic class.

Example usage of a generic extension:
```kotlin
val genericExtension = ExtensionSpec.builder("ListExt", List::class.parameterizedBy(ClassName("T")))
.genericTypes(ClassName("T"))
.build()
```

Which generates the following code:
```text
extension ListExt<T> on List<T> {}
```

> The extension structure checks if the generic types are used correctly otherwise it throws an exception.
> {style="warning"}
### Unnamed extensions

The declaration of an extension typically involves assigning a name. Unnamed extensions, on the other hand, lack a
specific identifier and are only visible within the library where they are declared. Since they are not applied
explicitly, unnamed extensions cannot be utilized to resolve API conflicts.

//TODO: Add example

> If you need more acknowledgement about extensions in Dart please visit their [documentation](https://dart.dev/language/extension-methods)
> {style=note}
> Note: You can invoke an unnamed extension’s static members only within the extension declaration.
> {style="note"}
16 changes: 15 additions & 1 deletion Writerside/topics/Mixin.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Mixin

Start typing here...
Dart has the language conecpt of mixing clases. These are classes which defines code that can be used in multiple class
hierarchies. They are intended to provide member implementations en masse.

The context of mixing has two keywords which are `with` and `mixin`. The `with` keyword is used to add a mixin to a
class.
`mixin` indicates that the class

To mark a class as mixing class, you need to add the `mixin` keyword to the class definition.

```kotlin
sss
```

> Mixins and mixing classes can't have an extends clause. The definition of constructors is not necessary.
> {style="warning"}
8 changes: 5 additions & 3 deletions Writerside/topics/class.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Class
# Classes

The programming language Dart supports different type such `Enum`, `abstract` or `normale` classes.
Both of them hve different abilities which are supported from the library.
Dart is an object-oriented programing languages which classes and a mixin-base inhertance.
Each object is an instance of a class. Mixin-based inheritance means that although every class (except for the top
class, Object?) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods
are a way to add functionality to a class without changing the class or creating a subclass. Class modifiers allow you


4 changes: 4 additions & 0 deletions Writerside/topics/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ class Greetings {
}
```

> When you are new to the programming language Dart or have question. Take a look at
> their [documentation](https://dart.dev/guides)
> {style=note}
**Note:** The team from Google is very active to enhance the programming language Dart which is a hard to stand update
with the specification from it.
15 changes: 8 additions & 7 deletions Writerside/topics/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

Most of the programming languages on the marked have the ability to store data in variables.
When you want to create variable for your code you need to use the `PropertySpec` structure to create them.
It could be a bit annoying that the structure which allows the creation variables doesn't have the same name.
The reason is that the definition from `Dart` names them `Properties`.
It could be a bit annoying that the structure which allows the creation variables doesn't have the same name.
The reason is that the definition from `Dart` names them `Properties`. A variable can have a bunch of different keywords
which changes some characteristics of the variable.

#### Creation of a Property:

The way to create a property is not very complex and has the same structure as `Functions`, `Parameters` etc.
To create a property you only need a reference from the `PropertySpecBuilder` which can be accessed over the `PropertySpec.builder()` call.
To create a property you only need a reference from the `PropertySpecBuilder` which can be accessed over
the `PropertySpec.builder()` call.
The `builder()` call needs two parameters, a name for the variable and a type

```kotlin
Expand All @@ -18,13 +20,12 @@ val property = PropertySpec.builder("name", String::class)
```

Which generates:

```text
String name = 'test';
```

By default, the PropertySpec produces a [null safe](https://dart.dev/null-safety) variant of a variable, which follows the guidelines from the language.
By default, the PropertySpec produces a [null safe](https://dart.dev/null-safety) variant of a variable, which follows
the guidelines from the language.
When the context requires a nullable variant use `nullable(true)` from the builder.

#### Inline properties:


0 comments on commit acea669

Please sign in to comment.