I also agree that there are some problems with enums in current Swift.
(tl;dr: will Swift 3.0 improve iteration for enum values? Some kind of
.next() method for values or .all[] property. Currently we have a problem
with this.)
Java's version of enum declaration in this case seems much better IMO. It
allows to assign additional parameters to "main" enum value in handy way
and in one place(where the main value itself is declared). In Swift to
implement the same we have to write these for each param:
var <param>: <Type> {
switch self {
case .<first> : return <for first>
case .<second> : return <for second>
case .<etc> : return <for etc>
}
}
Actually, as I understand , we can use custom struct as RawValue to have
assigned params for each value i.e. something like this:
enum Devices: CGSize {
case iPhone5 = CGSize(width: 320, height: 568)
case iPhone6 = CGSize(width: 375, height: 667)
case iPhone6Plus = CGSize(width: 414, height: 736)
}
and
extension CGSize: StringLiteralConvertible {..}
let a = Devices.iPhone5
let b = a.rawValue
print("string is \(a), width is \(b.width), height is \(b.height)")
(found in Internet)
But personally I don't think this is big problem or if this is totally
unusable. I think we can live with this.
The real problem is iteration the enum values.
In initial message for this [Idea], in code sample, they use static
property to be able to iterate enum values - array where all values is stored:
static var all = [ Hearts, Spades, Diamonds, Clubs ]
If we add new enum value to this enum - Swift will protect us and force us
to add one more case: in each "switch self".
But! It will not protect us from not adding this new value to this "all"
array. This is absolutely not-Swift way.
Without such "all" property, how will you iterate values for such enum? I
didn't find any good method to iterate enums.
In Delphi(yes, I know ;-) you can iterate enums using such syntax:
i: EnumType
for i := Low(EnumType) to High(EnumType)
i.e. it will always work correctly, don't need to change this if you added
value.
and can have such declaration of array where indexes are EnumType:
a: array [EnumType] of String = ('1', '2', '3', '4')
and it will force you to fill/fix this array if you added/deleted the enum
value.
In Swift we need something to normally iterate enum values. Probably some
.next()->EnumType? method for each value or built-in .all:[EnumType] property.
Am I wrong somewhere? Are there some plans to improve enums?
On 08.04.2016 21:13, Kenny Leung via swift-evolution wrote:
I much prefer the Java style of enums, which are essentially classes limited to
a fixed set of instances. In Java, Suit would look like this:
public enum Suit {
Hearts("♥", true),
Spades("♠", false),
Diamonds("♦", true),
Clubs("♣", false);
String description;
boolean isRed;
Suit(String description, boolean isRed) {
this.description = description;
this.isRed = isRed;
}
}
The class java.lang.Enum already provides handy methods like
name(),
ordinal(),
toString(), and its inverse, valueOf()
values() - to get all values
-Kenny
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution