Function conforming to a typealias

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

···

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

It feels a bit strange to write it like that, but yes, that would work. Thanks!

(It will prevent me from having to touch up old functions when adding more parameters to the signature as the project evolves)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

···

On 18 Feb 2017, at 11:52, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Sorry: “prevent me from” should have been “allow me to avoid”

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

···

On 18 Feb 2017, at 12:05, Rien <Rien@Balancingrock.nl> wrote:

It feels a bit strange to write it like that, but yes, that would work. Thanks!

(It will prevent me from having to touch up old functions when adding more parameters to the signature as the project evolves)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

On 18 Feb 2017, at 11:52, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Just tried it, unfortunately it does not achieve what I wanted. As soon as a parameter is added, the compiler complains (rightly so) about ignoring an argument.

Oh well, at least I get warnings when parameters are added so that I don’t have to remember which functions need a touch-up.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

···

On 18 Feb 2017, at 12:05, Rien via swift-users <swift-users@swift.org> wrote:

It feels a bit strange to write it like that, but yes, that would work. Thanks!

(It will prevent me from having to touch up old functions when adding more parameters to the signature as the project evolves)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

On 18 Feb 2017, at 11:52, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

You’re welcome. I don’t think there is a better way of doing that. However you can build some wrappers with some shortcuts.

func createInstance<T>(of _: T.Type, _ istance: T) -> T {
     
    return istance
}

let myGreatFunc1 = createInstance(of: Function.self) {
     
    return "\($0) \($1)" // Or whatever
}

···

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 12:05:57, Rien (rien@balancingrock.nl) schrieb:

It feels a bit strange to write it like that, but yes, that would work. Thanks!

(It will prevent me from having to touch up old functions when adding more parameters to the signature as the project evolves)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

On 18 Feb 2017, at 11:52, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Typo, should be instance not istance.

···

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 12:16:34, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

You’re welcome. I don’t think there is a better way of doing that. However you can build some wrappers with some shortcuts.

func createInstance<T>(of _: T.Type, _ istance: T) -> T {
      
    return istance
}

let myGreatFunc1 = createInstance(of: Function.self) {
      
    return "\($0) \($1)" // Or whatever
}

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 12:05:57, Rien (rien@balancingrock.nl) schrieb:

It feels a bit strange to write it like that, but yes, that would work. Thanks!

(It will prevent me from having to touch up old functions when adding more parameters to the signature as the project evolves)

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

On 18 Feb 2017, at 11:52, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Not sure what you’re trying to solve there but you could use it like this with closures:

typealias Function = (Int, Bool) -> String?

let myGreatFunc1: Function = { _ in return nil /* or whatever you need there */ }
let myGreatFunc2: Function = { _ in return nil }

--
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 11:47:26, Rien via swift-users (swift-users@swift.org) schrieb:

I want to create a few functions that all have the same signature.

The signature is defined in a typealias

Is there any way to shortcut the function definition, or alternatively ensure that the function signature will always be equal to the typealias?

Examplecode:

typealias Mysig = (Int, Bool) -> String?

func myGreatFunc1:Mysig {…}

func myGreatFunc2:Mysig { …}

Alternatively

@conformsTo(Mysig)
func myGreatFunc1(_ a:Int, _ b: Bool) -> String? {…}

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users