-
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
b5ca047
commit acea669
Showing
6 changed files
with
98 additions
and
19 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 |
---|---|---|
@@ -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"} |
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,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"} |
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,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 | ||
|
||
|
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