-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
944d70b
commit b5ca047
Showing
8 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Mixin | ||
|
||
Start typing here... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Class Objects | ||
# Classes & Objects | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters