How to build an extension for all Enums? - dart

I currently have two enums
enum UserVisibleSettings {
track,
mark,
}
enum UserVisibleTwo {
thing,
otherthing,
}
And the goal is that I want to make an extension that will allow me to convert an enum into a List<String>
I'm able to make an extension on UserVisibleSettings or UserVisibleTwo but how can I make an extension on all enums?

Not possible in current version of Dart since enums does not have a shared interface in Dart which are specific for enums.

Related

Extension method on a class without a constructor in dart? [duplicate]

I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}

How to create a static extension method in Dart?

I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}

How to create a common property in swift4

I need a common property in my project so that I can use or share it thought out the application.
I have tried many solutions but didn't work.
I think it would make many things simple and reusable.
You can use Extension
as documented in
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID383
you can change previously defined classes properties
example
extension UIColor {
class var customGreen: UIColor {
let darkGreen = 0x008110
return UIColor.rgb(fromHex: darkGreen)
}
}
Make use of the iOS Design pattern "Singleton".
Its uses only one reference for a class and the same property will get reflected where ever you are using it throughout the app.
Reference : https://thatthinginswift.com/singletons/
You can create a lightweight "namespace" by declaring a public struct or enum. Then, simply add your static vars and you can safely share it.
As an added benefit, you get thread-safety for free. (Static stored property initializers are thread-safe.)
public enum SharedConstants {
public static var id = "MyID"
public static var hashCode = 12345678
}
By the way, extensions are great if you want to enhance an existing type. Type extensions let you add new functionality to a type without modifying its original code.
Yet, if all you need is a common shared property, extensions may not be the best choice.

Does Dart 2.0 have extension methods?

in dart 2.0,types are mandatory,will dart 2.0 or 2.x add extension methods like
static int WordCount(this String str){
// custom String method here
}
No, there won't be extension methods in 2.0.
There is a lot of work going on to be able to move faster after 2.0.
I think extension methods is a popular wish, but there is no definitive statement Dart will get them.
Updated answer
As of today, Dart 2.6, added extension methods support.

How to create an extension for SequenceOf in Swift?

Generic arguments are not allowed in Swift extensions, something like
public extension SequenceOf<T>{
...
}
is not possible. Using something like
public extension SequenceOf{
public func Something<T>(){
....
}
}
leads to
Extension of generic type 'SequenceOf' from a different module cannot provide public declarations
So, how does one extend SequenceOf (or similar types) with extra methods?
The problem here is not that your are extending SequenceOf but that you are doing it in a different module (i.e. a library or a framework).
Add the source file containing the extension to the project that is using it.

Resources