-3

My question is not related to flagged question as I want to call both the functions at once.


I am looking for a solution when I can call two functions in a single line.

Here's the playground code.

import UIKit

func aFunc(file:String = #file, line:Int = #line, funcname:String = #function) {
    print("file: \(file)")
    print("line: \(line)")
    print("function: \(funcname)")
}

func bFunc(num:Int, thing:String) {
    for i in 1...num {
        print("\(thing)\n")
    }
}

func cFunc() {
    bFunc(num: 1, thing: "I  Swift!")
    aFunc()
}

cFunc()

Above code is working fine. And giving this as output.

I  Swift!

file: TestFunctions.playground
line: 19
function: cFunc()

However, I have a necessity where I will need to call bFunc( ) and aFunc( ) together.

I don't want to do it like you can see in the playground code.

I want something like or more better ways :

func cFunc() {
    bFunc(num: 1, thing: "I  Swift!").aFunc()
}

So it will output like the playground code.

What should I do to achieve this?

The reason I want to do is that I have written a lot of code and now I just don't want to make changes to achieve this functionality.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • 4
    Note that what you are trying to achieve is not going to happen *"at the same time"* or *"together"*, they will just be called one after the other. Generally what you are looking for is called "fluent api". You probably should introduce some classes / structs / objects into your code for that to work. – luk2302 May 03 '17 at 13:08
  • Is it even possible to have multiple functions execute at the exact same time? – LinusGeffarth May 03 '17 at 13:09
  • 3
    Please explain your "necessity" to call the function together, that does not sound right or useful at all. – luk2302 May 03 '17 at 13:10
  • 2
    Do you want to call two functions *"at the same time"* or *"in a single line"*? The latter can be done with `bFunc(...) ; aFunc()` – Martin R May 03 '17 at 13:11
  • Are you trying to influence the `#line` output? If so: `bFunc(num: 1, thing: "I Swift!"); aFunc()` – Xavier Lowmiller May 03 '17 at 13:11
  • @XML and Martin, I am aware that, I can call two functions in the same line like this. But I want them to call together (at the same time). – Hemang May 03 '17 at 13:14
  • @luk2302, thanks! let me explain: I have lots of functions with different arguments calling base function `bFunc( )` for various operations. Now from any other classes if I will call these functions, I just want to know from which class, function name and at the line, they get called. To achieve this now, I will need to do lots of rewrites and that's what I don't want to do. Hope this will be helpful. – Hemang May 03 '17 at 13:17
  • 1
    As long as the functions are being executed on the same thread, one will always be executed after the other. Maybe you are looking for [Grand Central Dispatch](http://www.appcoda.com/grand-central-dispatch/) to run the functions concurrently? – Xavier Lowmiller May 03 '17 at 13:18
  • @luk2302, right now, I will need to call `aFunc( )` every time, I calls `bFunc( )` or other functions. This is what I don't want. – Hemang May 03 '17 at 13:18
  • 1
    So you are looking for a way to call `aFunc` whenever another function gets called? Why not put a call to `aFunc` as the first statement into `bFunc`? – luk2302 May 03 '17 at 13:20
  • @XML, nope, GCD is not useful for this case. Please read my lastly added comments to understand the problem a bit more. – Hemang May 03 '17 at 13:20
  • @luk2302, `bFunc( )` is in a different class, the purpose of `aFunc( )` to print the things (you can see the arguments). If I will do what you suggest, will always print the things for `bFunc( )`. Hope you get my point. – Hemang May 03 '17 at 13:21
  • @Hemang You could always add `file:`, `line:` & `funcname:` parameters (w/ the appropriate default values, i.e `#file` etc.) to `bFunc`, then pass those arguments onto `aFunc`. – Hamish May 03 '17 at 13:24
  • @Hamish, yes - you're 100% correct if I start writing that code today. But as I told, I reached to a place from where I can't do that. Or I can rewrite the stuff, *waste of time*. – Hemang May 03 '17 at 13:25
  • @Hemang So you're saying you cannot edit the implementation of `bFunc`? – Hamish May 03 '17 at 13:30
  • People, if you want to see the exact problem place, here's my [class](https://github.com/hemangshah/printer) in Github for which I am looking for the solution. Please check the `Printer.swift` class. You will find lots of functions to print the logs. There is also one method `trace( )` which is equals to `aFunc( )` here. Everything is working fine if I will call the `trace( )` but I feel it's not a convenient solution. So if I can call the `trace ( )` together when I call the `show( )` function? You can find it inside `Printer.swift` class. – Hemang May 03 '17 at 13:34
  • Possible duplicate of [Swift - Method chaining](http://stackoverflow.com/questions/37114234/swift-method-chaining) –  May 03 '17 at 13:52

1 Answers1

1

The problem is that functions have a specific signature in Swift. If you know the signatures of the two functions you want to call, you can generalize the notion of calling both of them.

For example, let's take the simplest case, where our functions take no parameters and return no result:

func f() {print("f")}
func g() {print("g")}
func h() {print("h")}

Now we can write a generalized function for calling any two of those in succession:

func call(_ aFunction:()->(), and anotherFunction:()->()) {
    aFunction()
    anotherFunction()
}

Here's how to test it:

call(h, and:f)

And there certainly are situations where it is useful to have on hand a generalized call function of just this kind. But, as I said, your ability to do this depends on the signatures of the functions to be called slotting successfully into the call parameter types. You cannot pass just any old function to call, because Swift has strict typing.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks, matt, I appreciate your answer. I probably learn something new here. I will play more in the playground to check what I can do with my code. – Hemang May 04 '17 at 04:16
  • Is there a way to fix the strict argument for function passing? For example: In your code, we shouldn't interchange the functions h & f. Possible? – Hemang May 04 '17 at 04:30