I think you're missing the underlying semantic problem here:
func transform(item: inout Position) {
item = Airplane()
}
var car = Car(x: 50)
var pos: Position = car
move(item: &pos) // this works, but 'pos' is now an Airplane
move(item: &car) // this doesn't work because 'car' has to stay a Car
Because generic uses `Car` instead of `Position` when running the code, so
there is no casting as `car as Position` as in your original code. The
`T:Position`
part restricts that the type conforms `Position`, but it won't use `Position`,
it uses the type.
Zhaoxin
On Sat, May 27, 2017 at 4:58 PM,
Thanks. Why generic function don not require memory allocate for inout
variable, even if it is protocol type?
>Пятница, 26 мая 2017, 19:35 +03:00 от Guillaume Lessard
>:
>
>In your example, the compiler needs a parameter of type Position. Car is a
>type of Position, but they are not interchang
Try this.
protocol Position {
var x: Double { get set }
}
struct Car: Position {
var x: Double
}
func move(item: inout T) where T:Position {
item.x += 1
}
var car = Car(x: 50)
move(item: &car)
car.x // 51
Zhaoxin
On Sat, May 27, 2017 at 12:35 AM, Guillaume Lessard vi
In your example, the compiler needs a parameter of type Position. Car is a type
of Position, but they are not interchangeable. See below:
> On May 26, 2017, at 00:33, Седых Александр via swift-users
> wrote:
>
> protocol Position {
> var x: Double { getset }
> }
>
> struct Car: Position
Hello. I have protocol witch allow type to handle it's position and function
who handle this position and compiler get me error. Please, tell, Why I can not
use protocol name in inout function? The example here.
protocol Position {
var x: Double { get set }
}
struct Car: Position {