ios - Replace enum's rawValue property with a custom operator -
well, honest, don't calling rawvalue
when accessing enum
value.
use enum
time , think calling .rawvalue
makes code less "readable":
enum fontsize: cgfloat { case small = 12 case normal = 15 case large = 18 } enum example: string { case first = "first" case second = "second" }
so, i'm trying define generic operator enum
"override" .rawvalue
.
i can non-generically.
postfix operator .. { } postfix func .. (lhs: example) -> string { return lhs.rawvalue } postfix func .. (lhs: fontsize) -> cgfloat { return lhs.rawvalue }
however, i'm lazy want universal solution. writes one, works all.
can me ? thank you.
update: people interested in question, if want increase/decrease functions enum
fontsize
above. use these:
postfix func ++ <t: rawrepresentable, v: floatingpointtype>(lhs: t) -> v { return (lhs.rawvalue as! v) + 1 } postfix func -- <t: rawrepresentable, v: floatingpointtype>(lhs: t) -> v { return (lhs.rawvalue as! v) - 1 } postfix func ++ <t: rawrepresentable, v: integertype>(lhs: t) -> v { return (lhs.rawvalue as! v) + 1 } postfix func -- <t: rawrepresentable, v: integertype>(lhs: t) -> v { return (lhs.rawvalue as! v) - 1 }
hei man, lazy! ;-)
postfix operator .. { } postfix func ..<t: rawrepresentable> (lhs: t) -> t.rawvalue { return lhs.rawvalue }
there's protocol :-)
anyway beware not introducing esoteric custom operators ;-)
Comments
Post a Comment