0

I'm using an array that returns whether any of my nested buttons are selected.

To make it easier on me, I made this internal function.

Everything gets really really slow.

I took out the helper function and build times went up to pretty much instant.

Not sure why the type inference isn't working here? (If that is the problem)

I have read this Why is swift compile time so slow?, but I don't yet know why these specific lines are stalling.

func areAnyAMPMButtonsSelected() -> [Bool] {


     func nilToBool(optional: AppointmentDatePickerAMPMButton?) -> Bool                      {
          return (optional != nil) ? optional!.selected : false
      }  

    return [
    nilToBool(self.sundayAMButton),
    nilToBool(self.mondayAMButton),
    nilToBool(self.tuesdayAMButton),
    nilToBool(self.wednesdayAMButton),
    nilToBool(self.thursdayAMButton),
    nilToBool(self.fridayAMButton),
    nilToBool(self.saturdayAMButton),

    nilToBool(self.sundayPMButton),
    nilToBool(self.mondayPMButton),
    nilToBool(self.tuesdayPMButton),
    nilToBool(self.wednesdayPMButton),
    nilToBool(self.thursdayPMButton),
    nilToBool(self.fridayPMButton),
    nilToBool(self.saturdayPMButton)
]

Compiler Stalling here

1.  While type-checking 'getValidTimesArray' at /Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:104:5

2. While type-checking expression at [/Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:110:16 - line:126:9] RangeText="[ nilToBool(self.sundayAMButton), nilToBool(self.mondayAMButton), nilToBool(self.tuesdayAMButton), nilToBool(self.wednesdayAMButton), nilToBool(self.thursdayAMButton), nilToBool(self.fridayAMButton), nilToBool(self.saturdayAMButton),

        nilToBool(self.sundayPMButton),
        nilToBool(self.mondayPMButton),
        nilToBool(self.tuesdayPMButton),
        nilToBool(self.wednesdayPMButton),
        nilToBool(self.thursdayPMButton),
        nilToBool(self.fridayPMButton),
        nilToBool(self.saturdayPMButton)
    ]"
Community
  • 1
  • 1
ovatsug25
  • 7,786
  • 7
  • 34
  • 48
  • Possible [duplicate](http://stackoverflow.com/a/25813625/148357)? – Antonio May 01 '15 at 15:08
  • @Antonio - in this case its stuck type-checking, not indexing so different problem. type checking needs to be cleared up here and I am wondering whether it can be done without some obscene repetion – ovatsug25 May 01 '15 at 16:05
  • Have you tried reducing the array to just 3 or 4 elements, just for testing? in the few cases I experienced slow compilation, the solution has always been to split the initialization into multiple lines. – Antonio May 01 '15 at 16:09
  • Unclear what you're asking. What kind of answer would make you happy here? – matt May 01 '15 at 16:10
  • @matt - understanding the type checking system. negative answers, such as this is not possible are super ok. i need to know how it works. – ovatsug25 May 01 '15 at 16:17
  • @Antonio - thats a pretty interesting point! it sped things up back to fast, though I thought that picking between that and not using the helper function is for me a wash. – ovatsug25 May 01 '15 at 16:19
  • Please don't answer your own question in your question. Answer your own question as an answer! You are even allowed to _accept_ your own answer!! – matt May 01 '15 at 16:23

2 Answers2

1

I don't know if it solves the problem, but it's worth checking - create the array with just the properties, then use the map method to transform each of them into the return value of the nilToBool function:

return [
    self.sundayAMButton,
    self.mondayAMButton,
    self.tuesdayAMButton,
    self.wednesdayAMButton,
    self.thursdayAMButton,
    self.fridayAMButton,
    self.saturdayAMButton,
    self.sundayPMButton,
    self.mondayPMButton,
    self.tuesdayPMButton,
    self.wednesdayPMButton,
    self.thursdayPMButton,
    self.fridayPMButton,
    self.saturdayPMButton
    ].map(nilToBool)
Antonio
  • 71,651
  • 11
  • 148
  • 165
1

Still slow with this here:

    1.  While type-checking 'getValidTimesArray' at /Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:104:5
    2.  While type-checking expression at [/Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:110:16 - line:126:9] RangeText="[
        nilToBool(self.sundayAMButton) as Bool,
        nilToBool(self.mondayAMButton) as Bool,
        nilToBool(self.tuesdayAMButton) as Bool,
        nilToBool(self.wednesdayAMButton) as Bool,
        nilToBool(self.thursdayAMButton) as Bool,
        nilToBool(self.fridayAMButton) as Bool,
        nilToBool(self.saturdayAMButton) as Bool,

        nilToBool(self.sundayPMButton) as Bool,
        nilToBool(self.mondayPMButton) as Bool,
        nilToBool(self.tuesdayPMButton) as Bool,
        nilToBool(self.wednesdayPMButton) as Bool,
        nilToBool(self.thursdayPMButton) as Bool,
        nilToBool(self.fridayPMButton) as Bool,
        nilToBool(self.saturdayPMButton) as Bool
    ]"

