Dataphor.org

Message Interpolation

Modified: 2009/06/30 14:01 by luxspes - Uncategorized
Message Interpolation is a concept borrowed from JavaServerFaces that makes it easy to include contextual information in a validation message. For example, you might want to write a type with a constraint like this:

create type Description like String { constraint LengthValid Length(value) <= 50 tags { DAE.SimpleMessage = "The Length of the string must be less than 50 characters." } } static tags { Storage.Length = "50" };



But, when the users sees the error message for that, he is going to have a question: "What is the actual size of the string"? "How many characters do I need to remove for it to fit?

If we had Message interpolation in Dataphor we could modify the the constraint like this:

create type Description like String { constraint LengthValid Length(value) <= Storage.Length tags { DAE.SimpleMessage = "The Length of the string is #{Length(value)} that is more than #{Storage.Length} characters. Please remove #{Length(value)-Storage.Length} characters" } } static tags { Storage.Length = "50" };

notice the #{ } they mark the places were the interpolation occurs, inside we can write simple expressions that will get mixed in to the string of the SimpleMessage (similar to what can be done with String.Format)

This has several advantages:

  • We inform the user by exactly how many characters he is violating the constraint
  • We inform the user the size of the string he wrote
  • The code for the constraint is no longer violating DRY: The maximum length for the string is written once an only once

ScrewTurn Wiki version 2.0.36. Some of the icons created by FamFamFam.