theturnout / black-tie Goto Github PK
View Code? Open in Web Editor NEWA formal approach to semantic validation
License: MIT License
A formal approach to semantic validation
License: MIT License
We could do OCL based Post and Preconditionals, but let's see how hard it would be to do native preconditionals.
OCL conditionals operate with contextual access to a method's arguments
, result
, and for postconditional, values of the arguments prior to execution, pre@
.
To replicate this using TypeScript decorators, we would need the same access to the arguments as OCL does. This can be (poorly) replicated using a custom generic type that extracts the arguments of a function as a tuple list.
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never;
We can then define a decorator function like so...
export function Preconditional<TArgs>(expr: (args: TArgs) => boolean) {...}
We can then define a Preconditional on a method
export class Student {
@Preconditional<ArgumentTypes<typeof Student.addCourse>>((args) => args[0] > 0)
public static addCourse(courseId: number) {...}
}
The above example works for Preconditions on the arguments, but what about the larger environment? Postconditionals are often used to validate not only the return value, but side effects that the function generated, such as a change to an instance field.
Some notes:
The above sections prove that preconditions and postconditionals can be implemented without changes. However, the required boilerplate is highly verbose and prone to error. A solution would be for the typescript language service to pass two generic parameters whenever it sees a generic function. This would bake in this functionality. But perhaps we could be more cautious, and just define this
as the target type of the decorator. This would result in much simpler definitions, such as
@Preconditional<ArgumentTypes<this>>((args) => args[0].courseNumber > 5)
So in the above example, this
would be logically equivalent to typeof Student.addCourse
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.