Re: Translating to Swift

2014-08-14 Thread Gerriet M. Denkmann

On 13 Aug 2014, at 17:35, Jean-Daniel Dupas mail...@xenonium.com wrote:

 At global scope
 
 var sharedThing : Thing = Thing();
 

Excellent. Just what I needed.

But: is this thread-safe? What if several threads are trying to use the 
sharedThing? The Swift book doesn't mention the word thread.

Kind regards,

Gerriet.

 Le 13 août 2014 à 12:30, Gerriet M. Denkmann gerr...@mdenkmann.de a écrit :
 
 
 How could I translate this to Swift?
 
 + (Thing *)sharedThing
 {
  static Thing *commonThing;
  static dispatch_once_t justOnce;
  dispatch_once( justOnce, ^void
  {
  commonThing = [ [ Thing alloc ] init ];
  }
  );
  
  return commonThing;
 }
 
 Gerriet.
 
 ___
 
 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/mailing%40xenonium.com
 
 This email sent to mail...@xenonium.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: Translating to Swift

2014-08-14 Thread Quincey Morris
On Aug 14, 2014, at 11:44 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:

 But: is this thread-safe? What if several threads are trying to use the 
 sharedThing?

Jean-Daniel is teasing you slightly. It *is* thread-safe. For the reason, look 
at the August 1 entry in the Swift blog:

https://developer.apple.com/swift/blog/

It’s in the 2nd-last paragraph of the entry.

___

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: Translating to Swift

2014-08-14 Thread Jean-Daniel Dupas
In fact, this is actually exactly equivalent to you sample. Global and class 
variable initialization on Swift is performed lazily and is implemented using 
dispatch_once().

And after a second thought, I think that it should be 'let' and not 'var', and 
you don't want the pointer to be changed after initialization.

I don't remember if static class variable are supported yet, but if they are, 
you can also use one instead of a global.

Le 14 août 2014 à 20:44, Gerriet M. Denkmann gerr...@mdenkmann.de a écrit :

 
 
 On 13 Aug 2014, at 17:35, Jean-Daniel Dupas mail...@xenonium.com wrote:
 
 At global scope
 
 var sharedThing : Thing = Thing();
 
 
 Excellent. Just what I needed.
 
 But: is this thread-safe? What if several threads are trying to use the 
 sharedThing? The Swift book doesn't mention the word thread.
 
 Kind regards,
 
 Gerriet.
 
 Le 13 août 2014 à 12:30, Gerriet M. Denkmann gerr...@mdenkmann.de a écrit :
 
 
 How could I translate this to Swift?
 
 + (Thing *)sharedThing
 {
 static Thing *commonThing;
 static dispatch_once_t justOnce;
 dispatch_once( justOnce, ^void
 {
 commonThing = [ [ Thing alloc ] init ];
 }
 );
 
 return commonThing;
 }
 
 Gerriet.
 
 ___
 
 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/mailing%40xenonium.com
 
 This email sent to mail...@xenonium.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: Translating to Swift

2014-08-14 Thread Roland King

 
 I don't remember if static class variable are supported yet, but if they are, 
 you can also use one instead of a global.

Not yet. Not yet even confirmed for Swift 1.0 release as far as I recall from 
the last devforum message I read on this. 



___

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: Translating to Swift

2014-08-14 Thread Gerriet M. Denkmann

On 15 Aug 2014, at 02:50, Quincey Morris quinceymor...@rivergatesoftware.com 
wrote:

 On Aug 14, 2014, at 11:44 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:
 
 But: is this thread-safe? What if several threads are trying to use the 
 sharedThing?
 
 Jean-Daniel is teasing you slightly. It *is* thread-safe. For the reason, 
 look at the August 1 entry in the Swift blog:
 
   https://developer.apple.com/swift/blog/
 
 It’s in the 2nd-last paragraph of the entry.

This paragraph reads:

The lazy initializer for a global variable (also for static members of structs 
and enums) is run the first time that global is accessed, and is launched as 
dispatch_once to make sure that the initialization is atomic. This enables a 
cool way to use dispatch_once in your code: just declare a global variable with 
an initializer and mark it private.

So it is indeed thread-safe via using dispatch_once. Good.

But does global variable means var? 
Jean-Daniel rightly says:  I think that it should be 'let' and not 'var', and 
you don't want the pointer to be changed after initialization

So this:
let sharedMaker : Maker = Maker();
seems to do exactly what I need in a thread-safe way.

Thanks for your help!

Kind regards,

Gerriet.


___

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: Translating to Swift

2014-08-14 Thread Quincey Morris
On Aug 14, 2014, at 19:17 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:

 But does global variable means var? 
 Jean-Daniel rightly says:  I think that it should be 'let' and not 'var', 
 and you don't want the pointer to be changed after initialization

I know nothing about the real answer, but my guess is that writes to a global 
var after initialization would not be atomic, but the initialization itself 
would be.

___

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

Translating to Swift

2014-08-13 Thread Gerriet M. Denkmann

How could I translate this to Swift?

+ (Thing *)sharedThing
{
static Thing *commonThing;
static dispatch_once_t justOnce;
dispatch_once( justOnce, ^void
{
commonThing = [ [ Thing alloc ] init ];
}
);

return commonThing;
}

Gerriet.

___

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: Translating to Swift

2014-08-13 Thread Jean-Daniel Dupas

At global scope

var sharedThing : Thing = Thing();

Le 13 août 2014 à 12:30, Gerriet M. Denkmann gerr...@mdenkmann.de a écrit :

 
 How could I translate this to Swift?
 
 + (Thing *)sharedThing
 {
   static Thing *commonThing;
   static dispatch_once_t justOnce;
   dispatch_once( justOnce, ^void
   {
   commonThing = [ [ Thing alloc ] init ];
   }
   );
   
   return commonThing;
 }
 
 Gerriet.
 
 ___
 
 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/mailing%40xenonium.com
 
 This email sent to mail...@xenonium.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