Still slow in this case:

    var returnArray: [Bool] = []


    returnArray  = [
                nilToBool(self.sundayAMButton) as Bool,
                nilToBool(self.mondayAMButton) as Bool,
                nilToBool(self.tuesdayAMButton) as Bool,
                nilToBool(self.wednesdayAMButton) as Bool,
                nilToBool(self.thursdayAMButton) as Bool,
                nilToBool(self.fridayAMButton) as Bool,
                nilToBool(self.saturdayAMButton) as Bool,

                nilToBool(self.sundayPMButton) as Bool,
                nilToBool(self.mondayPMButton) as Bool,
                nilToBool(self.tuesdayPMButton) as Bool,
                nilToBool(self.wednesdayPMButton) as Bool,
                nilToBool(self.thursdayPMButton) as Bool,
                nilToBool(self.fridayPMButton) as Bool,
                nilToBool(self.saturdayPMButton) as Bool
    ]

    return returnArray

But it gets pretty fast here! Though this is in a way a lot of code:

    var returnArray: [Bool] = []

    returnArray.append(nilToBool(self.sundayAMButton) as Bool)
    returnArray.append(nilToBool(self.sundayAMButton) as Bool)
    returnArray.append(nilToBool(self.mondayAMButton) as Bool)
    returnArray.append(nilToBool(self.tuesdayAMButton) as Bool)
    returnArray.append(nilToBool(self.wednesdayAMButton) as Bool)
    returnArray.append(nilToBool(self.thursdayAMButton) as Bool)
    returnArray.append(nilToBool(self.fridayAMButton) as Bool)
    returnArray.append(nilToBool(self.saturdayAMButton) as Bool)
    returnArray.append(nilToBool(self.sundayPMButton) as Bool)
    returnArray.append(nilToBool(self.mondayPMButton) as Bool)
    returnArray.append(nilToBool(self.tuesdayPMButton) as Bool)
    returnArray.append(nilToBool(self.wednesdayPMButton) as Bool)
    returnArray.append(nilToBool(self.thursdayPMButton) as Bool)
    returnArray.append(nilToBool(self.fridayPMButton) as Bool)
    returnArray.append(nilToBool(self.saturdayPMButton) as Bool)

    return returnArray

This is the solution I opted for, pretty quick.

    return [
        (self.sundayAMButton    != nil)   ?  self.sundayAMButton.selected     : false,
        (self.mondayAMButton    != nil)   ?  self.mondayAMButton.selected     : false,
        (self.tuesdayAMButton   != nil)   ?  self.tuesdayAMButton.selected    : false,
        (self.wednesdayAMButton != nil)   ?  self.wednesdayAMButton.selected  : false,
        (self.thursdayAMButton  != nil)   ?  self.thursdayAMButton.selected   : false,
        (self.fridayAMButton    != nil)   ?  self.fridayAMButton.selected     : false,
        (self.saturdayAMButton  != nil)   ?  self.saturdayAMButton.selected   : false,

        (self.sundayPMButton    != nil)   ?  self.sundayAMButton.selected    : false,
        (self.mondayPMButton    != nil)   ?  self.mondayAMButton.selected    : false,
        (self.tuesdayPMButton   != nil)   ?  self.tuesdayAMButton.selected   : false,
        (self.wednesdayPMButton != nil)   ?  self.wednesdayAMButton.selected : false,
        (self.thursdayPMButton  != nil)   ?  self.thursdayAMButton.selected  : false,
        (self.fridayPMButton    != nil)   ?  self.fridayAMButton.selected    : false,
        (self.saturdayPMButton  != nil)   ?  self.saturdayAMButton.selected  : false
    ]
ovatsug25
  • 7,786
  • 7
  • 34
  • 48