You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are a number of cases where Sango clients would benefit from richer typing of output, especially when it comes to finite groups of elements that act as identifiers. At the moment, when Sango outputs native types, it always uses structs grouping constants of trivial type for these. For example, if I had three valid identifiers that act as keys in a configuration dictionary, I may see:
The developer is forced to perform runtime validity checks on values from these structs, e.g., when passing them to a method.
No functionality can be added, via subclassing (java) or retroactive modeling (swift). Not all languages support this, so it's not a disadvantage for everyone, but it eliminates taking advantage of very useful features of languages that do.
If Sango were able to output enumerations for these kinds of constants, some advantages could immediately be had:
Validity checks could be performed at compile-time
Using enumerations, runtime assertions could be delegated to the compiler. For example, using the current output:
publicstructConstants{publicstructMyAttributeIds{staticletid1=1000staticletid2=1002staticletid3=1003}}varattrs:[Int:Any]=[:]attrs[666]="something really important" // No error, but semantically incorrect
func set(value:Any, for attributeId:Int){assert([Constants.MyAttributeIds.id1,Constants.MyAttributeIds.id2,Constants.MyAttributeIds.id3,].contains(attributeId),"Invalid attribute id")attrs[attributeId]= value
}set("something really important", for:666) // Caught by assertion at runtime
Instead, if Sango supported enumerations, the following would be possible:
publicstructConstants{publicenumMyAttribute{case id1 =1000case id2 =1002case id3 =1003}}varattrs:[Constants.MyAttribute:Any]=[:]attrs[666]="foo" // impossible, compile-time error, pointed out in source editor.
func set(value:Any, for attribute:Constants.MyAttribute){
// No runtime checks required
attrs[attributeId]= value
}letmaybeMyAttr=Constants.MyAttribute(1000) // myAttr is .some(.id1)
letmaybeMyAttr2=Constants.MyAttribute(6666) // myAttr is .none
Retroactive modeling can be performed
Functionality can be added to enumeration cases. This can be done with strings and ints too, but those are available globally. Adding to enumeration cases limits their scope. For example:
publicstructConstants{publicenumMyAttribute{case id1 =1000case id2 =1002case id3 =1003}}extensionConstants.MyAttribute:CustomDebugStringConvertible{
switch self {varname:String
switch self{case.id1: name ="Attribute 1"case.id2: name ="Attribute 2"case.id3: name ="Attribute 3"}
return "\(name) (\(rawValue))"}}
In sum, this makes Sango output play better with developers' IDEs, and allows them to take better advantage of their compiler's facilities in particular, helping to streamline development and reduce the changes of human error.
The text was updated successfully, but these errors were encountered:
There are a number of cases where Sango clients would benefit from richer typing of output, especially when it comes to finite groups of elements that act as identifiers. At the moment, when Sango outputs native types, it always uses structs grouping constants of trivial type for these. For example, if I had three valid identifiers that act as keys in a configuration dictionary, I may see:
There are a couple of disadvantages here:
The developer is forced to perform runtime validity checks on values from these structs, e.g., when passing them to a method.
No functionality can be added, via subclassing (java) or retroactive modeling (swift). Not all languages support this, so it's not a disadvantage for everyone, but it eliminates taking advantage of very useful features of languages that do.
If Sango were able to output enumerations for these kinds of constants, some advantages could immediately be had:
Validity checks could be performed at compile-time
Using enumerations, runtime assertions could be delegated to the compiler. For example, using the current output:
Instead, if Sango supported enumerations, the following would be possible:
Retroactive modeling can be performed
Functionality can be added to enumeration cases. This can be done with strings and ints too, but those are available globally. Adding to enumeration cases limits their scope. For example:
In sum, this makes Sango output play better with developers' IDEs, and allows them to take better advantage of their compiler's facilities in particular, helping to streamline development and reduce the changes of human error.
The text was updated successfully, but these errors were encountered: