6

I'm having an issue when using a Generic as the type constraint on a property. Here is a very simple example:

import UIKit

class TSSignal<MessageType> {

    var message: MessageType?

    init() {
    }

}

In Xcode 6 Beta (6A215l) this will not compile. It fails with the following error at the bottom:

TSSignal.swift:13:9: error: unimplemented IR generation feature non-fixed class layout var message: MessageType? ^ LLVM ERROR: unimplemented IRGen feature! non-fixed class layout Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc‌​hain/usr/bin/swift failed with exit code 

But, if I remove the line var message: MessageType? it will build fine. Any ideas? Thanks.

Edit - changed code and error to reflect current status of issue

Edit - related: Swift compile error when subclassing NSObject and using generics

Update (6/18/14) - The issue still persists as of Xcode 6 - Beta 2

Update (7/25/14) - The issue still persists as of Xcode 6 - Beta 4 (thanks @Ralfonso, and I verified as well)

Update (8/4/14) - The issue is FIXED as of Xcode 6 - Beta 5!

Community
  • 1
  • 1
michaelavila
  • 1,059
  • 7
  • 20

3 Answers3

2

There is a workaround without type erasure (works as of Xcode6-Beta2):

import UIKit

class TSSignal<MessageType> {
    var _message: [MessageType] = []

    func getMessage() -> MessageType? {
        if _message.count > 0 {
            return _message[0]
        } else {
            return nil
        }
    }

    func setMessage(maybeMessage: MessageType?) {
        if let message = maybeMessage {
            _message = [message]
        } else {
            _message = []
        }
    }

    init() {
    }
}
Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
1

EDIT EDIT:

This is definitely a bug in the compiler.

I tried to 'outsmart' the compiler by using the following:

class TSSignal<TMessage>
{
    var messageType : Optional<TMessage> = nil

    init() { }
}

Same issue.

Erik
  • 12,730
  • 5
  • 36
  • 42
  • Doing this changed the error to this: TSSignal.swift:13:9: error: unimplemented IR generation feature non-fixed class layout var message: MessageType? ^ LLVM ERROR: unimplemented IRGen feature! non-fixed class layout Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1 – michaelavila Jun 13 '14 at 02:58
  • I'm confused by this because MessageType is not actually a real type. It's the name I'm giving to the generic. Are you saying that is the problem? – michaelavila Jun 13 '14 at 03:19
  • Sorry! I'm so used to seeing the `T` prefix that I wrongly assumed you were trying to use a defined type as the generic! Will revert my edit. – Erik Jun 13 '14 at 03:24
  • @BryanChen True, though it is still the answer from what it seems. – Erik Jun 13 '14 at 03:28
0

This is NOT an "answer" (it is my own question), but I thought it worth noting what I did in this case to move passed this situation, for the time being.

import UIKit
class TSSignal {
    var message: AnyObject?
    init() {
    }
}

Lame, but I am sure it's only temporary.

Termininja
  • 6,620
  • 12
  • 48
  • 49
michaelavila
  • 1,059
  • 7
  • 20