So I have a View and I wanna make a stored property inside it and add a modifier to it, but when I try to do this:
var heyButton2 = Text("T").background(.red)
I get this error: Property definition has inferred type 'some View', involving the 'some' return type of another declaration
Now if I make heyButton2 a computed property everything works as expected but could someone explain why this isn't okay if it's a stored property? Do I just not understand how exactly view modifiers work? I'm creating a Text view and adding a background modifier to it I don't see exactly why this would cause any problems.
Also another related question, if I remove the modifier from heyButton2, so just
var heyButton2 = Text("T")
I can just put 10 heyButton2's in a VStack or anything and each one will be a "different"? button? It just seems a little weird to me because it's just one variable containing a View but I can contain put it in any View ten times or something and they'll all be different, is it fair to assumt that each time I put a heyButton2 in a VStack it's just creating a copy of the button? Like the original heyButton2 is a variable containing one View but each one I put into a VStack is just a copy, would that be correct?
CodePudding user response:
This is not related to views specifically; you probably should rephrase the question. You just can't have type inference for opaque types. E.g.
struct S {
var bool = someExpressibleByBooleanLiteral
}
var someExpressibleByBooleanLiteral: some ExpressibleByBooleanLiteral { true }
Fix it by explicitly typing with some Protocol.
This does not require a computer property, as you suggested. That’s simply an option.
CodePudding user response:
I think that answer would help you. Actually it's weird but simple, if you look definition of "background" it has a return type some view. Therefore return type must be 'view' but your variable type isn't that. definition