class Main{
static Void main(){
obj:=TestObj{list=[,]}
sb:=StrBuf()
sb.out.writeObj(obj)
echo(sb)//there is ok
nobj:=sb.toStr.in.readObj
}
}
@Serializable
class TestObj{
Str[]? list
}
Please modify list=[,] to list=Str[,]. It should work. The modified code is given below:
class Main{
static Void main(){
obj:=TestObj{list=Str[,]}
sb:=StrBuf()
sb.out.writeObj(obj)
echo(sb)//there is ok
nobj:=sb.toStr.in.readObj
}
}
@Serializable
class TestObj{
Str[]? list
}
go4Mon 25 Oct 2010
Thank you. I'am confused there is no error until serialize
brianMon 25 Oct 2010
Okay first off, it may just be terminology but I think it is important to clarify: there is no type inference on field assignment except in a field initializer:
Str[] f := [,] // f is Str[]
instance.f = [,] // f is Obj?[]
The latter is not type inference, it is auto-casting in that we let you upcast an Obj?[] to a Str[]. It is important distinction because if you call typeof on the list it will be Obj?[], not Str[]. This is similar to Java in that you can downcast an arry, but the getClass() will still report the declaration type.
That said, I believe there was a bug in the serialization code. Actually it wasn't a bug so much as poorly chosen semantics. I originally maintained the list/map type if it was different from the declared field type. That is not the best choice for various reasons since it doesn't combine well with auto casting (as this issue shows). So I think the correct semantics are to always encode a list/map field with the type inferred by the field type.
brianMon 25 Oct 2010
Renamed from List type inference to Serialization of List/Map fields always use field type
go4 Sun 24 Oct 2010
List type inference
I get a ObjDecoder error:
error:
I try on fansh:
rosarinjroy Mon 25 Oct 2010
Please modify
list=[,]
tolist=Str[,]
. It should work. The modified code is given below:go4 Mon 25 Oct 2010
Thank you. I'am confused there is no error until serialize
brian Mon 25 Oct 2010
Okay first off, it may just be terminology but I think it is important to clarify: there is no type inference on field assignment except in a field initializer:
The latter is not type inference, it is auto-casting in that we let you upcast an Obj?[] to a Str[]. It is important distinction because if you call typeof on the list it will be Obj?[], not Str[]. This is similar to Java in that you can downcast an arry, but the getClass() will still report the declaration type.
That said, I believe there was a bug in the serialization code. Actually it wasn't a bug so much as poorly chosen semantics. I originally maintained the list/map type if it was different from the declared field type. That is not the best choice for various reasons since it doesn't combine well with auto casting (as this issue shows). So I think the correct semantics are to always encode a list/map field with the type inferred by the field type.
brian Mon 25 Oct 2010
Renamed from List type inference to Serialization of List/Map fields always use field type
brian Mon 25 Oct 2010
Promoted to ticket #1263 and assigned to brian
brian Mon 25 Oct 2010
Ticket resolved in 1.0.56
Pushed a fix - changeset