diff --git a/Writerside/docs.tree b/Writerside/docs.tree index e9eb4810..15a5be90 100644 --- a/Writerside/docs.tree +++ b/Writerside/docs.tree @@ -7,10 +7,14 @@ start-page="overview.md"> + + + + diff --git a/Writerside/topics/Directives.md b/Writerside/topics/Directives.md new file mode 100644 index 00000000..52c03e3b --- /dev/null +++ b/Writerside/topics/Directives.md @@ -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"} \ No newline at end of file diff --git a/Writerside/topics/Extension.md b/Writerside/topics/Extension.md new file mode 100644 index 00000000..456b73ac --- /dev/null +++ b/Writerside/topics/Extension.md @@ -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} diff --git a/Writerside/topics/Lets-get-started.md b/Writerside/topics/Lets-get-started.md index b6bb0ada..a5103d60 100644 --- a/Writerside/topics/Lets-get-started.md +++ b/Writerside/topics/Lets-get-started.md @@ -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 + + dev.themeinerlp + dartpoet + 0.0.1-SNAPSHOT + +``` + +### Gradle: {collapsible="true"} +```kotlin +implementation("dev.themeinerlp:dartpoet:0.0.1-SNAPSHOT") +``` + ## API Specifications Most of API from DartPoet uses immutable objects from Kotlin. diff --git a/Writerside/topics/Methods.md b/Writerside/topics/Methods.md new file mode 100644 index 00000000..37b0829e --- /dev/null +++ b/Writerside/topics/Methods.md @@ -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 +``` \ No newline at end of file diff --git a/Writerside/topics/Mixin.md b/Writerside/topics/Mixin.md new file mode 100644 index 00000000..dfb9a183 --- /dev/null +++ b/Writerside/topics/Mixin.md @@ -0,0 +1,3 @@ +# Mixin + +Start typing here... \ No newline at end of file diff --git a/Writerside/topics/classes/classes.md b/Writerside/topics/classes/classes.md index f68af649..b524bad2 100644 --- a/Writerside/topics/classes/classes.md +++ b/Writerside/topics/classes/classes.md @@ -1 +1,2 @@ -# Class Objects \ No newline at end of file +# Classes & Objects + diff --git a/Writerside/topics/variables.md b/Writerside/topics/variables.md index df68b789..e67227c0 100644 --- a/Writerside/topics/variables.md +++ b/Writerside/topics/variables.md @@ -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.