<div dir="ltr">Perhaps there's an argument to be made for a sort of 'enumDictionary' type - a dictionary whose keys are all the cases of an enum, and is thus guaranteed to produce a value.<div><div><br></div><div>I think the question I have is how you'd access the values, syntactically. To use the Planet example, if '.earth' is a value of the Planet enum, is '.earth.mass' an acceptable way to access its mass? Or perhaps 'Planet[.earth].mass'?</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 26, 2016 at 4:43 PM, Vladimir.S via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Or(if we are sure we'll don't forget to udpate `infoDict` in case of new added case in future):<br>
<br>
enum Planet {<br>
case earth<br>
case moon<span class=""><br>
<br>
struct PlanetInfo {<br>
var mass: Double<br>
var description: String<br>
}<br>
<br></span>
private static let infoDict = [<br>
Planet.earth :<br>
PlanetInfo(mass: 1.0, description:"Earth is our home"),<br>
.moon:<br>
PlanetInfo(mass: 0.2, description:"Just a moon"),<br>
]<br>
<br>
var info : PlanetInfo { return Planet.infoDict[self]! }<br>
}<br>
<br>
But I agree with you, IMO we need static stored properties for each case.<div class="HOEnZb"><div class="h5"><br>
<br>
On <a href="tel:26.05.2016%2018" value="+12605201618" target="_blank">26.05.2016 18</a>:15, Jānis Kiršteins wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The problem is that PlanetInfo values are recreated each time while<br>
they are static. Imagine if PlanetInfo where some type that expensive<br>
to create performance wise.<br>
<br>
You could solve it by:<br>
<br>
enum Planet {<br>
struct PlanetInfo {<br>
var mass: Double<br>
var description: String<br>
}<br>
<br>
case earth<br>
case moon<br>
<br>
private static earthInfo = PlanetInfo(mass: 1.0, description:<br>
"Earth is our home")<br>
private static moonInfo = PlanetInfo(mass: 0.2, description: "Just a moon")<br>
<br>
var info : PlanetInfo {<br>
switch self {<br>
case earth: return PlanetInfo.earthInfo<br>
case moon: return PlanetInfo.moonInfo<br>
}<br>
}<br>
}<br>
<br>
But that again more verbose. The proposed solution is explicit that<br>
those properties are static for each case.<br>
<br>
<br>
On Thu, May 26, 2016 at 5:58 PM, Vladimir.S via swift-evolution<br>
<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I support the proposal, but couldn't the initial target be achieved today<br>
with such (more verbose,yes) solution? :<br>
<br>
enum Planet {<br>
struct PlanetInfo {<br>
var mass: Double<br>
var description: String<br>
}<br>
<br>
case earth<br>
case moon<br>
<br>
var info : PlanetInfo {<br>
switch self {<br>
case earth: return PlanetInfo(mass: 1.0, description: "Earth is<br>
our home")<br>
case moon: return PlanetInfo(mass: 0.2, description: "Just a<br>
moon")<br>
}<br>
}<br>
}<br>
<br>
<br>
let e = Planet.earth<br>
print(e, e.info.description)<br>
<br>
let m = Planet.moon<br>
print(m, m.info.description)<br>
<br>
<br>
<br>
On 26.05.2016 8:26, Charlie Monroe via swift-evolution wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
What this proposal is asking for is an easier way to have derived values<br>
from enum cases. Asking for more flexible RawValues means mass and radius<br>
are not derived, they are the source of truth. It goes against the whole<br>
point of RawRepresentable. You are not saying ‘Mercury is identified by<br>
the case .mercury’, you are saying ‘Mercury is identified by a mass of<br>
3.303e+23’. It’s backwards.<br>
</blockquote>
<br>
<br>
I see what Janis meant in the first email. It's not that the planet would<br>
be identified by the mass or radius. It could very much be<br>
<br>
case Mercury = 1 where (mass: 3, radius: 2),<br>
<br>
- Mercury's rawValue would be 1.<br>
<br>
The issue here is that sometimes you want additional information with the<br>
enum. There are many cases where you extend the enum with a variable:<br>
<br>
enum Error {<br>
case NoError<br>
case FileNotFound<br>
...<br>
<br>
var isFatal: Bool {<br>
/// swtich over all values of self goes here.<br>
}<br>
<br>
var isNetworkError: Bool {<br>
/// swtich over all values of self goes here.<br>
}<br>
<br>
var isIOError: Bool {<br>
/// swtich over all values of self goes here.<br>
}<br>
}<br>
<br>
What the propsal suggests is to simplify this to the following:<br>
<br>
enum Error {<br>
var isFatal: Bool<br>
<br>
case NoError where (isFatal: false, isNetworkError: false, isIOError:<br>
false)<br>
case FileNotFound where (isFatal: true, isNetworkError: false, isIOError:<br>
true)<br>
...<br>
<br>
}<br>
<br>
So that you assign the additional information to the enum value itself.<br>
<br>
Charlie<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 26 May 2016, at 1:47 PM, David Sweeris via swift-evolution<br>
<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On May 25, 2016, at 10:27 PM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a><br>
<mailto:<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>>> wrote:<br>
<br>
On Wed, May 25, 2016 at 8:15 PM, David Sweeris via swift-evolution<br>
<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
<br>
On May 25, 2016, at 7:37 AM, Leonardo Pessoa via swift-evolution<br>
<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>><br>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Hi,<br>
<br>
Couldn't this be solved by using tuples? If not because the syntax<br>
is not allowed I think this would be more coherent to do it using<br>
current syntax.<br>
<br>
enum Planet : (mass: Float, radius: Float) {<br>
case mercury = (mass: 3.303e+23, radius: 2.4397e6)<br>
case venus = (mass: 4.869e+24, radius: 6.0518e6)<br>
case earth = (mass: 5.976e+24, radius: 6.37814e6)<br>
case mars = (mass: 6.421e+23, radius: 3.3972e6)<br>
case jupiter = (mass: 1.9e+27, radius: 7.1492e7)<br>
case saturn = (mass: 5.688e+26, radius: 6.0268e7)<br>
case uranus = (mass: 8.686e+25, radius: 2.5559e7)<br>
case neptune = (mass: 1.024e+26, radius: 2.4746e7)<br>
}<br>
</blockquote>
<br>
<br>
This would be my preferred solution… AFAIK, the only reason we<br>
can’t do it now is that Swift currently requires RawValue be an<br>
integer, floating-point value, or string. I don’t know why the<br>
language has this restriction, so I can’t comment on how hard it<br>
would be to change.<br>
<br>
- Dave Sweeris<br>
<br>
<br>
Except you'd have to write Planet.mercury.rawValue.mass, rather than<br>
Planet.mercury.mass.<br>
<br>
This could be one or two proposals: allow enums with tuple RawValues,<br>
and allow `TupleName.caseName.propertyName` to access a tuple element<br>
without going through .rawValue.<br>
</blockquote>
<br>
<br>
Good point… Has there been a thread on allowing raw-valued enums to be<br>
treated as constants of type `RawValue` yet? Either way, removing the<br>
restriction on what types can be a RawValue is still my preferred<br>
solution.<br>
<br>
- Dave Sweeris<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote>
<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote>
<br>
<br>
<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</blockquote>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote>
<br>
</blockquote>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>