Unable to use weak variables in structs

(wdb) #1

IDE: Version 10.0.0.2397 (develop) built on bajor, 20190419-153353. Commit 6f2fbf4.
Version: Version 10.0.0.2397 (develop) built on bajor, 20190419-153353. Commit 6f2fbf4.
Target (If relevant): iOS
Description:
Unable to use a public weak variable with a struct. I am trying to create a tree hierarchy with the type TreeNode type of which I would like to make the parent a weak reference. Typically, you would use a struct definition like:

public class TreeNode<T> {
    public var value: T

    public weak var parent: TreeNode?
    public var children = [TreeNode<T>]()

    public init(value: T) {
        self.value = value
    }

    public func addChild(_ node: TreeNode<T>) {
        children.append(node)
        node.parent = self
    }
}

If I add this file Node.swift in a SharedProject which is referred to by iOS/Toffee project and then try to compile my project

Expected Behavior:
Ability to compile the TreeNode-class without compiler errors

Actual Behavior:
Getting the error:
E: Flag “Weak” not allowed on this type [/Users/x/Development/Projects/x/x/version-3/config-app/SharedProject/Models/Node.swift (4)]

Steps:

  1. Create new project
  2. Add the above TreeNode-definition as Node.swift to this project
  3. Try to compile the project
  4. Notice compiler error

I am not sure if this is expected behaviour. I have checked the limitations documentation but couldn’t find a reference to it. Also the forum doesn’t give much results when searching for weak var

(RemObjects) #2

Thanks, logged as bugs://82444

Using typealias throws an internal error on compiler
(wdb) #3

Currently, I temporary removed the weak bit in the code

(RemObjects) #4

bugs://82444 got closed with status fixed.

1 Like
(wdb) #5

I can confirm it’s working in the 10.0.0.2398 build! Thanks :smiley:

1 Like