DartPoet Docs Help

Extension

Modern programming languages have a feature that allows the creation of additional methods to existing libraries. 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.

Create an extension method

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.

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:

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:

val genericExtension = ExtensionSpec.builder("ListExt", List::class.parameterizedBy(ClassName("T"))) .genericTypes(ClassName("T")) .build()

Which generates the following code:

extension ListExt<T> on List<T> {}

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

Last modified: 11 March 2024