Skip to content

Commit

Permalink
Add more content
Browse files Browse the repository at this point in the history
  • Loading branch information
theEvilReaper committed Jan 24, 2024
1 parent 944d70b commit b5ca047
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Writerside/docs.tree
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
start-page="overview.md">
<toc-element topic="overview.md"/>
<toc-element topic="Lets-get-started.md"/>
<toc-element topic="Directives.md"/>
<toc-element topic="annotation.md"/>
<toc-element topic="variables.md"/>
<toc-element topic="placeholders.md"/>
<toc-element topic="classes.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>
Expand Down
16 changes: 16 additions & 0 deletions Writerside/topics/Directives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Directives / Imports

When you're writing an application which Dart / Flutter sometimes you need to add imports from other classes or files to
access new types, function or other related stuff. The desing of the library only allows that import can be added to a
`DartFileSpec` and to no other type.

### Create imports

The creation of Import objects is quite simple over the api from DartPoet. There is a `DirectiveFactory` that has some
methods which allows the creation of those objects.

TODO: Add code

> DartPoet can't generate an import for that class automatically. When you add a new class to the generation and its
> require an important, so you must add this by your own
> {style="warning"}
15 changes: 15 additions & 0 deletions Writerside/topics/Extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.

### Create an extension method

DartPoet provides a dedicated spec object for the usage of extension structures.


> If you need more acknowledgement about extensions in Dart please visit their [documentation](https://dart.dev/language/extension-methods)
> {style=note}
15 changes: 15 additions & 0 deletions Writerside/topics/Lets-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ The project is available over the Maven Central Repository and doesn't require t

To use it, you need to add the following dependency to your used build system:

### Maven: {collapsible="true"}

```xml
<dependency>
<groupId>dev.themeinerlp</groupId>
<artifactId>dartpoet</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
```

### Gradle: {collapsible="true"}
```kotlin
implementation("dev.themeinerlp:dartpoet:0.0.1-SNAPSHOT")
```

## API Specifications

Most of API from DartPoet uses immutable objects from Kotlin.
Expand Down
121 changes: 121 additions & 0 deletions Writerside/topics/Methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Methods

Methods are functions that a class can have. This adds functionality to a class and provides the behaviour for each
method. The library has a `FunctionSpec` which allows the creation of functions that can be added to a class.

### Instance methods

A class definition can have an unspecific amount of instance methods. An instance method can access instance variables
from a class and use them for his behaviour.

For our example we have a small two-dimensional `Point` and want to add the behaviour that a function calculates the
distance to another point object.

Point class:

```text
import 'dart:math';
class Point {
final double x;
final double y;
Point(this.x, this.y);
}
```

Now we create our FunctionSpec which creates the required method:

```Kotlin
val calcFunc = FunctionSpec.builder("distanceTo")
.returns(Double::class)
.parameter(ParameterSpec.builder("other", ClassName("Point")).build())
.addCode(
buildCodeBlock {
addStatement("var dx = x - other.x;");
addStatement("var dy = y - other.y;");
add("return sqrt(dx * dx + dy * dy);");
}
)
.build()
```

The generated code for this function is the following:

```text
double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
```

And the generated file looks like this (for this example we say that we have already a FileSpec for this case:

```text
import 'dart:math';
class Point {
final double x;
final double y;
Point(this.x, this.y);
}
double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
```

## Operators

> The functionality of operator overloading is not supported by DartPoet!!
> {style="warning"}
## Getters and setters

Getters and setters are a special type of methods. Those methods provide read and write access to an object's
properties. Dart generates an implicit getter, plus a setter if appropriate, for every instance variable of a class.
You can create additional properties by implementing getters and setters, using the `FunctionSpec` with the right
attributes.

The creation of getters and setters is very similar to the creation of a normal function. To tell the library that it
should be as setter, you need to set the `setter`or `getter` attribute to `true`.

### Getter example {collapsible="true"}
```Kotlin
val getter = FunctionSpec.builder("x")
.getter(true) //Indicates the function is a getter
.returns(Int::class)
.addCode("%L", "10")
.build()
```

The getter will be generated as the following:

```text
int get x => 10;
```

### Setter example {collapsible="true"}
```Kotlin
val setter = FunctionSpec.builder("x")
.setter(true) //Indicates the function is a setter
.addParameter(ParameterSpec.builder("value", Int::class).build())
.addCode("%L", "10")
.build()
```

## Abstract methods

Functions, getters and setters can be abstract. Abstraction is more relevant when the functions etc. are defined in an
interface. In this case the implementation is delegated to another class which relies on the interface.

> Abstract methods can only exist in abstract classes or [mixins](Mixin.md).
> {style="note"}
```Kotlin
TODO
```
3 changes: 3 additions & 0 deletions Writerside/topics/Mixin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Mixin

Start typing here...
3 changes: 2 additions & 1 deletion Writerside/topics/classes/classes.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Class Objects
# Classes & Objects

2 changes: 1 addition & 1 deletion Writerside/topics/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ When you want to create variable for your code you need to use the `PropertySpec
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`.

#### Creation of a `Property`:
#### 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.
Expand Down

0 comments on commit b5ca047

Please sign in to comment.