Introduction to K3

K3 is a modern, high-performance programming language designed for building everything from system utilities to web applications. It combines the speed of compiled languages with the ease of use of scripting languages.

K3 was created with several key principles in mind:

  • Performance: K3 is designed to be fast, with performance comparable to C++ and Rust.
  • Safety: Memory safety without garbage collection through an ownership model and automatic reference counting.
  • Expressiveness: Clean, readable syntax that makes code easy to write and maintain.
  • Versatility: Suitable for a wide range of applications, from low-level system programming to high-level web development.

Note: K3 is under active development. While the core language is stable, some advanced features may change in future releases.

Installation

K3 can be installed on Windows, macOS, and Linux. Choose the installation method that works best for your operating system.

Windows

  1. Download the Windows installer (.exe) from the download page.
  2. Run the installer and follow the prompts.
  3. Open Command Prompt and verify the installation:
k3 --version

macOS

  1. Download the macOS package (.pkg) from the download page.
  2. Open the package and follow the installation wizard.
  3. Open Terminal and verify the installation:
k3 --version

Alternatively, you can use Homebrew:

brew install k3

Linux

For Ubuntu/Debian:

sudo apt-get update
sudo apt-get install k3

For Fedora:

sudo dnf install k3

For Arch Linux:

sudo pacman -S k3

Alternatively, you can download and install the binary manually:

  1. Download the Linux tarball (.tar.gz) from the download page.
  2. Extract it:
tar -xzf k3-1.0.0-linux-x64.tar.gz
  1. Move to a directory in your PATH:
sudo mv k3-1.0.0 /usr/local/k3
sudo ln -s /usr/local/k3/bin/k3 /usr/local/bin/k3
  1. Verify the installation:
k3 --version

Hello World

Let's start with the traditional "Hello, World!" program in K3:

// hello.k3
fn main() {
    println("Hello, World!")
}

Save this code to a file named hello.k3, then compile and run it:

k3 build hello.k3
./hello

Alternatively, you can use the run command to compile and execute in one step:

k3 run hello.k3

You should see the following output:

Hello, World!

Understanding the Code

Let's break down the Hello World program:

  • // hello.k3 - This is a comment indicating the filename.
  • fn main() { ... } - This defines the main function, which is the entry point of the program.
  • println("Hello, World!") - This calls the println function to print the string "Hello, World!" to the console.

A More Interactive Example

Let's create a slightly more interactive program that asks for the user's name and greets them:

// greeting.k3
fn main() {
    println("What is your name?")
    
    let name = readLine()
    
    if name.trim() == "" {
        println("Hello, stranger!")
    } else {
        println("Hello, {}!", name.trim())
    }
}

Run this program with:

k3 run greeting.k3

Tip: K3 uses string interpolation with curly braces {} to insert values into strings, similar to Rust's format strings.

Editor Setup

K3 has excellent editor support for popular code editors and IDEs. Here's how to set up K3 in your favorite editor:

Visual Studio Code

  1. Install the K3 extension from the VS Code marketplace.
  2. Open a K3 file or create a new one with a .k3 extension.
  3. The extension provides syntax highlighting, code completion, error checking, and more.

JetBrains IDEs (IntelliJ IDEA, WebStorm, etc.)

  1. Install the K3 plugin from the JetBrains marketplace.
  2. Restart your IDE.
  3. Open a K3 file or create a new one with a .k3 extension.

Vim/Neovim

For Vim or Neovim, you can use the K3 plugin:

# For Vim with vim-plug
Plug 'k3-lang/vim-k3'

# For Neovim with packer.nvim
use 'k3-lang/vim-k3'

Emacs

For Emacs, you can use the K3 mode:

;; Using use-package
(use-package k3-mode
  :ensure t)

Sublime Text

  1. Install Package Control if you haven't already.
  2. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
  3. Select "Package Control: Install Package".
  4. Search for "K3 Language" and install it.

