Trouble with function properties


(Doug Hill) #1

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


(Saagar Jha) #2

`Gen` defines `afunc`, but you’re trying to access `aFunc`.

···

On Wed, Jun 15, 2016 at 6:22 PM Doug Hill via swift-users < swift-users@swift.org> wrote:

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
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha


(Doug Hill) #3

Thank you Saagar for looking over this code for me. I guess I got really caught up in the weird diagnostic message and assumed things weren’t working.
Now I see that ‘afunc’ works as I expected:

60> g.afunc(numberRecords:1,userRecord:(13,9))
13 9
$R3: (Int, (Int, Int)) = {
  0 = 13
  1 = {
    0 = 9
    1 = 1
  }
}

Although I would be interested in why the compiler spits out the first message in the first place (e.g. the message with the strange description of afunc).

Looking forward to see what I can do with Swift,

Doug Hill

···

On Jun 15, 2016, at 6:49 PM, Saagar Jha <saagarjha28@gmail.com> wrote:

`Gen` defines `afunc`, but you’re trying to access `aFunc`.

On Wed, Jun 15, 2016 at 6:22 PM Doug Hill via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
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
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users
--
-Saagar Jha


(Saagar Jha) #4

That’s how the Swift interpreter prints out closures. Try let testClosure =
{} in the REPL and you’ll get a similar result, except with different
arguments and return value due to {} being () -> ().

···

On Wed, Jun 15, 2016 at 8:13 PM Doug Hill via swift-users < swift-users@swift.org> wrote:

Thank you Saagar for looking over this code for me. I guess I got really
caught up in the weird diagnostic message and assumed things weren’t
working.
Now I see that ‘afunc’ works as I expected:

60> g.afunc(numberRecords:1,userRecord:(13,9))
13 9
$R3: (Int, (Int, Int)) = {
  0 = 13
  1 = {
    0 = 9
    1 = 1
  }
}

Although I would be interested in why the compiler spits out the first
message in the first place (e.g. the message with the strange description
of afunc).

Looking forward to see what I can do with Swift,

Doug Hill

On Jun 15, 2016, at 6:49 PM, Saagar Jha <saagarjha28@gmail.com> wrote:

`Gen` defines `afunc`, but you’re trying to access `aFunc`.

On Wed, Jun 15, 2016 at 6:22 PM Doug Hill via swift-users < > swift-users@swift.org> wrote:

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
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha

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

--
-Saagar Jha