The aim of this project is to simulate Protocol.
The check is at runtime and not at compilation time, too bad.
First we have the Protocol
class. All others protocols classes must extends it.
Class extends Protocol
Then each protocol define the function and property
Class constructor
Super()
This.addProperty("name"; Is text)
This.addFunction("getRepository")
A variable ps
represent the protocol store.
You must instanciate it one time and fill it with all protocols.
To do so just instantiate one times them.
ps:=New object()
cs.ServiceProtocol.new() // auto register into ps
cs.MySecondProtocol.new()
then you can access protocol with ps
: ps.ServiceProtocol
or use psInit
which instanciate all protocols found in class store cs
.
You have an instance and want to know if all properties and functions match a protocol.
$instance:=cs.Github.new()
You could ask protocol for that:
If(ps.ServiceProtocol.isInstance($instance))
// do something
End if
You could also assert if you code need to be called by a correct instance type
If (ps.ServiceProtocol.assertedInstance($instance))
// do something
End if
💡 You could do
ASSERT(ps.ServiceProtocol.isInstance(This))
in your class object contructor to check immediately that it follow the protocol rules
Just by using JSON Validate
with schema validation. The protocol is the schema.