Early preview: syntax, standard library APIs, and tooling may change.

FoundationsMethod Syntax

Method Syntax

Method-style calls let ordinary functions read like a pipeline.

First Argument

The first argument of a function can make calls via "method syntax". x.f(y) and f(x, y) are equivalent.

func string_and_reverse(n: Int) -> String:
	n
		.to_string()
		.reverse()


func main(args: List[String]) -> Void:
	text: String = string_and_reverse(42) -- "24"

	same: String = 42.string_and_reverse() -- "24"

Chains

Method syntax allows left to right operations and keeps nested calls from growing inward.

pure func slug(name: String) -> String:
	name
		.trim()
		.split(" ")
		.join("-")

Globally Available

Prelude types such as List, Int, String, Bool, Option, and Result have method-style calls available without imports. The bare function name still needs an import; the method call does not.

func globally_available_methods() -> String:
	scores: List[Int] = [3, -1, 4]
	score_text: String = scores
		.filter(func(n): n > 0)
		.map(func(n): n.to_string())
		.join(", ")
	name: String = "  Ada  "
		.trim()
	"${name}: ${score_text}"


func main(args: List[String]) -> Void:
	print(globally_available_methods()) -- prints: Ada: 3, 4

Example

Non-method syntax requires explicit imports and can be harder to read.

pipeline.brp
import:
	list: filter, join, map


func main(args: List[String]) -> Void:
	values: List[Int] = [3, -1, 4]

	chained: String = values
		.filter(func(x): x > 0)
		.map(func(x): x * 2)
		.map(func(x): to_string(x))
		.join(", ")

	nested: String = join(
		map(
			map(
				filter(
					values,
					func(x): x > 0,
				),
				func(x): x * 2,
			),
			func(x): to_string(x),
		),
		", ",
	)

	print(chained) -- prints: 6, 8
	print(nested) -- prints: 6, 8

Try it

terminal
blorp run pipeline.brp