Syntax
Draft’s syntax is designed to be readable and consistent. This page covers all syntax elements.
Comments
Section titled “Comments”Single-Line Comments
Section titled “Single-Line Comments”@// This is a comment@x = 5 @// Inline commentMulti-Line Comments
Section titled “Multi-Line Comments”@/*This is amulti-line comment*/Doc Comments
Section titled “Doc Comments”Doc comments generate documentation:
@##Calculates the factorial of a number.
@@param @n The number to calculate@@return The factorial result##action factorial(@n: Int) -> Int if @n <= 1 return 1 fin if. return @n * factorial(@n - 1)fin action.Identifiers
Section titled “Identifiers”Identifiers must start with a letter or underscore, followed by letters, digits, or underscores.
@valid_name@_private@count123@CamelCase@snake_caseReserved keywords cannot be used as identifiers.
Reserved Keywords
Section titled “Reserved Keywords”action and async await break const continueelse enum error false fin for funif in let match mut modulenot or pub record returnstruct super then true type varwhile with yieldStatements
Section titled “Statements”Statements end with a period (.) or are terminated by block keywords.
@x = 5. @// Statement with period@greet(). @// Action callreturn 42. @// Return statementBlocks
Section titled “Blocks”Blocks begin with a keyword and end with a matching fin keyword followed by the block type and a period.
action example() @// Action blockfin if.
if @condition @// If blockfin if.
for @i in @range @// For loop blockfin for.
while @condition @// While loop blockfin while.Expressions
Section titled “Expressions”Literals
Section titled “Literals”@integer = 42@float = 3.14@string = "Hello, Draft!"@char = 'a'@boolean = true@nothing = nilArithmetic
Section titled “Arithmetic”@sum = @a + @b@diff = @a - @b@product = @a * @b@quotient = @a / @b@remainder = @a % @bComparison
Section titled “Comparison”@equal = @a == @b@not_equal = @a != @b@greater = @a > @b@less = @a < @b@greater_equal = @a >= @b@less_equal = @a <= @bLogical
Section titled “Logical”@and = @a and @b@or = @a or @b@not = not @aString Interpolation
Section titled “String Interpolation”@name = "World"@greeting = "Hello, {@name}!"@// Result: "Hello, World!"Ranges
Section titled “Ranges”@range = 1..10 @// Inclusive range@exclusive = 1..<10 @// Exclusive upper boundMember Access
Section titled “Member Access”@length = @string#length@first = @list#first@value = @map#"key"Index Access
Section titled “Index Access”@first = @array[0]@value = @map["key"]Whitespace and Formatting
Section titled “Whitespace and Formatting”Draft is not whitespace-sensitive like Python. Statements are terminated explicitly.
@// Validaction greet() write("Hi") fin action.
@// Preferred (formatted)action greet() write("Hi")fin action.The formatter (draft fmt) handles consistent style.
Operator Precedence
Section titled “Operator Precedence”From lowest to highest:
orand==,!=,<,>,<=,>=+,-*,/,%not, unary-#,[],()
Semicolons
Section titled “Semicolons”Semicolons are not used in Draft. Statements end with . or block terminators.
End of File
Section titled “End of File”The last statement in a file may omit the trailing period if it’s a block terminator.
action main() write("Hello")fin action@// No period needed at EOF