New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-8148] inout is not working with subclass object throws Cannot pass immutable value #3667
Comments
Hi Prashant, I’m pretty sure Swift is supposed to behave this way. The problem is, `update(_:withGeometry:type🙂` could assign a plain old `SCNNode` to that `inout` parameter, but not all `SCNNode`s can be put into `plane`—only `plane`s can. So Swift rejects this as a type error. There are three ways you could solve this. The first is to explicitly give `plane` the type `SCNNode`, like this: `var plane: SCNNode = Plane(with: planeAnchor)`. The second is to make the parameter not be `inout`—if you’re just setting properties on the `Plane`, it probably doesn’t need to be `inout`. The other is to change the method from `func update(: inout SCNNode, withGeometry: ...)` to `func update<T: SCNNode>(: inout T, withGeometry: ...)`; that means `update(_withGeometry:type🙂` will only be able to replace `plane` with another `Plane`, not with a different kind of `SCNNode`. Hope this helps! |
Comment by Prashant Tukadiya (JIRA) Hi Brent, Thanks for that explanation I can't declare it with `var plane: SCNNode` then after I can't use inner properties of that subclass (need to type cast ), Yes I know about pass by reference that you mentioned in your solution so with that I do not need to add inout. But I am playing with inout 😛 I’m pretty sure Swift is supposed to behave this way :- This is strange to me. I was thinking that object is still representing SCNNode class so it should work as inout. But as you mentioned inout required pure types means it not allow subclass to be passed in Please correct me if I am wrong. Again thanks |
Comment by Prashant Tukadiya (JIRA) it is default swift behaviour |
It’s not that the value can’t be a subclass, it’s that the variable you put it in must have exactly the same type as the inout parameter. Another way to address this would be to write something like: let plane = Plane(…) Maybe the error message could suggest this; Swift is right to diagnose this code as incorrect, that doesn’t mean the error is as helpful as it should be. |
Environment
Using XCODE 9.3
Swift 4.1
ios 11 +
Additional Detail from JIRA
md5: 1fcfb7f12ae9da038b4cb352aa28c3bc
duplicates:
Issue Description:
I have following code.
Now Check
is a subclass of `SCNNode` when try to pass it in `update` method which has `SCNPhysicsBody` applied to that node
it throws
However for simple object `planeNode` it is not showing any error
The text was updated successfully, but these errors were encountered: