@@ -50,19 +50,7 @@ Add `part` pointing to a file which you want classes be generated in. with `.sup
5050part 'weather.sealed.dart';
5151` ` `
5252
53- Add `@Sealed` annotation, and an abstract private class as a manifest for generated code.
54-
55- You can choose between three types of equality using `@WithEquality(...)` annotation. Default equality is `data` if not
56- specified. This will become default equality for all sub-classes. You can change equality of each sub-class by using
57- this annotation on individual methods.
58-
59- Equality types :
60-
61- * `data` Equality is implemented with Equatable package. It behaves like kotlin data classes.
62- * `identity` Only identical instances are equal. It's like when you don't implement any specific equality.
63- * `distinct` All the instances are not equal with each other. Even an instance is not equal with itself.
64-
65- A basic example :
53+ Add `@Sealed` annotation, and an abstract private class as a manifest for generated code. For example :
6654
6755` ` ` dart
6856@Sealed()
@@ -75,61 +63,6 @@ abstract class _Weather {
7563}
7664` ` `
7765
78- In the proceeding example all classes will have `data` equality. For example if you wanted `identity` equality for all
79- classes but using `distinct` equality for `windy` :
80-
81- ` ` ` dart
82- @Sealed()
83- @WithEquality(Equality.identity)
84- abstract class _Weather {
85- void sunny();
86-
87- void rainy(int rain);
88-
89- @WithEquality(Equality.distinct)
90- void windy(double velocity, double? angle);
91- }
92- ` ` `
93-
94- An abstract super class is generated with name equal to name of manifest class without the underline (here `Weather`).
95- Each method will become a sub-class. There should be at least one method. sub-class names are based on method name
96- prefixed with super class name (for example `WeatherSunny`). Naming process can be tailored with use of `@WithPrefix`
97- and `@WithName` annotations. Each method argument will become a field in corresponding sub-class. Field names are equal
98- to argument names and field types are equal to argument types or dynamic if not specified. Argument types can be
99- overridden using `@WithType` annotation for example when type information is not available at build time. Note that you
100- can have nullable and non-nullable fields. In legacy projects all fields are considered nullable.
101-
102- To change prefix of sub-class names which by default is top class name, you can use `@WithPrefix` annotation. for
103- example :
104-
105- ` ` ` dart
106- @Sealed()
107- @WithPrefix('Hello')
108- abstract class _Weather {
109- void sunny();
110- }
111- ` ` `
112-
113- Now `sunny` will be named `HelloSunny` instead of the default `WeatherSunny`. You can use `@WithPrefix('')` to remove
114- all prefix from sub-class names.
115-
116- To change sub-class names directly you can use `@WithName` annotation. It will override `WithPrefix` if specified. for
117- example :
118-
119- ` ` ` dart
120- @Sealed()
121- abstract class _Weather {
122- @WithName('Hello')
123- void sunny();
124- }
125- ` ` `
126-
127- Now `sunny` will be named `Hello` instead of the default `WeatherSunny`. This is useful if you want not to use prefix
128- for some items.
129-
130- Almost all methods on sealed classes use short names extracted from manifest method names. Full sub-class names are not
131- used. It is recommended not to use sub-classes directly. There are factory methods for each item on super class.
132-
13366Then run the following command to generate code for you.
13467
13568` ` ` bash
@@ -230,6 +163,86 @@ Notes:
230163- Prefer using factories in super class instead of sub-class constructors.
231164- Minimize usage of cast methods, most of the time they can be replaced with a match method.
232165
166+ # # Equality and generated class names
167+
168+ You can choose between three types of equality using `@WithEquality(...)` annotation. Default equality is `data` if not
169+ specified. This will become default equality for all sub-classes. You can change equality of each sub-class by using
170+ this annotation on individual methods.
171+
172+ Equality types :
173+
174+ * `data` Equality is implemented with Equatable package. It behaves like kotlin data classes.
175+ * `identity` Only identical instances are equal. It's like when you don't implement any specific equality.
176+ * `distinct` All the instances are not equal with each other. Even an instance is not equal with itself.
177+
178+ A basic example :
179+
180+ ` ` ` dart
181+ @Sealed()
182+ abstract class _Weather {
183+ void sunny();
184+
185+ void rainy(int rain);
186+
187+ void windy(double velocity, double? angle);
188+ }
189+ ` ` `
190+
191+ In the proceeding example all classes will have `data` equality. For example if you wanted `identity` equality for all
192+ classes but using `distinct` equality for `windy` :
193+
194+ ` ` ` dart
195+ @Sealed()
196+ @WithEquality(Equality.identity)
197+ abstract class _Weather {
198+ void sunny();
199+
200+ void rainy(int rain);
201+
202+ @WithEquality(Equality.distinct)
203+ void windy(double velocity, double? angle);
204+ }
205+ ` ` `
206+
207+ An abstract super class is generated with name equal to name of manifest class without the underline (here `Weather`).
208+ Each method will become a sub-class. There should be at least one method. sub-class names are based on method name
209+ prefixed with super class name (for example `WeatherSunny`). Naming process can be tailored with use of `@WithPrefix`
210+ and `@WithName` annotations. Each method argument will become a field in corresponding sub-class. Field names are equal
211+ to argument names and field types are equal to argument types or dynamic if not specified. Argument types can be
212+ overridden using `@WithType` annotation for example when type information is not available at build time. Note that you
213+ can have nullable and non-nullable fields. In legacy projects all fields are considered nullable.
214+
215+ To change prefix of sub-class names which by default is top class name, you can use `@WithPrefix` annotation. for
216+ example :
217+
218+ ` ` ` dart
219+ @Sealed()
220+ @WithPrefix('Hello')
221+ abstract class _Weather {
222+ void sunny();
223+ }
224+ ` ` `
225+
226+ Now `sunny` will be named `HelloSunny` instead of the default `WeatherSunny`. You can use `@WithPrefix('')` to remove
227+ all prefix from sub-class names.
228+
229+ To change sub-class names directly you can use `@WithName` annotation. It will override `WithPrefix` if specified. for
230+ example :
231+
232+ ` ` ` dart
233+ @Sealed()
234+ abstract class _Weather {
235+ @WithName('Hello')
236+ void sunny();
237+ }
238+ ` ` `
239+
240+ Now `sunny` will be named `Hello` instead of the default `WeatherSunny`. This is useful if you want not to use prefix
241+ for some items.
242+
243+ Almost all methods on sealed classes use short names extracted from manifest method names. Full sub-class names are not
244+ used. It is recommended not to use sub-classes directly. There are factory methods for each item on super class.
245+
233246# # Generic Usage
234247
235248For generic sealed classes you should write manifest class like a generic class which you are implementing.
0 commit comments