Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not discard casting information #149

Closed
charlieMonroe opened this issue Dec 11, 2015 · 1 comment
Closed

Do not discard casting information #149

charlieMonroe opened this issue Dec 11, 2015 · 1 comment

Comments

@charlieMonroe
Copy link

Currently, casting information is discarded:

[item setIndex:(int)index];

Becomes simply item.index = index. With Swift really forcing type-safe environment, where are no implicit conversions even between Int and UInt, this is a place I need to go back to and add the casting manually.

Proposal:

  • if you are casting to a primitive value (integers, floats, boolean), do the following: ObjC: [item setIndex:(int)index]; --> Swift: item.index = Int32(index) (int is a 32-bit integer).
  • if you are casting to an enum, do the following: ObjC: value = (MyEnum)index; --> Swift: value = MyEnum(rawValue: index) - since casting directly to an enum isn't possible in Swift.
  • if you are casting to a class type (or id) use as?: ObjC: myVar = (NSTableView *)[self superview]; --> Swift: myVar = self.superview as? NSTableView. In case of id, use AnyObject. Standard class conversions apply - NSString -> String, NSArray -> [AnyObject], NSDictionary -> [NSObject : AnyObject].
  • if you are casting to anything else:
    • if it's a pointer (e.g. void *, or float *, or mystruct *), use the following: ObjC: floatArray = (float *)malloc(16) --> floatArray = UnsafePointer<Float>(malloc(16)) - (UnsafePointer<Void>, UnsafePointer<mystruct>, ...)
    • at this point, I can't think of another valid cast, in which case I'd throw an error.
@drkameleon
Copy link
Collaborator

Not sure about how... complete my implementation is (we'll see, I guess), but as of the upcoming 2.3 I'm marking this as "fixed".

For now:

  • Type cast are now taken into account (while they were being ignored)
  • Primitive values are being casted using the proposed way (+ the usual type conversions)
  • Other types are casted by as? which seems like a rather universal (and safe) way

Looking forward to input once 2.3 goes out (I'm doing my best to release it today).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants