Feng,Clarity by design

Feng helps developers and AI write clear, reliable, and maintainable code

  • AI-friendly
  • Explicit contracts
  • Static typing
  • Managed memory
  • Compiled
  • Cross-platform

Quick install

Copy the command and run it in your terminal

curl -fsSL https://feng-lang.com/install.sh | bash
module app;

import std.io;

// main is the sole entry point of an executable program
func main(args: string[]) {
  println("Hello, world!");
}
module app;

// type organizes data and behavior into a static type
type User {
  // let fields are read-only; var fields can be updated
  let name: string;
  let email: string;
  var active: bool;

  // Methods access the current instance through self
  func greeting(): string {
    return "Hello, " + self.name;
  }
}
module app;

// An object contract declares required behavior
spec Named {
  func name(): string;
}

spec Persistable {
  func save(): void;
}

// A function contract declares a callable value's full signature
spec Formatter(value: string): string;

// A union contract accepts values from multiple candidate types
spec Identifier: int | string;

// An intersection contract requires every member contract
spec Entity: Named & Persistable;

type User {
  let username: string;
}

// fit satisfies object contracts without changing the type declaration
fit User: Named, Persistable {
  func name(): string {
    return self.username;
  }

  func save(): void {
  }
}
module app;

import std.io;

// T makes logic reusable while preserving static types
func identity<T>(value: T): T {
  return value;
}

func main(args: string[]) {
  let language = identity("Feng");
  println("Hello, {0}!", language);
}
module app;

func calculate_total(): int {
  var total = 0;

  // A three-part for loop suits a defined counting range
  for var i = 1; i <= 3; i += 1 {
    total += i;
  }

  // for/in iterates through array elements in order
  let values = [4, 5, 6];
  for let value in values {
    total += value;
  }

  // while checks its condition again before each iteration
  var remaining = 2;
  while remaining > 0 {
    total += remaining;
    remaining -= 1;
  }
  return total;
}
module app;

func evaluate(score: int): string {
  var status = "";

  // An if/else statement performs different actions by condition
  if score >= 60 {
    status = "Pass";
  } else {
    status = "Fail";
  }

  // An if expression directly produces a value to bind
  let level = if score >= 90 {
    "Excellent"
  } else if score >= 60 {
    "Qualified"
  } else {
    "Needs improvement"
  };
  return status + ": " + level;
}
module app;

func describe(code: int): string {
  var detail = "";

  // A match statement runs the first matching branch
  match code {
    200 { detail = "Request succeeded"; }
    404 { detail = "Resource not found"; }
    else { detail = "Unknown status"; }
  }

  // A match expression returns the value of its matching branch
  let category = match code {
    200...299 { "Success" }
    400...499 { "Client error" }
    else { "Other" }
  };
  return category + ": " + detail;
}
module app;

import std.io;

func require_port(port: int): int {
  if port <= 0 {
    // throw ends the current path and passes an exception to the caller
    throw "Port must be positive";
  }
  return port;
}

func main(args: string[]) {
  // Statement form performs actions while handling an exception
  try require_port(-1) catch error: string {
    println(error);
  };

  // Expression form can provide a fallback value for an exception path
  let port = try require_port(-1) catch error: string {
    println(error);
    3000
  };
  println("Using port: {0}", port);
}