Samples Hands-on code

Code Examples

Klyn distributions include a samples directory with runnable examples. Use these programs to explore the standard library, the terminal GUI layer, database access, and larger application structure before starting your own project.

Where the Examples Live

From a source checkout, examples are available in the repository-level samples directory. Packaged Linux installations also install them under the Klyn home directory.

cd samples
klyn gui/Calculator.kn

When an example spans several files, launch its main file from the sample directory or use the path shown below. Klyn will resolve dependencies through its normal class-loading rules.

Available Examples

Examples are grouped with the same high-level categories and order as the tutorial sidebar. Each card tag shows the category it belongs to.

Core Syntax

Hello World Countdown

Core Syntax

A first Klyn program built as a launch countdown. It introduces an explicitly typed variable, a while loop, decrementing, and console output before lift-off.

klyn samples/HelloWorld.kn

MegaGame

Core Syntax

A larger sample entry point intended to exercise application-level composition and reusable language-level building blocks.

klyn samples/MegaGame.kn

Types and OOP

POO

Types and OOP

A compact shape hierarchy showing classes, constructors, inheritance, overriding, and polymorphic calls in idiomatic Klyn.

klyn samples/POO.kn

File Handling

JSON Game Configuration

File Handling

Stores typed game settings in a HashSet, writes them as a formatted JSON array beside the sample, then reloads the file and reconstructs the set.

klyn samples/io/JsonConfig.kn

Export XML

File Handling

Exports the people and monthly activity data from the XLSX example as a structured DOM document beside the sample, then parses the result back for verification.

klyn samples/io/ExportXML.kn

Export XLSX

File Handling

Creates an XLSX workbook with a people directory, a second activity worksheet, and a line chart based on the exported monthly values.

klyn samples/io/ExportXLSX.kn

Databases & Entities

SQL Login

Databases & Entities

A console MariaDB login flow using DriverManager, try-with-resources, and a prepared update statement.

klyn samples/sql/Login.kn

Entities and KQL

Databases & Entities

Builds a MariaDB article catalog with the Klyn Entity Engine, creates its schema, persists demonstration entities transactionally, and executes typed KQL price queries from a desktop GUI. A configured MariaDB database is required.

klyn samples/sql/Entities.kn

GUI

Calculator

GUI

A compact graphical application showing event handling, widgets, and simple interaction patterns.

klyn samples/gui/Calculator.kn

Speed Demo Window

GUI

A custom speedometer widget built with Canvas, gradients, antialiased painting, sliders, and signal-property bindings.

klyn samples/gui/SpeedDemoWindow.kn

Clock Sample

GUI

An analog clock implemented as a custom Canvas widget. It combines gradients, antialiased painting, time-based geometry, and a GUI Timer that repaints once per second.

klyn samples/gui/ClockSample.kn

Slider Sample

GUI

Shows slider variants: filled track, ticks, values, vertical orientation, and live value tooltip during pointer drag.

klyn samples/gui/SliderSample.kn

DatePicker Sample

GUI

Shows the standard calendar popup and the quickEditMode workflow where the user selects a year, a month, then a day.

klyn samples/gui/DatePickerSample.kn

File Dialogs

GUI

Demonstrates typed modal lightboxes for opening one or more files, choosing a save destination, and selecting a folder inside the application window.

klyn samples/gui/FileDialogsSample.kn

Font Dialog

GUI

Lists operating-system fonts in an embedded lightbox with per-family previews and applies the selected typed Font value to a widget.

klyn samples/gui/FontDialogSample.kn

Menu Bar Sample

GUI

Demonstrates a desktop menu bar with File, Edit, and Help menus, mnemonic labels, shortcuts, icons, separators, and typed action events.

klyn samples/gui/MenuBarSample.kn

Terminal Widget

GUI

Embeds a TerminalWidget in a desktop window, demonstrates cwd tracking, multi-tab terminals, and the compact singleTerminal mode.

klyn samples/gui/TerminalWidgetSample.kn

Browser Widget

GUI

Embeds a styled HTML document with asynchronous navigation, retained rendering, history, and deterministic DOM scripting.

klyn samples/gui/BrowserWidgetSample.kn

Login Form

GUI

A MariaDB-backed login window using FormLayout, password input, default-button behavior, buddies, and prepared statements.

klyn samples/gui/LoginForm.kn

Color Picker

GUI

Demonstrates the reusable ColorPicker, its RGB, HSV, HSL, color-wheel and spectrum modes, plus the standalone dialog and embedded lightbox hosts.

klyn samples/gui/ColorPickerSample.kn

Color Chooser

GUI

Shows Binding.twoWay between sliders and spin boxes, plus Binding.watch to repaint a live color preview.

klyn samples/gui/ColorChooser.kn

Color Chooser 2

GUI

Refactors the color editor into reusable color lines that expose a bindable value signal property, while keeping the live preview synchronized.

klyn samples/gui/ColorChooser2.kn

Database Explorer

GUI

A larger GUI sample with a modal connection lightbox, split views, a schema tree, closable tabs, live database schema discovery, editable table models, read-only views, and an SQL editor with schema-aware completion.

klyn samples/gui/dbexplorer/DatabaseExplorer.kn

Graphical Breakout

GUI

A resizable vector game built around one custom canvas, a fixed-step animation loop, direct mouse and keyboard input, four levels, brick effects, and falling bonuses.

klyn samples/gui/Breakout/Start.kn

TUI

Breakout

TUI

A multi-file terminal game. It is useful for understanding how a larger Klyn sample is split into several classes and views.

klyn samples/Breakout/Breakout.kn

Terminal Resize

TUI

Focuses on terminal resize behavior and is useful when validating layout and event-loop behavior on a new platform.

klyn samples/TerminalResize.kn

Advanced Topics

Structured Logging

Advanced Topics

Uses a generic class logger, structured fields, scoped context, and a timed span while a multi-handler writes the same events to the console and a file beside the sample. See the Logging tutorial for configuration and handler details.

klyn samples/logging/MoonMissionLogs.kn

Performance Benchmark

Advanced Topics

Runs equivalent Python, Klyn, and C++20 programs three times, compares cold and warm Klyn startup, and generates a self-contained HTML performance report.

klyn samples/perf/Benchmark.kn

LLM

Advanced Topics

Exercises the language-level LLM helpers. This sample normally requires provider configuration and valid credentials. See the LLM API and Agents tutorial for the underlying request model.

klyn samples/llm.kn

Financial

Advanced Topics

Shows an LLM-backed financial agent sample and is a good place to inspect how domain logic can be combined with annotated tools and the agent loop described in the LLM API and Agents tutorial.

klyn samples/Financial.kn
Next Step

Continue with Lexical Structure to understand the source rules used by these examples, or open API Reference while reading their imports.