Re: [webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-16 Thread Ryosuke Niwa via webkit-dev
On Wed, Feb 16, 2022 at 6:13 AM Darin Adler  wrote:

> I think of it as following the same naming pattern as downcast<> or
> static_cast<>, but you don’t have to specify a template argument since it
> infers it for you.
>

I guess the closer analogy might be with const_cast, which does the "right
thing" based on the type.

I still think a single name conversion is less clear since we don't
typically call out which type of an object is returned by a function (since
we don't really use Hungarian notation). We'd end up having to look up the
function declaration / definition to see whether it returns CF vs NS/UI.

- R. Niwa
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-16 Thread Darin Adler via webkit-dev
I think of it as following the same naming pattern as downcast<> or 
static_cast<>, but you don’t have to specify a template argument since it 
infers it for you.

— Darin
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-16 Thread Darin Adler via webkit-dev
Just economy. There is no need for two different names. I personally like it 
this way, and have found it appealing when I used it. I think you should give 
it a try. We can certainly change the name later if this turns out to 
significantly improve things.

— Darin
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-15 Thread Ryosuke Niwa via webkit-dev
On Tue, Feb 15, 2022 at 9:44 AM Darin Adler via webkit-dev <
webkit-dev@lists.webkit.org> wrote:

> For those of you doing work with Objective-C on Cocoa platforms, I want to
> draw your attention to a great new idiom. Back in October, David Kilzer
> added bridge_cast, a type-safe set of functions that convert an Objective-C
> pointer to a Core Foundation pointer or vice versa, preserving types across
> the toll-free bridging boundary. It’s better than traditional non-WTF
> idioms where you use casts that look like “(__bridge id)” because you don’t
> have to write the type, and the correct corresponding type is chosen for
> you.
>
> When you have a CFStringRef and need to use it as an NSString key and
> value in an Objective-C dictionary, for example, the idiom would be:
>
> bridge_cast(constantKey): bridge_cast(constantValue),
>
> Rather than the old:
>
> (__bridge id)constantKey: (__bridge id)constantValue,
>
> It converts to NSString *, not id, which is better for many contexts, and
> good here, too. Since the function works in both directions, it will also
> turn an NSDictionary into a CFDictionaryRef. And it works on both raw
> pointers and on RetainPtr. I find it’s even more welcome to have something
> that can convert a RetainPtr into RetainPtr
> without danger of getting the reference counting wrong, doing the right
> thing in both ARC and non-ARC source files, and optimizing the move cases
> appropriately.
>

Was there a particular reason we picked the same name for both conversions?
At first glance, the code would have been easier to read or understand if
it said to which direction we're doing the conversion: e.g.
toCF(constantKey), toNS(constantKey). We do this for WebCore/WebKit type
conversions.

- R. Niwa
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-15 Thread Darin Adler via webkit-dev
Oh, forgot to say, it’s in this header:

 #include 

Using functions like this one will help us get over the “bridge” to ARC on this 
project eventually.

— Darin___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


[webkit-dev] Great function for Cocoa platform, bridge_cast

2022-02-15 Thread Darin Adler via webkit-dev
Hi folks.

For those of you doing work with Objective-C on Cocoa platforms, I want to draw 
your attention to a great new idiom. Back in October, David Kilzer added 
bridge_cast, a type-safe set of functions that convert an Objective-C pointer 
to a Core Foundation pointer or vice versa, preserving types across the 
toll-free bridging boundary. It’s better than traditional non-WTF idioms where 
you use casts that look like “(__bridge id)” because you don’t have to write 
the type, and the correct corresponding type is chosen for you.

When you have a CFStringRef and need to use it as an NSString key and value in 
an Objective-C dictionary, for example, the idiom would be:

bridge_cast(constantKey): bridge_cast(constantValue),

Rather than the old:

(__bridge id)constantKey: (__bridge id)constantValue,

It converts to NSString *, not id, which is better for many contexts, and good 
here, too. Since the function works in both directions, it will also turn an 
NSDictionary into a CFDictionaryRef. And it works on both raw pointers and on 
RetainPtr. I find it’s even more welcome to have something that can convert a 
RetainPtr into RetainPtr without danger of 
getting the reference counting wrong, doing the right thing in both ARC and 
non-ARC source files, and optimizing the move cases appropriately.

Please consider this instead of writing things like (CFStringRef)keyNS.get() 
because it’s easier to see it’s correct.

— Darin___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev