Possible to defer initialization of let variable?

2015-07-07 Thread Rick Mann
I thought I saw some Swift code that did this:

let someString : String?

if some things
{
// some stuff

someString = a string from something

// some other stuff
}

//  use someString

But I can't get Swift 2 to let me.

Basically, I want to make someString available outside the scope in which it's 
initialized, but I don't want its assignment to ever change after that. I could 
do the former with var instead of let, of course, but I'd like it to be treated 
as a let after the first assignment. Is that possible?

-- 
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: Possible to defer initialization of let variable?

2015-07-07 Thread Quincey Morris
On Jul 7, 2015, at 01:08 , Rick Mann rm...@latencyzero.com wrote:
 
 But I can't get Swift 2 to let me.

You’ve left a path through the code that doesn’t set ‘someString’. So, you’ll 
need to add:

 else
 {
   someString = nil
 }

to the ‘if’.

___

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: Possible to defer initialization of let variable?

2015-07-07 Thread Rick Mann

 On Jul 7, 2015, at 01:16 , Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 On Jul 7, 2015, at 01:08 , Rick Mann rm...@latencyzero.com wrote:
 
 But I can't get Swift 2 to let me.
 
 You’ve left a path through the code that doesn’t set ‘someString’. So, you’ll 
 need to add:
 
 else
 {
  someString = nil
 }
 
 to the ‘if’.

Well, I wanted to do this:

//  Get the hero thumbnail…

let thumbURL: String?
for imgD in images
{
if let isHero = imgD[is_hero] as? Bool where isHero,
let t = imgD[thumbnail_signed_src] as? String
{
thumbURL = t
break
}
}

print(Thumb: \(thumbURL))

But it complains that it's used before being initialized. I guess I want it to 
initialize it to nil, then let me assign it once, then keep it constant.

-- 
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: Possible to defer initialization of let variable?

2015-07-07 Thread Rick Mann

 On Jul 7, 2015, at 01:37 , Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 On Jul 7, 2015, at 01:33 , Rick Mann rm...@latencyzero.com wrote:
 
 Yeah, there doesn't seem to be an elegant way to do it
 
 Actually, there is:
 
 let thumbURL: String? =
 {
  for imgD in images
  {
  if let isHero = imgD[is_hero] as? Bool where isHero,
  let t = imgD[thumbnail_signed_src] as? String
  {
  return t
  }
  }
  
  return nil
 } ()
 
 Or, if you prefer, instead of using a closure, you can define a nested 
 function and call it to get the initial value.

I tried it as a closure. The problem arises when you want to initialize more 
than one variable in that block of work.

-- 
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: Possible to defer initialization of let variable?

2015-07-07 Thread Rick Mann

 On Jul 7, 2015, at 01:49 , Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 On Jul 7, 2015, at 01:39 , Rick Mann rm...@latencyzero.com wrote:
 
 The problem arises when you want to initialize more than one variable in 
 that block of work.
 
 If you ask the wrong question, you get a wrong answer. :)

Whether or not it was the wrong question is debatable.

 You can use a tuple:

Unfortunately, that requires you to have the values all ready at once. Thanks 
for trying, but I think there's no good way to do this. Best is to just deal 
with var. Or do the hacky thing you suggested earlier, with local vars that get 
assigned at the very end to their lets.

-- 
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: Possible to defer initialization of let variable?

2015-07-07 Thread Quincey Morris
On Jul 7, 2015, at 01:39 , Rick Mann rm...@latencyzero.com wrote:
 
 The problem arises when you want to initialize more than one variable in that 
 block of work.

If you ask the wrong question, you get a wrong answer. :)

You can use a tuple:

 let (thumbURL, isHero) =
 {
   () - (String?, Bool) in
   
   for imgD in images
   {
   if let isHero = imgD[is_hero] as? Bool where isHero,
   let t = imgD[thumbnail_signed_src] as? String
   {
   return (t, isHero)
   }
   }
   
   return (nil, false)
 } ()

___

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: Possible to defer initialization of let variable?

2015-07-07 Thread Rick Mann
Yeah, there doesn't seem to be an elegant way to do it, so I gave up and used a 
var.

 On Jul 7, 2015, at 01:31 , Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 On Jul 7, 2015, at 01:21 , Rick Mann rm...@latencyzero.com wrote:
 
  let thumbURL: String?
  for imgD in images
  {
  if let isHero = imgD[is_hero] as? Bool where isHero,
  let t = imgD[thumbnail_signed_src] as? String
  {
  thumbURL = t
  break
  }
  }
 
 You’ve still got a path where ‘thumbURL’ is uninitialized — when ‘images’ is 
 empty. I’m not sure if there’s an elegant way to do it, but you can do it by 
 brute force something like this:
 
 do
 {
  var url: String? = nil
 
  for imgD in images
  {
  if let isHero = imgD[is_hero] as? Bool where isHero,
  let t = imgD[thumbnail_signed_src] as? String
  {
  url = t
  break
  }
  }
  
  thumbURL = url
 }


-- 
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: Possible to defer initialization of let variable?

2015-07-07 Thread Quincey Morris
On Jul 7, 2015, at 01:33 , Rick Mann rm...@latencyzero.com wrote:
 
 Yeah, there doesn't seem to be an elegant way to do it

Actually, there is:

 let thumbURL: String? =
 {
   for imgD in images
   {
   if let isHero = imgD[is_hero] as? Bool where isHero,
   let t = imgD[thumbnail_signed_src] as? String
   {
   return t
   }
   }
   
   return nil
 } ()

Or, if you prefer, instead of using a closure, you can define a nested function 
and call it to get the initial value.
___

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: Possible to defer initialization of let variable?

2015-07-07 Thread Quincey Morris
On Jul 7, 2015, at 01:21 , Rick Mann rm...@latencyzero.com wrote:
 
   let thumbURL: String?
   for imgD in images
   {
   if let isHero = imgD[is_hero] as? Bool where isHero,
   let t = imgD[thumbnail_signed_src] as? String
   {
   thumbURL = t
   break
   }
   }

You’ve still got a path where ‘thumbURL’ is uninitialized — when ‘images’ is 
empty. I’m not sure if there’s an elegant way to do it, but you can do it by 
brute force something like this:

 do
 {
   var url: String? = nil
 
   for imgD in images
   {
   if let isHero = imgD[is_hero] as? Bool where isHero,
   let t = imgD[thumbnail_signed_src] as? String
   {
   url = t
   break
   }
   }
   
   thumbURL = url
 }


___

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: Possible to defer initialization of let variable?

2015-07-07 Thread Greg Weston
 If you ask the wrong question, you get a wrong answer. :)
 
 Whether or not it was the wrong question is debatable.

Honestly I find that assertion suspect considering that you've moved the 
goalposts three times.
___

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