Repository navigation
Flag v0.3.0
A release about errors that say what happened and unions you handle with switch, with a standard library that is much faster and has one way to do each thing.
New
switchover a tagged union names the types of its members:case i32:,case A|B:,case f64, null:. In each case the variable has that type, like afteris, and the default gets the types no case named. A switch over aboolwithcase true:andcase false:is complete- Open slices,
x[a:],x[:b]andx[:], on text, lists, slices, arrays and your types with alenfield - Structs and unions without their own
==compare field by field, and get ahashthe same way, so a struct with text fields works as the key of a map m[k] += v, and the other assignments with an operator, on maps and on any type whosex[i]gives a copy. A key that is not there counts as zero, and on a map the key is looked up once- Errors with data,
type error BadLine:with fields, are documented and work everywhere:try,iferror,switch,print Fileis in core:stdin,stdoutandstderr, andopen,create,read,read_line,write,flushandclosefor any file.create(path, append = true)writes at the end without emptying it, andflush()sends what waits in every file- A
Socketofnetreads and writes like a file, withread,read_line,write,flushandclose, andtimeout(s, seconds)makes a slow read or write fail with the codeTIMED_OUTon every system. A server is aServer, its own type, so a file, a server and a connection never go one where the other goes - What the system refuses is an
FsErrorwith the code, what failed and the path, and printing it says all of it:cannot open notes.txt: No such file or directory.netfails with aNetErrorof the same fields:cannot connect to example.com:80: Connection refused.NotJsonsays where and why:not JSON at line 3, column 14: expected ':' after the key. Witherrno()your own C calls fail the same way - A name you declare that core also declares, like
File,Integerorstdout, is yours in your files, and the library keeps its own, shown ascore.Filein messages - A field or an element of a value in a map changes in place, the key found once and added with a zero value when it is missing:
players[name].score += 10,pairs[a][b] += 1. Passed where a pointer goes,m[k]is copied and put back after the call:append(groups[k], v) words(text)splits on spaces, tabs and line ends:for w in words(text): counts[w] += 1append(list, items),clearandreservefor lists,clearfor rings,find_last,pad,view(bytes),free(p)for whatheapgives, theIntegeralias andjson.key(d, value)- A value goes into a union through the member it converts to: a
&strinto&strview|i32, a&list[T]into&slice[T]?, and{}or{1, 2}into a union with one struct, alsom[k] = {} a += bon a&strviewjoins likea = a + b, and aforwalks through a pointer to an array, a list, a slice or textexamples/grep.flg, files line by line with the errors of each step
Faster
- The output is buffered: printing a million lines is 7 to 10 times faster, and
printallocates nothing. It goes out line by line on a terminal for c in textis written by the compiler and is 57 times faster,find39 times, floats print 6 times faster,read_line4 times,split3 times, counting words in a map 2 times, andjson.parse2 times. A line of 80 MB reads in 0.13 s, it took 3.2 s- The compiler checks itself with half the instructions and builds itself with 38 % fewer: the overload a call chooses is remembered, and no code is generated to be thrown away
- Structs, unions and arrays bigger than 128 bytes are copied with
memcpyand passed by address. A struct of hundreds of kilobytes passed by value compiled for minutes, now it's instant - The
==andhashthat the compiler makes are one function per type, so big and nested structs compile at once - A
contexttakes no room in the program file, the tags of its unions are written when the program starts - QBE is linear on big functions: a loop over an enum of 1500 variants compiles in 0.2 s, it took 37 s. Long chains are faster too: 20000
elifcompile in 8 s, they took 38 s - A run of
andor oforis tested in one block after another, so what the left side narrowed is known once: 10000 terms compile in 3.5 s, they took 42 s and 1.8 GB - The compiler uses less stack: blocks, expressions and types nest up to 500 levels (blocks were about 150), and chains of
elif,ifnull,iferroror of one operator are not nesting - A connection sends at once (
TCP_NODELAY)
Changed
f(x[i])with a pointer parameter, whenx[i]gives a copy, puts the copy back withput(x, i, v)after the call, and without thatputit doesn't compile: it changed a copy that nobody saw- An enum without a value starts as the first variant you wrote, also when another enum declared some of its variants before, and two enums with all the same variants are an error:
type Paint = Colorgives one a second name - A case of a switch over a union is a type, never a value or a variable, and a default that can never run is an error
- A deferred call that can fail doesn't compile,
defer _ = f()drops the error on purpose, and awithwhoseclosecan fail is an error - A type with its own
==and nohashcan't be the key of a map, declare itshashtoo - The counter of a range loop and the index of
for i, x incan't be assigned arg_count,arg,write(fd, text)andos.read_line()are gone:args()ofos,write(stdout, x)andread_line(stdin)- Files moved from
fsto core, andread_text_fileisread_file.close(f)gives nothing, sowith file:works, andflush(f)gives the first write that failed since the lastflush. A write that is interrupted or refused for now is tried again stdoutandstderrkeep the order of your calls, and every file sends what waits before the program waits to read and before another program runs. A program whose standard output could not be written ends withflag: cannot write the standard output: ...and the exit code 1sendandreceiveofnetarewriteandread,NetErroris a struct with the code, andCannotRunis gone:os.runfails withFsErroros.runandos.outputtake a&sliceand givei32|FsError, andoutput(command, into)appends to your text and gives the exit code. On Windowsrunof a list refuses.batand.cmd,cmd.exewould read their arguments again- Files, connections and pipes of the library are not passed to the programs you run
getis gone from maps and fixed maps,m[k]is the way.json.textgivesnullwhen the value is not a string,json.quoted(text)is a value for+andprint, andjson.freetakes the document by pointerwrite_file,append_fileandchange_dirgiveFsError?,fs.joinisjoin_path,swaptakes any integer,bitsany integer index andhexany integer, printing the bits of its sizeparse_floattakes only decimal numbers,infandnan, andfixedprints0, not-0- Text is read with one UTF-8 rule: an overlong form, a surrogate or a code above U+10FFFF comes byte by byte in
for c in text,utf8.countcounts those pieces andutf8.validagrees printcomputes all its values before it writes- An error inside a generic of the library points at your call, with a note for the line in the library
- No type can be made from an empty literal (
wrap([])intoarray[$T, 2]),heap([])still works atfor a list, a slice, an array or text can't take an index that can be an integer- A module with a
privfunction keeps that name for itself, so an exact public overload next to aprivgeneric wins - Passing an empty function value stops the program at that call
forover text belongs to the language, like over lists, soiterfor text can't be declared-memorytakes a free through an element as the free of all the elements, so freeing them in a loop or withpopdoesn't warn, and freeing only some of them is not reported- Type names in messages read like code,
func(i32, i32) -> bool, and many messages say more about why
Fixed
m["a"] = showwithshowoverloaded,return {1, 2}into aPoint?, and(usize(0) as func())()skipping the check of an empty function value-memoryfalse positives: unions whose members keep a pointer in the same place, reading a whole file and printing its error, errors with data aftertry,ison a union like&str|FsError, and print, read a file, print- On Windows a connection and a file could get the same number and share what waits to be sent
- Using
netignoredSIGPIPEfor the whole program and the programs it ran is_dirof a folder you cannot read,file_sizeof a FIFO (it waited forever),removeof a link to a folder,walklisting locked folders as files,executable()afterchange_dir, andlist_dirandwalkon macos-x86_64renamereplaces an existing file on Windows too, andoutput(command)keeps every byte on Windowsos.outputcouldn't tell a program that doesn't start from one that prints nothing- An
eprintinside youroperator +=stopped the program inside core, andout_of_memorycould call itself build.shrebuilds QBE when one of its headers changes
What may still surprise you is in Rough edges.