Saturday 15 June 2013

Swift type erasure / reification behavior? -



Swift type erasure / reification behavior? -

does swift back upwards type reification? cannot snippet work properly:

import swift protocol p { var name:string { set } } class : p { var name:string = "a" } class b : p { var name:string = "b" } class c : p { var name:string = "c" } func inspect(var p:p) { println("should c: \(p.name)") } func inspect(var p:a) { println("a: \(p.name)") } func inspect(var p:b) { println("b: \(p.name)") } func failstoretaintype<t:p>(t:t) { print("inspecting \(t.name): ") // allow t = t.dynamictype inspect(t) } inspect(a()) inspect(b()) inspect(c()) failstoretaintype(a()) // expect a, c failstoretaintype(b()) // expect b, c failstoretaintype(c()) // expect c, c

am missing something?

for it's worth, have seen 2 other posts on same general topic seem more focused on building, , can solved creating new instance of same type:

swift generics not instantiate generic when using inheritance swift generics not preserving type

as far know, need actively cast instances in generic functions specific type:

func failstoretaintype<t:p>(t:t) { print("inspecting \(t.name): ") switch t { case allow t a: inspect(t) case allow t b: inspect(t) default: inspect(t) } } // inspecting a: a: // inspecting b: b: b // inspecting c: should c: c

swift

No comments:

Post a Comment