Jac in a Flash#
This mini tutorial uses a single toy program to highlight the major pieces of the Jac language. We start with a small Python game and gradually evolve it into a fully object‑spatial Jac implementation. Each iteration introduces a new Jac feature while keeping the overall behaviour identical.
Step 0 – The Python version#
Our starting point is a regular Python program that implements a simple "guess the number" game. The player has several attempts to guess a randomly generated number.
Step 1 – A direct Jac translation#
guess_game1.jac
mirrors the Python code almost line for line. Classes are
declared with obj
and methods with def
. Statements end with a semicolon and
the parent initializer is invoked via super.init
. Program execution happens
inside a with entry { ... }
block, which replaces Python's
if __name__ == "__main__":
section. This step shows how familiar Python
concepts map directly to Jac syntax.
Step 2 – Declaring fields with has
#
The second version moves attribute definitions into the class body using the
has
keyword. Fields may specify types and default values directly on the
declaration. Methods that take no parameters omit parentheses in their
signature, making the object definition concise.
Step 3 – Separating implementation with impl
#
The fourth version splits object declarations from their implementations using
impl
. The object lists method signatures (def init;
, override def play;
),
and the actual bodies are provided later in impl Class.method
blocks. This
separation keeps the interface clean and helps organise larger codebases.
Step 4 – Walking the graph#
Finally guess_game4.jac
re‑imagines the game using Jac's object‑spatial
architecture. A walker
visits a chain of turn
nodes created with ++>
edges. The walker moves with visit [-->]
and stops via disengage
when the
guess is correct. The game is launched by spawn
ing the walker at root
.
This example shows how conventional logic can become graph traversal.
Step 5 – Scale Agnostic Approach#
The fifth version demonstrates Jac's scale-agnostic design. The same code that runs locally can seamlessly scale to cloud deployment without modification. By running the command jac serve filename.jac
, the walkers become API endpoints that can be called via HTTP requests. This shows how Jac applications are inherently cloud-ready.
Step 6 – AI-Enhanced Gameplay with MTLLM#
The final version integrates AI capabilities using MTLLM (Meaning Typed LLM). Instead of simple "too high" or "too low" responses, the game now provides intelligent, context-aware hints generated by an LLM. This demonstrates how easily AI can be woven into Jac applications to create more engaging user experiences.
Happy code deconstructing!