You can edit almost every page by Creating an account. Otherwise, see the FAQ.

Hare (programming language)

From EverybodyWiki Bios & Wiki

Script error: No such module "Draft topics". Script error: No such module "AfC topic".

Hare
Harelang mascot.png
Designed byDrew DeVault
First appearedApril 25, 2022; 2 years ago (2022-04-25)[1]
Platformx86_64, aarch64, riscv64
OSLinux, FreeBSD
LicenseMozilla Public License - Hare Standard Library
GPL 3.0 - reference compiler[2]
CC-BY-ND - Hare specification
Filename extensions.ha
Websiteharelang.org
Influenced by
C, Go

Search Hare (programming language) on Amazon.

Hare is a general-purpose systems programming language designed by Drew DeVault and implemented together with a team of contributors.[1][3] It is designed to be used in projects such as operating systems, system tools, video games, compilers and other tasks where high performance is required. Hare aims to improve upon the C programming language, and most closely compares to languages such as Zig and Go.

The language has a rich standard library[4] which offers support for cryptography, networking, regular expressions, date/time operations and others. Third-party libraries add support for many other features, such as OpenGL-based 3D graphics.[5]

Hare's design is based on the following principles:

  • Trust the programmer.
  • Provide tools the programmer may use when they don’t trust themselves.
  • Prefer explicit behavior over implicit behavior.
  • A good program must be both correct and simple.[1]

The reference compiler uses the QBE compiler backend to produce machine code. Currently, the only officially supported operating systems are Linux and FreeBSD, but the compiler has been ported to OpenBSD.[6]

Key features[edit]

Hare attempts to improve on the C programming language by making advances in error handling and introducing a more advanced, but still minimal, type system.[7]

Tagged unions[edit]

Tagged unions allow values to take one of several types, and are defined like so: (int | str | io::error). This feature allows functions to return any one of several possible types (including error types, covered below). The match statement can then be used to define actions to be taken when each of these different types are encountered.

fn io::write(s: *stream, buf: const []u8) (size | io::error);

// ...

sum += match (io::write(s, buf)) {
case let err: io::error =>
	match (err) {
	case unsupported =>
		abort("Expected write to be supported");
	case =>
		return err;
	};
case let n: size =>
	process(buf[..n]);
	yield n;
};

Error types[edit]

In Hare, any type can become an error type by being prefixed with an exclamation mark (!). This feature, in combination with tagged unions, allows functions to return errors containing detailed information alongside their other return values, without placing an undue burden on the programmer.

type error = !str;

export fn do_thing() (int | !str) = {
	if (some_condition) {
		return 42;
	} else {
		return "Something bad happened": error;
	};
};

Arrays and slices[edit]

Hare's support for arrays and slices provides spatial memory safety for array operations and also allows the programmer to easily use dynamically allocated, growable slices.

export fn main() void = {
	let x = [1, 2, 3];
	let y = x[42];
};
// $ hare run main.ha
// Abort: slice or array access out of bounds

Examples[edit]

Hello world[edit]

use fmt;

export fn main() void = {
	const greetings = [
		"Hello, world!",
		"¡Hola Mundo!",
		"Γειά σου Κόσμε!",
		"Привет, мир!",
		"こんにちは世界!",
	];
	for (let i = 0z; i < len(greetings); i += 1) {
		fmt::println(greetings[i])!;
	};
};

A program which computes its own SHA-256 hash[edit]

use crypto::sha256;
use encoding::hex;
use fmt;
use hash;
use io;
use os;

export fn main() void = {
	const hash = sha256::sha256();
	const file = os::open("main.ha")!;
	defer io::close(file)!;
	io::copy(&hash, file)!;

	let sum: [sha256::SIZE]u8 = [0...];
	hash::sum(&hash, sum);
	hex::encode(os::stdout, sum)!;
	fmt::println()!;
};

References[edit]

  1. 1.0 1.1 1.2 "Announcing the Hare programming language". harelang.org.
  2. "~sircmpwn/harec: COPYING - sourcehut git". git.sr.ht.
  3. Claburn, Thomas. "Heresy: Hare programming language an alternative to C". www.theregister.com. Retrieved 2022-04-27.
  4. "Hare documentation". harelang.org. Retrieved 2022-04-30.
  5. Harbuz, Vlad-Stefan. "hare-3d-graphics: A summary of the state of 3D graphics in Hare". git.sr.ht. Retrieved 2022-04-30.
  6. Callahan, Brian. "I ported the new Hare compiler to OpenBSD". briancallahan.net. Retrieved 13 May 2022.
  7. DeVault, Drew. "Hare's advances compared to C". harelang.org. Retrieved 2022-04-30.

External links[edit]



This article "Hare (programming language)" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Hare (programming language). Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.