does typealias information survive serialization?
# serialization
t
does typealias information survive serialization?
b
All references to typealias is replaced with actual aliased type at compilation which survives serialization
t
that's a little confising...
so since typealiass don't even surrive compliation, then they are erased by the time you even get to serlizaton...
yes?
a
Type alias do survive serialization. As @Big Chungus stated, a type alias is replaced by the compiler to its actual implementation
t
still very confusing.
b
Think of typealias as a note to the compiler that you want to use x type in all places where you use it
t
so will
thing is AliasType
return true or false after serialization? And does it depend on the code that defines the alias being identical ?
b
true
t
but only because AliasType is the same type that was serialized, or because the serialization took note of the type alias?
b
typealias MyMap = Map<String, String> val x: Map<String, String> = mapOf() val y: MyMap = x val test = x is MyMap // true
Does the above help?
t
no, because that's all done by the compiler.
b
In short, typealias is not an actual type, but rather a shortcut to refer to the backing type
So serializing and deserializing MyMap would actually serialize and deserialize Map<String, String>
t
if I un-serlize an object that has a type alias, but that alias isn't in my code, can I pragmatically figure that out?
or will that alias be generated, like any other custom type?
b
No, since typealias is not a type
t
so type alias don't survive.
if I have a custom type, if it doesn't exist when I try to de-serial, I get an error
but w/ type aliases it just defaults to the underlying type.
yes?
so type alias's are erased. yes?
b
Kinda
t
again... confused...
b
You can't erase what's not there in the first place
typealias is just a shortcut
t
exactly. it was errased by the compiler in the first place.
which is what I said 20 hours ago, but you seemed to imply that it somehow made it though serialization.
b
It did, since typealias == actual type
You can deserialize same json object to both since they're the exact same thing under the hood
t
but I'm not asking about the underlying type. I'm asking if the typealias survives. and the answer is a rounding "no".
b
Let's justb leave it at no for all intents and purposes.
Although if you deserialize json to MyMap you gst MyMap back
So that way one could say it did survive
t
en... no.. if I de-serialize in another language, I don't need to make a type for the typealias, unlike all the other types. The alias has been fully and totally erased. In no way is that surviving. Also in your case if I define MyMapp w/o the typealias, it works fine. Your definition of surviving is based on luck of things being setup correctly. That is not reliable or safe and no one should ever depend on that.
b
That's not what I meant... In order to deserialize a Map you need it defined (just like any other type). It just so happens that all languages usually have it in stdlib...
In any deserialization you need to have a backing type ready, regardless of the language
Anyways, I give up. Clearly we're talking in different languages here 😀 let's leave it at that
😀 1
p
TypeAlias is just an alias it's not compiled it's just replaced
typealias Name = String typealias Address = String "Peter" is Name is equivalent to: "Peter" is String "Peter" is Address is equivalent to: "Peter" is String There is nothing to survive here