Note: All these editor extensions provide syntax highlighting, code completion, and error checking. Some also provide debugging support, code formatting, and more advanced features.

Syntax Overview

K3's syntax is designed to be clean, readable, and familiar to developers coming from languages like Rust, TypeScript, and Swift. This section provides a quick overview of K3's syntax.

Comments

// This is a single-line comment

/* This is a
   multi-line comment */

/// Documentation comment for the following item
fn documented_function() {
    // Function body
}

Basic Structure

// Import statement
import Math from "k3/math"

// Function definition
fn add(a: int, b: int) -> int {
    return a + b
}

// Main function (entry point)
fn main() {
    let result = add(5, 3)
    println("5 + 3 = {}", result)
    
    // Using an imported function
    let sqrt = Math.sqrt(16)
    println("Square root of 16 is {}", sqrt)
}

Variables and Constants

// Variable declaration with type inference
let name = "Alice"

// Variable declaration with explicit type
let age: int = 30

// Constants
const PI: f64 = 3.14159
const MAX_USERS: int = 1000

// Mutability
let mut counter = 0
counter += 1  // OK, counter is mutable

name = "Bob"  // Error, name is immutable

Control Flow

// If-else statement
if age >= 18 {
    println("Adult")
} else if age >= 13 {
    println("Teenager")
} else {
    println("Child")
}

// For loop
for i in 0..10 {
    println("{}", i)
}

// While loop
let mut i = 0
while i < 10 {
    println("{}", i)
    i += 1
}

// Match statement (pattern matching)
match status {
    Status.Success => println("Operation succeeded"),
    Status.Error(err) => println("Error: {}", err),
    Status.Pending => println("Operation pending"),
    _ => println("Unknown status")
}

Tip: K3's syntax is designed to be familiar to developers coming from languages like Rust, TypeScript, and Swift, making it easy to learn if you already know one of these languages.

K3 Documentation - Fast, Safe, and Expressive Programming Language

Introduction to K3

K3 is a modern, high-performance programming language designed for building everything from system utilities to web applications. It combines the speed of compiled languages with the ease of use of scripting languages.

K3 was created with several key principles in mind:

  • Performance: K3 is designed to be fast, with performance comparable to C++ and Rust.
  • Safety: Memory safety without garbage collection through an ownership model and automatic reference counting.
  • Expressiveness: Clean, readable syntax that makes code easy to write and maintain.
  • Versatility: Suitable for a wide range of applications, from low-level system programming to high-level web development.

Note: K3 is under active development. While the core language is stable, some advanced features may change in future releases.

Installation

K3 can be installed on Windows, macOS, and Linux. Choose the installation method that works best for your operating system.

Windows

  1. Download the Windows installer (.exe) from the download page.
  2. Run the installer and follow the prompts.
  3. Open Command Prompt and verify the installation:
k3 --version

macOS

  1. Download the macOS package (.pkg) from the download page.
  2. Open the package and follow the installation wizard.
  3. Open Terminal and verify the installation:
k3 --version

Alternatively, you can use Homebrew:

brew install k3

Linux

For Ubuntu/Debian:

sudo apt-get update
sudo apt-get install k3

For Fedora:

sudo dnf install k3

For Arch Linux:

sudo pacman -S k3

Alternatively, you can download and install the binary manually:

  1. Download the Linux tarball (.tar.gz) from the download page.
  2. Extract it:
tar -xzf k3-1.0.0-linux-x64.tar.gz
  1. Move to a directory in your PATH:
sudo mv k3-1.0.0 /usr/local/k3
sudo ln -s /usr/local/k3/bin/k3 /usr/local/bin/k3
  1. Verify the installation:
k3 --version

Hello World

Let's start with the traditional "Hello, World!" program in K3:

// hello.k3
fn main() {
    println("Hello, World!")
}

Save this code to a file named hello.k3, then compile and run it:

