Re: CGFloat and literal floats in Swift
Slightly off topic; I would love to see some form of what Scala calls implicits in swift. For exactly this reason. On Mon, 27 Jul 2015 05:10 Rick Mann wrote: > > > On Jul 26, 2015, at 20:57 , Jens Alfke wrote: > > > > > >> On Jul 26, 2015, at 4:50 PM, Quincey Morris < > quinceymor...@rivergatesoftware.com> wrote: > >> > >> No. Swift doesn’t convert between numeric types automatically, so > Double —> CGFloat produces an error. > > > > I’m guessing this is only a problem when building 32-bit? Because in > 64-bit, CGFloat is equivalent to Double, so there shouldn’t be a problem > there. > > I don't think this is a 32- vs 64-bit issue. For example, both of these > fail: > > let f : CGFloat = Float(1.0) > let g : CGFloat = Double(1.0) > > > Rick: You could just define your own CGFloat constant equal to M_PI. > (You could even name it π …!) > > I guess, but that's not really the problem. I think it would not be > unusual for code in one place to produce Double (or other arithmetic type) > values that you then want to pass to CG. But you have to explicitly convert > them. Seems very clunky to me. I prefer C's approach where a conversion > that doesn't lose data is allowed, and you can turn on warnings for ones > that might lose data. > > > > > —Jens > > ___ > > > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > > > Please do not post admin requests or moderator comments to the list. > > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > > > Help/Unsubscribe/Update your Subscription: > > > https://lists.apple.com/mailman/options/cocoa-dev/rmann%40latencyzero.com > > > > This email sent to rm...@latencyzero.com > > > -- > Rick Mann > rm...@latencyzero.com > > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > > https://lists.apple.com/mailman/options/cocoa-dev/timothy.m.fletcher%40gmail.com > > This email sent to timothy.m.fletc...@gmail.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
> On Jul 26, 2015, at 20:57 , Jens Alfke wrote: > > >> On Jul 26, 2015, at 4:50 PM, Quincey Morris >> wrote: >> >> No. Swift doesn’t convert between numeric types automatically, so Double —> >> CGFloat produces an error. > > I’m guessing this is only a problem when building 32-bit? Because in 64-bit, > CGFloat is equivalent to Double, so there shouldn’t be a problem there. I don't think this is a 32- vs 64-bit issue. For example, both of these fail: let f : CGFloat = Float(1.0) let g : CGFloat = Double(1.0) > Rick: You could just define your own CGFloat constant equal to M_PI. (You > could even name it π …!) I guess, but that's not really the problem. I think it would not be unusual for code in one place to produce Double (or other arithmetic type) values that you then want to pass to CG. But you have to explicitly convert them. Seems very clunky to me. I prefer C's approach where a conversion that doesn't lose data is allowed, and you can turn on warnings for ones that might lose data. > > —Jens > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > https://lists.apple.com/mailman/options/cocoa-dev/rmann%40latencyzero.com > > This email sent to rm...@latencyzero.com -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
> On Jul 26, 2015, at 4:50 PM, Quincey Morris > wrote: > > No. Swift doesn’t convert between numeric types automatically, so Double —> > CGFloat produces an error. I’m guessing this is only a problem when building 32-bit? Because in 64-bit, CGFloat is equivalent to Double, so there shouldn’t be a problem there. Rick: You could just define your own CGFloat constant equal to M_PI. (You could even name it π …!) —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
On Jul 26, 2015, at 17:42 , Rick Mann wrote: > > So, why can I do this? > > let n : NSNumber = M_PI That’s bridged, not converted. :) The Swift compiler has built-in knowledge of what can be bridged, and does a typecast or value conversion according to context. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
> On Jul 26, 2015, at 16:50 , Quincey Morris > wrote: > > On Jul 26, 2015, at 16:38 , Rick Mann wrote: >> >> So, is a literal 0.0 not of type Double? > > No, it’s a numeric literal, so it has no numeric type. However your ‘addArc’ > function requires CGFloat parameters. Literal 0.0 is convertible to CGFloat, > but Double M_PI isn’t convertible automatically. > >> And can CGFloat be made to be implicitly assignable from Double? > > No. Swift doesn’t convert between numeric types automatically, so Double —> > CGFloat produces an error. However, many types satisfy a > ‘…LiteralConvertible’ protocol, which allows the compiler to treat a > compile-time literal such as 0 (which is ambiguous as to Swift type) to the > type expected in a given context. So, why can I do this? let n : NSNumber = M_PI -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
On Jul 26, 2015, at 16:38 , Rick Mann wrote: > > So, is a literal 0.0 not of type Double? No, it’s a numeric literal, so it has no numeric type. However your ‘addArc’ function requires CGFloat parameters. Literal 0.0 is convertible to CGFloat, but Double M_PI isn’t convertible automatically. > And can CGFloat be made to be implicitly assignable from Double? No. Swift doesn’t convert between numeric types automatically, so Double —> CGFloat produces an error. However, many types satisfy a ‘…LiteralConvertible’ protocol, which allows the compiler to treat a compile-time literal such as 0 (which is ambiguous as to Swift type) to the type expected in a given context. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
> On Jul 26, 2015, at 16:34 , Quincey Morris > wrote: > > On Jul 26, 2015, at 16:29 , Rick Mann wrote: >> >> addArc(CGPoint(x: 0, y: 0), 50.0, 0.0, M_PI) > > The problem in this case is that M_PI is a Double variable in Swift, not a > compile time constant like in Obj-C. You will unfortunately have to wrap it > in CGFloat(), then the constants should work fine. So, is a literal 0.0 not of type Double? And can CGFloat be made to be implicitly assignable from Double? -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
On Jul 26, 2015, at 16:29 , Rick Mann wrote: > > addArc(CGPoint(x: 0, y: 0), 50.0, 0.0, M_PI) The problem in this case is that M_PI is a Double variable in Swift, not a compile time constant like in Obj-C. You will unfortunately have to wrap it in CGFloat(), then the constants should work fine. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
Also weird, I only have to cast the last two arguments, not the second. > On Jul 26, 2015, at 16:07 , Quincey Morris > wrote: > > On Jul 26, 2015, at 15:57 , Rick Mann wrote: >> >> I'm finding it a bit cumbersome to use CGFloat in graphics code in Swift, >> because the compiler won't let me pass a floating-point literal to a >> parameter that takes a CGFloat. I have to wrap them all in CGFloat(). > > I’m not seeing this. Do you have an example that produces an error in a > playground? > -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
> On Jul 26, 2015, at 16:07 , Quincey Morris > wrote: > > On Jul 26, 2015, at 15:57 , Rick Mann wrote: >> >> I'm finding it a bit cumbersome to use CGFloat in graphics code in Swift, >> because the compiler won't let me pass a floating-point literal to a >> parameter that takes a CGFloat. I have to wrap them all in CGFloat(). > > I’m not seeing this. Do you have an example that produces an error in a > playground? Well, I'm trying, but Xcode crashes evaluating it. Here's the (iOS) playground: http://pastebin.com/UM8NWS34 import CoreGraphics import UIKit let ctx = UIGraphicsGetCurrentContext() func addArc(inCenter : CGPoint, _ inRadius : CGFloat, _ inStartAngle : CGFloat, _ inEndAngle : CGFloat, _ inClockwise : Bool = false) { CGContextAddArc(ctx, inCenter.x, inCenter.y, inRadius, inStartAngle, inEndAngle, inClockwise ? 1 : 0) } addArc(CGPoint(x: 0, y: 0), 50.0, 0.0, M_PI) I thought maybe the problem is that I'm not naming arguments in my CG wrapper function, and Swift isn't matching it because of the types? Weird, 'cause there's just the one function (it's actually a member in a class). But no, that doesn't seem to be it. I added names, and it still doesn't like it. The error I get in both cases is "cannot invoke 'addArc' with an argument list of type '(CGPoint, CGFloat, Double, Double)'" -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: CGFloat and literal floats in Swift
On Jul 26, 2015, at 15:57 , Rick Mann wrote: > > I'm finding it a bit cumbersome to use CGFloat in graphics code in Swift, > because the compiler won't let me pass a floating-point literal to a > parameter that takes a CGFloat. I have to wrap them all in CGFloat(). I’m not seeing this. Do you have an example that produces an error in a playground? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com