[swift-users] Trouble with function class variables

Doug Hill fitz at breaqz.com
Wed Jun 15 20:20:09 CDT 2016


I’m just starting to learn Swift and attempting to do some functional-style programming. Specifically I’m learning how to create generic algorithms that can be reused for many different types.
What I’m attempting to do is create a new object, passing functions to the initializer. The class would store these functions as properties and then use them for functional-style algorithms.

The problem is I’m running into weird compiler errors/messages that I’m trying to figure out. I'm hoping someone here can give me some pointers on what these errors mean, and most likely what I’m doing wrong.

	  Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31) Target: x86_64-apple-macosx10.9

// =====================================================================
class Gen<InputKeyType, InputValueType, OutputKeyType, OutputValueType> {
    typealias func1Type = (numberRecords:InputKeyType, userRecord: InputValueType ) -> (OutputKeyType, OutputValueType)

    var afunc: func1Type

    init( inFunc: func1Type ) {
        afunc = inFunc
    }
}

var g: Gen<Int, (Int, Int), Int, (Int, Int)> =
    Gen( inFunc: { (numberRecords: Int, userRecord: (Int, Int)) -> (Int, (Int, Int)) in
                var b: Int = numberRecords
                var (age, numFriends) = userRecord
                print( (age), (numFriends) )
                return (age, (numFriends, 1))
            }
    )
// =====================================================================

What I get as output from the Swift compiler are these confusing messages. I included some print statements that hopefully gives some more info about what’s happening.

g: Gen<Int, (Int, Int), Int, (Int, Int)> = {
  afunc = 0x00000001012024d0 $__lldb_expr7`partial apply forwarder for reabstraction thunk helper from @callee_owned (@unowned Swift.Int, @unowned Swift.Int, @unowned Swift.Int) -> (@unowned (Swift.Int, (Swift.Int, Swift.Int))) to @callee_owned (@in Swift.Int, @in (Swift.Int, Swift.Int)) -> (@out (Swift.Int, (Swift.Int, Swift.Int))) at repl6.swift
}

print( (g) )
Gen<Swift.Int, (Swift.Int, Swift.Int), Swift.Int, (Swift.Int, Swift.Int)>

print( (g.aFunc) )
repl.swift:48:9: error: value of type 'Gen<Int, (Int, Int), Int, (Int, Int)>' has no member 'aFunc'
        ^ ~~~~~

There’s a good chance I’m doing something wrong but I don’t know how to figure out what that problem is. Any ideas?

Thanks.

Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160615/d280a929/attachment.html>


More information about the swift-users mailing list