#1036 Serialization of const fields

brian Tue 23 Mar 2010

As one last component of using it-blocks for runtime setting of const fields, I'm going to change the semantics of serialization slightly.

If you have a class with const fields, then you must declare a make constructor that takes an optional it-block. If the deserializer encounters any const fields then it uses Field.makeSetFunc to pass the fields into the constructor.

This allows construction to be completely controlled by the constructor even during deserialization. I think after this change we'll have completely solved the initialization and validation of setting const fields (a design process that has spanned almost a year and half).

This will also tie in nicely to the null checking feature #832.

This topic has been touched on in various threads previously such as #1005.

brian Tue 23 Mar 2010

Promoted to ticket #1036 and assigned to brian

lbertrand Tue 23 Mar 2010

Another great news for today... Serialization being fully incorporated in the construction mechanism is indeed a more than welcome evolution of Fantom.

jodastephen Wed 24 Mar 2010

Cool!

brian Wed 24 Mar 2010

Ticket resolved in 1.0.52

The deserialization code now supports a constructor which takes an it-block. This allows you to validate your objects after deserialization. This is something that seems pretty powerful that I've never really seen other languages tackle well. It seems to prove out the idea of using it-blocks as a way to cleanly integrate OO validation and const/immutability.

Updated docs:

A serializable object must support a make constructor which either takes no parameters or takes an it-block. If the constructor takes an it-block then the field values are passed in using Field.makeSetFunc and the object is given a chance to perform validation. For example:

@Serializable
const class Rec
{
  new make(|This| f)
  {
    f(this)
    if (id <= 0) throw ArgErr("id <= 0")
  }
  const Int id
}

"Rec { id = 3 }".in.readObj  // ok
"Rec {}".in.readObj          // throws ArgErr

katox Wed 24 Mar 2010

It really makes non-null objects even more useful, great work.

Login or Signup to reply.