k3 build hello.k3
./hello

Alternatively, you can use the run command to compile and execute in one step:

k3 run hello.k3

You should see the following output:

Hello, World!

Understanding the Code

Let's break down the Hello World program:

  • // hello.k3 - This is a comment indicating the filename.
  • fn main() { ... } - This defines the main function, which is the entry point of the program.
  • println("Hello, World!") - This calls the println function to print the string "Hello, World!" to the console.

A More Interactive Example

Let's create a slightly more interactive program that asks for the user's name and greets them:

// greeting.k3
fn main() {
    println("What is your name?")
    
    let name = readLine()
    
    if name.trim() == "" {
        println("Hello, stranger!")
    } else {
        println("Hello, {}!", name.trim())
    }
}

Run this program with:

k3 run greeting.k3

Tip: K3 uses string interpolation with curly braces {} to insert values into strings, similar to Rust's format strings.

Editor Setup

K3 has excellent editor support for popular code editors and IDEs. Here's how to set up K3 in your favorite editor:

Visual Studio Code

  1. Install the K3 extension from the VS Code marketplace.
  2. Open a K3 file or create a new one with a .k3 extension.
  3. The extension provides syntax highlighting, code completion, error checking, and more.

JetBrains IDEs (IntelliJ IDEA, WebStorm, etc.)

  1. Install the K3 plugin from the JetBrains marketplace.
  2. Restart your IDE.
  3. Open a K3 file or create a new one with a .k3 extension.

Vim/Neovim

For Vim or Neovim, you can use the K3 plugin:

# For Vim with vim-plug
Plug 'k3-lang/vim-k3'

# For Neovim with packer.nvim
use 'k3-lang/vim-k3'

Emacs

For Emacs, you can use the K3 mode:

;; Using use-package
(use-package k3-mode
  :ensure t)

Sublime Text

  1. Install Package Control if you haven't already.
  2. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
  3. Select "Package Control: Install Package".
  4. Search for "K3 Language" and install it.

Note: All these editor extensions provide syntax highlighting, code completion, and error checking. Some also provide debugging support, code formatting, and more advanced features.

Syntax Overview

K3's syntax is designed to be clean, readable, and familiar to developers coming from languages like Rust, TypeScript, and Swift. This section provides a quick overview of K3's syntax.

Comments

// This is a single-line comment

/* This is a
   multi-line comment */

/// Documentation comment for the following item
fn documented_function() {
    // Function body
}

Basic Structure

// Import statement
import Math from "k3/math"

// Function definition
fn add(a: int, b: int) -> int {
    return a + b
}

// Main function (entry point)
fn main() {
    let result = add(5, 3)
    println("5 + 3 = {}", result)
    
    // Using an imported function
    let sqrt = Math.sqrt(16)
    println("Square root of 16 is {}", sqrt)
}

Variables and Constants

// Variable declaration with type inference
let name = "Alice"

// Variable declaration with explicit type
let age: int = 30

// Constants
const PI: f64 = 3.14159
const MAX_USERS: int = 1000

// Mutability
let mut counter = 0
counter += 1  // OK, counter is mutable

name = "Bob"  // Error, name is immutable

Control Flow

// If-else statement
if age >= 18 {
    println("Adult")
} else if age >= 13 {
    println("Teenager")
} else {
    println("Child")
}

// For loop
for i in 0..10 {
    println("{}", i)
}

// While loop
let mut i = 0
while i < 10 {
    println("{}", i)
    i += 1
}

// Match statement (pattern matching)
match status {
    Status.Success => println("Operation succeeded"),
    Status.Error(err) => println("Error: {}", err),
    Status.Pending => println("Operation pending"),
    _ => println("Unknown status")
}

Tip: K3's syntax is designed to be familiar to developers coming from languages like Rust, TypeScript, and Swift, making it easy to learn if you already know one of these languages.