manual.rst 220 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100
  1. ==========
  2. Nim Manual
  3. ==========
  4. :Authors: Andreas Rumpf, Zahary Karadjov
  5. :Version: |nimversion|
  6. .. contents::
  7. "Complexity" seems to be a lot like "energy": you can transfer it from the end
  8. user to one/some of the other players, but the total amount seems to remain
  9. pretty much constant for a given task. -- Ran
  10. About this document
  11. ===================
  12. **Note**: This document is a draft! Several of Nim's features may need more
  13. precise wording. This manual is constantly evolving into a proper specification.
  14. **Note**: The experimental features of Nim are
  15. covered `here <manual_experimental.html>`_.
  16. This document describes the lexis, the syntax, and the semantics of the Nim language.
  17. To learn how to compile Nim programs and generate documentation see
  18. `Compiler User Guide <nimc.html>`_ and `DocGen Tools Guide <docgen.html>`_.
  19. The language constructs are explained using an extended BNF, in which ``(a)*``
  20. means 0 or more ``a``'s, ``a+`` means 1 or more ``a``'s, and ``(a)?`` means an
  21. optional *a*. Parentheses may be used to group elements.
  22. ``&`` is the lookahead operator; ``&a`` means that an ``a`` is expected but
  23. not consumed. It will be consumed in the following rule.
  24. The ``|``, ``/`` symbols are used to mark alternatives and have the lowest
  25. precedence. ``/`` is the ordered choice that requires the parser to try the
  26. alternatives in the given order. ``/`` is often used to ensure the grammar
  27. is not ambiguous.
  28. Non-terminals start with a lowercase letter, abstract terminal symbols are in
  29. UPPERCASE. Verbatim terminal symbols (including keywords) are quoted
  30. with ``'``. An example::
  31. ifStmt = 'if' expr ':' stmts ('elif' expr ':' stmts)* ('else' stmts)?
  32. The binary ``^*`` operator is used as a shorthand for 0 or more occurrences
  33. separated by its second argument; likewise ``^+`` means 1 or more
  34. occurrences: ``a ^+ b`` is short for ``a (b a)*``
  35. and ``a ^* b`` is short for ``(a (b a)*)?``. Example::
  36. arrayConstructor = '[' expr ^* ',' ']'
  37. Other parts of Nim, like scoping rules or runtime semantics, are
  38. described informally.
  39. Definitions
  40. ===========
  41. Nim code specifies a computation that acts on a memory consisting of
  42. components called `locations`:idx:. A variable is basically a name for a
  43. location. Each variable and location is of a certain `type`:idx:. The
  44. variable's type is called `static type`:idx:, the location's type is called
  45. `dynamic type`:idx:. If the static type is not the same as the dynamic type,
  46. it is a super-type or subtype of the dynamic type.
  47. An `identifier`:idx: is a symbol declared as a name for a variable, type,
  48. procedure, etc. The region of the program over which a declaration applies is
  49. called the `scope`:idx: of the declaration. Scopes can be nested. The meaning
  50. of an identifier is determined by the smallest enclosing scope in which the
  51. identifier is declared unless overloading resolution rules suggest otherwise.
  52. An expression specifies a computation that produces a value or location.
  53. Expressions that produce locations are called `l-values`:idx:. An l-value
  54. can denote either a location or the value the location contains, depending on
  55. the context.
  56. A Nim `program`:idx: consists of one or more text `source files`:idx: containing
  57. Nim code. It is processed by a Nim `compiler`:idx: into an `executable`:idx:.
  58. The nature of this executable depends on the compiler implementation; it may,
  59. for example, be a native binary or JavaScript source code.
  60. In a typical Nim program, most of the code is compiled into the executable.
  61. However, some of the code may be executed at
  62. `compile time`:idx:. This can include constant expressions, macro definitions,
  63. and Nim procedures used by macro definitions. Most of the Nim language is
  64. supported at compile time, but there are some restrictions -- see `Restrictions
  65. on Compile-Time Execution <#restrictions-on-compileminustime-execution>`_ for
  66. details. We use the term `runtime`:idx: to cover both compile-time execution
  67. and code execution in the executable.
  68. The compiler parses Nim source code into an internal data structure called the
  69. `abstract syntax tree`:idx: (`AST`:idx:). Then, before executing the code or
  70. compiling it into the executable, it transforms the AST through `semantic
  71. analysis`:idx:. This adds semantic information such as expression types,
  72. identifier meanings, and in some cases expression values. An error detected
  73. during semantic analysis is called a `static error`:idx:. Errors described in
  74. this manual are static errors when not otherwise specified.
  75. A `checked runtime error`:idx: is an error that the implementation detects
  76. and reports at runtime. The method for reporting such errors is via
  77. *raising exceptions* or *dying with a fatal error*. However, the implementation
  78. provides a means to disable these `runtime checks`:idx:. See the section
  79. pragmas_ for details.
  80. Whether a checked runtime error results in an exception or in a fatal error is
  81. implementation specific. Thus the following program is invalid; even though the
  82. code purports to catch the `IndexError` from an out-of-bounds array access, the
  83. compiler may instead choose to allow the program to die with a fatal error.
  84. .. code-block:: nim
  85. var a: array[0..1, char]
  86. let i = 5
  87. try:
  88. a[i] = 'N'
  89. except IndexError:
  90. echo "invalid index"
  91. An `unchecked runtime error`:idx: is an error that is not guaranteed to be
  92. detected, and can cause the subsequent behavior of the computation to
  93. be arbitrary. Unchecked runtime errors cannot occur if only `safe`:idx:
  94. language features are used and if no runtime checks are disabled.
  95. A `constant expression`:idx: is an expression whose value can be computed during
  96. semantic analysis of the code in which it appears. It is never an l-value and
  97. never has side effects. Constant expressions are not limited to the capabilities
  98. of semantic analysis, such as constant folding; they can use all Nim language
  99. features that are supported for compile-time execution. Since constant
  100. expressions can be used as an input to semantic analysis (such as for defining
  101. array bounds), this flexibility requires the compiler to interleave semantic
  102. analysis and compile-time code execution.
  103. It is mostly accurate to picture semantic analysis proceeding top to bottom and
  104. left to right in the source code, with compile-time code execution interleaved
  105. when necessary to compute values that are required for subsequent semantic
  106. analysis. We will see much later in this document that macro invocation not only
  107. requires this interleaving, but also creates a situation where semantic analyis
  108. does not entirely proceed top to bottom and left to right.
  109. Lexical Analysis
  110. ================
  111. Encoding
  112. --------
  113. All Nim source files are in the UTF-8 encoding (or its ASCII subset). Other
  114. encodings are not supported. Any of the standard platform line termination
  115. sequences can be used - the Unix form using ASCII LF (linefeed), the Windows
  116. form using the ASCII sequence CR LF (return followed by linefeed), or the old
  117. Macintosh form using the ASCII CR (return) character. All of these forms can be
  118. used equally, regardless of platform.
  119. Indentation
  120. -----------
  121. Nim's standard grammar describes an `indentation sensitive`:idx: language.
  122. This means that all the control structures are recognized by indentation.
  123. Indentation consists only of spaces; tabulators are not allowed.
  124. The indentation handling is implemented as follows: The lexer annotates the
  125. following token with the preceding number of spaces; indentation is not
  126. a separate token. This trick allows parsing of Nim with only 1 token of
  127. lookahead.
  128. The parser uses a stack of indentation levels: the stack consists of integers
  129. counting the spaces. The indentation information is queried at strategic
  130. places in the parser but ignored otherwise: The pseudo terminal ``IND{>}``
  131. denotes an indentation that consists of more spaces than the entry at the top
  132. of the stack; ``IND{=}`` an indentation that has the same number of spaces. ``DED``
  133. is another pseudo terminal that describes the *action* of popping a value
  134. from the stack, ``IND{>}`` then implies to push onto the stack.
  135. With this notation we can now easily define the core of the grammar: A block of
  136. statements (simplified example)::
  137. ifStmt = 'if' expr ':' stmt
  138. (IND{=} 'elif' expr ':' stmt)*
  139. (IND{=} 'else' ':' stmt)?
  140. simpleStmt = ifStmt / ...
  141. stmt = IND{>} stmt ^+ IND{=} DED # list of statements
  142. / simpleStmt # or a simple statement
  143. Comments
  144. --------
  145. Comments start anywhere outside a string or character literal with the
  146. hash character ``#``.
  147. Comments consist of a concatenation of `comment pieces`:idx:. A comment piece
  148. starts with ``#`` and runs until the end of the line. The end of line characters
  149. belong to the piece. If the next line only consists of a comment piece with
  150. no other tokens between it and the preceding one, it does not start a new
  151. comment:
  152. .. code-block:: nim
  153. i = 0 # This is a single comment over multiple lines.
  154. # The scanner merges these two pieces.
  155. # The comment continues here.
  156. `Documentation comments`:idx: are comments that start with two ``##``.
  157. Documentation comments are tokens; they are only allowed at certain places in
  158. the input file as they belong to the syntax tree!
  159. Multiline comments
  160. ------------------
  161. Starting with version 0.13.0 of the language Nim supports multiline comments.
  162. They look like:
  163. .. code-block:: nim
  164. #[Comment here.
  165. Multiple lines
  166. are not a problem.]#
  167. Multiline comments support nesting:
  168. .. code-block:: nim
  169. #[ #[ Multiline comment in already
  170. commented out code. ]#
  171. proc p[T](x: T) = discard
  172. ]#
  173. Multiline documentation comments also exist and support nesting too:
  174. .. code-block:: nim
  175. proc foo =
  176. ##[Long documentation comment
  177. here.
  178. ]##
  179. Identifiers & Keywords
  180. ----------------------
  181. Identifiers in Nim can be any string of letters, digits
  182. and underscores, beginning with a letter. Two immediate following
  183. underscores ``__`` are not allowed::
  184. letter ::= 'A'..'Z' | 'a'..'z' | '\x80'..'\xff'
  185. digit ::= '0'..'9'
  186. IDENTIFIER ::= letter ( ['_'] (letter | digit) )*
  187. Currently any Unicode character with an ordinal value > 127 (non ASCII) is
  188. classified as a ``letter`` and may thus be part of an identifier but later
  189. versions of the language may assign some Unicode characters to belong to the
  190. operator characters instead.
  191. The following keywords are reserved and cannot be used as identifiers:
  192. .. code-block:: nim
  193. :file: keywords.txt
  194. Some keywords are unused; they are reserved for future developments of the
  195. language.
  196. Identifier equality
  197. -------------------
  198. Two identifiers are considered equal if the following algorithm returns true:
  199. .. code-block:: nim
  200. proc sameIdentifier(a, b: string): bool =
  201. a[0] == b[0] and
  202. a.replace("_", "").toLowerAscii == b.replace("_", "").toLowerAscii
  203. That means only the first letters are compared in a case sensitive manner. Other
  204. letters are compared case insensitively within the ASCII range and underscores are ignored.
  205. This rather unorthodox way to do identifier comparisons is called
  206. `partial case insensitivity`:idx: and has some advantages over the conventional
  207. case sensitivity:
  208. It allows programmers to mostly use their own preferred
  209. spelling style, be it humpStyle or snake_style, and libraries written
  210. by different programmers cannot use incompatible conventions.
  211. A Nim-aware editor or IDE can show the identifiers as preferred.
  212. Another advantage is that it frees the programmer from remembering
  213. the exact spelling of an identifier. The exception with respect to the first
  214. letter allows common code like ``var foo: Foo`` to be parsed unambiguously.
  215. Note that this rule also applies to keywords, meaning that ``notin`` is
  216. the same as ``notIn`` and ``not_in`` (all-lowercase version (``notin``, ``isnot``)
  217. is the preferred way of writing keywords).
  218. Historically, Nim was a fully `style-insensitive`:idx: language. This meant that
  219. it was not case-sensitive and underscores were ignored and there was not even a
  220. distinction between ``foo`` and ``Foo``.
  221. String literals
  222. ---------------
  223. Terminal symbol in the grammar: ``STR_LIT``.
  224. String literals can be delimited by matching double quotes, and can
  225. contain the following `escape sequences`:idx:\ :
  226. ================== ===================================================
  227. Escape sequence Meaning
  228. ================== ===================================================
  229. ``\p`` platform specific newline: CRLF on Windows,
  230. LF on Unix
  231. ``\r``, ``\c`` `carriage return`:idx:
  232. ``\n``, ``\l`` `line feed`:idx: (often called `newline`:idx:)
  233. ``\f`` `form feed`:idx:
  234. ``\t`` `tabulator`:idx:
  235. ``\v`` `vertical tabulator`:idx:
  236. ``\\`` `backslash`:idx:
  237. ``\"`` `quotation mark`:idx:
  238. ``\'`` `apostrophe`:idx:
  239. ``\`` '0'..'9'+ `character with decimal value d`:idx:;
  240. all decimal digits directly
  241. following are used for the character
  242. ``\a`` `alert`:idx:
  243. ``\b`` `backspace`:idx:
  244. ``\e`` `escape`:idx: `[ESC]`:idx:
  245. ``\x`` HH `character with hex value HH`:idx:;
  246. exactly two hex digits are allowed
  247. ``\u`` HHHH `unicode codepoint with hex value HHHH`:idx:;
  248. exactly four hex digits are allowed
  249. ``\u`` {H+} `unicode codepoint`:idx:;
  250. all hex digits enclosed in ``{}`` are used for
  251. the codepoint
  252. ================== ===================================================
  253. Strings in Nim may contain any 8-bit value, even embedded zeros. However
  254. some operations may interpret the first binary zero as a terminator.
  255. Triple quoted string literals
  256. -----------------------------
  257. Terminal symbol in the grammar: ``TRIPLESTR_LIT``.
  258. String literals can also be delimited by three double quotes
  259. ``"""`` ... ``"""``.
  260. Literals in this form may run for several lines, may contain ``"`` and do not
  261. interpret any escape sequences.
  262. For convenience, when the opening ``"""`` is followed by a newline (there may
  263. be whitespace between the opening ``"""`` and the newline),
  264. the newline (and the preceding whitespace) is not included in the string. The
  265. ending of the string literal is defined by the pattern ``"""[^"]``, so this:
  266. .. code-block:: nim
  267. """"long string within quotes""""
  268. Produces::
  269. "long string within quotes"
  270. Raw string literals
  271. -------------------
  272. Terminal symbol in the grammar: ``RSTR_LIT``.
  273. There are also raw string literals that are preceded with the
  274. letter ``r`` (or ``R``) and are delimited by matching double quotes (just
  275. like ordinary string literals) and do not interpret the escape sequences.
  276. This is especially convenient for regular expressions or Windows paths:
  277. .. code-block:: nim
  278. var f = openFile(r"C:\texts\text.txt") # a raw string, so ``\t`` is no tab
  279. To produce a single ``"`` within a raw string literal, it has to be doubled:
  280. .. code-block:: nim
  281. r"a""b"
  282. Produces::
  283. a"b
  284. ``r""""`` is not possible with this notation, because the three leading
  285. quotes introduce a triple quoted string literal. ``r"""`` is the same
  286. as ``"""`` since triple quoted string literals do not interpret escape
  287. sequences either.
  288. Generalized raw string literals
  289. -------------------------------
  290. Terminal symbols in the grammar: ``GENERALIZED_STR_LIT``,
  291. ``GENERALIZED_TRIPLESTR_LIT``.
  292. The construct ``identifier"string literal"`` (without whitespace between the
  293. identifier and the opening quotation mark) is a
  294. generalized raw string literal. It is a shortcut for the construct
  295. ``identifier(r"string literal")``, so it denotes a procedure call with a
  296. raw string literal as its only argument. Generalized raw string literals
  297. are especially convenient for embedding mini languages directly into Nim
  298. (for example regular expressions).
  299. The construct ``identifier"""string literal"""`` exists too. It is a shortcut
  300. for ``identifier("""string literal""")``.
  301. Character literals
  302. ------------------
  303. Character literals are enclosed in single quotes ``''`` and can contain the
  304. same escape sequences as strings - with one exception: the platform
  305. dependent `newline`:idx: (``\p``)
  306. is not allowed as it may be wider than one character (often it is the pair
  307. CR/LF for example). Here are the valid `escape sequences`:idx: for character
  308. literals:
  309. ================== ===================================================
  310. Escape sequence Meaning
  311. ================== ===================================================
  312. ``\r``, ``\c`` `carriage return`:idx:
  313. ``\n``, ``\l`` `line feed`:idx:
  314. ``\f`` `form feed`:idx:
  315. ``\t`` `tabulator`:idx:
  316. ``\v`` `vertical tabulator`:idx:
  317. ``\\`` `backslash`:idx:
  318. ``\"`` `quotation mark`:idx:
  319. ``\'`` `apostrophe`:idx:
  320. ``\`` '0'..'9'+ `character with decimal value d`:idx:;
  321. all decimal digits directly
  322. following are used for the character
  323. ``\a`` `alert`:idx:
  324. ``\b`` `backspace`:idx:
  325. ``\e`` `escape`:idx: `[ESC]`:idx:
  326. ``\x`` HH `character with hex value HH`:idx:;
  327. exactly two hex digits are allowed
  328. ================== ===================================================
  329. A character is not an Unicode character but a single byte. The reason for this
  330. is efficiency: for the overwhelming majority of use-cases, the resulting
  331. programs will still handle UTF-8 properly as UTF-8 was specially designed for
  332. this. Another reason is that Nim can thus support ``array[char, int]`` or
  333. ``set[char]`` efficiently as many algorithms rely on this feature. The `Rune`
  334. type is used for Unicode characters, it can represent any Unicode character.
  335. ``Rune`` is declared in the `unicode module <unicode.html>`_.
  336. Numerical constants
  337. -------------------
  338. Numerical constants are of a single type and have the form::
  339. hexdigit = digit | 'A'..'F' | 'a'..'f'
  340. octdigit = '0'..'7'
  341. bindigit = '0'..'1'
  342. HEX_LIT = '0' ('x' | 'X' ) hexdigit ( ['_'] hexdigit )*
  343. DEC_LIT = digit ( ['_'] digit )*
  344. OCT_LIT = '0' 'o' octdigit ( ['_'] octdigit )*
  345. BIN_LIT = '0' ('b' | 'B' ) bindigit ( ['_'] bindigit )*
  346. INT_LIT = HEX_LIT
  347. | DEC_LIT
  348. | OCT_LIT
  349. | BIN_LIT
  350. INT8_LIT = INT_LIT ['\''] ('i' | 'I') '8'
  351. INT16_LIT = INT_LIT ['\''] ('i' | 'I') '16'
  352. INT32_LIT = INT_LIT ['\''] ('i' | 'I') '32'
  353. INT64_LIT = INT_LIT ['\''] ('i' | 'I') '64'
  354. UINT_LIT = INT_LIT ['\''] ('u' | 'U')
  355. UINT8_LIT = INT_LIT ['\''] ('u' | 'U') '8'
  356. UINT16_LIT = INT_LIT ['\''] ('u' | 'U') '16'
  357. UINT32_LIT = INT_LIT ['\''] ('u' | 'U') '32'
  358. UINT64_LIT = INT_LIT ['\''] ('u' | 'U') '64'
  359. exponent = ('e' | 'E' ) ['+' | '-'] digit ( ['_'] digit )*
  360. FLOAT_LIT = digit (['_'] digit)* (('.' digit (['_'] digit)* [exponent]) |exponent)
  361. FLOAT32_SUFFIX = ('f' | 'F') ['32']
  362. FLOAT32_LIT = HEX_LIT '\'' FLOAT32_SUFFIX
  363. | (FLOAT_LIT | DEC_LIT | OCT_LIT | BIN_LIT) ['\''] FLOAT32_SUFFIX
  364. FLOAT64_SUFFIX = ( ('f' | 'F') '64' ) | 'd' | 'D'
  365. FLOAT64_LIT = HEX_LIT '\'' FLOAT64_SUFFIX
  366. | (FLOAT_LIT | DEC_LIT | OCT_LIT | BIN_LIT) ['\''] FLOAT64_SUFFIX
  367. As can be seen in the productions, numerical constants can contain underscores
  368. for readability. Integer and floating point literals may be given in decimal (no
  369. prefix), binary (prefix ``0b``), octal (prefix ``0o``) and hexadecimal
  370. (prefix ``0x``) notation.
  371. There exists a literal for each numerical type that is
  372. defined. The suffix starting with an apostrophe ('\'') is called a
  373. `type suffix`:idx:. Literals without a type suffix are of an integer type,
  374. unless the literal contains a dot or ``E|e`` in which case it is of
  375. type ``float``. This integer type is ``int`` if the literal is in the range
  376. ``low(i32)..high(i32)``, otherwise it is ``int64``.
  377. For notational convenience the apostrophe of a type suffix
  378. is optional if it is not ambiguous (only hexadecimal floating point literals
  379. with a type suffix can be ambiguous).
  380. The type suffixes are:
  381. ================= =========================
  382. Type Suffix Resulting type of literal
  383. ================= =========================
  384. ``'i8`` int8
  385. ``'i16`` int16
  386. ``'i32`` int32
  387. ``'i64`` int64
  388. ``'u`` uint
  389. ``'u8`` uint8
  390. ``'u16`` uint16
  391. ``'u32`` uint32
  392. ``'u64`` uint64
  393. ``'f`` float32
  394. ``'d`` float64
  395. ``'f32`` float32
  396. ``'f64`` float64
  397. ================= =========================
  398. Floating point literals may also be in binary, octal or hexadecimal
  399. notation:
  400. ``0B0_10001110100_0000101001000111101011101111111011000101001101001001'f64``
  401. is approximately 1.72826e35 according to the IEEE floating point standard.
  402. Literals are bounds checked so that they fit the datatype. Non base-10
  403. literals are used mainly for flags and bit pattern representations, therefore
  404. bounds checking is done on bit width, not value range. If the literal fits in
  405. the bit width of the datatype, it is accepted.
  406. Hence: 0b10000000'u8 == 0x80'u8 == 128, but, 0b10000000'i8 == 0x80'i8 == -1
  407. instead of causing an overflow error.
  408. Operators
  409. ---------
  410. Nim allows user defined operators. An operator is any combination of the
  411. following characters::
  412. = + - * / < >
  413. @ $ ~ & % |
  414. ! ? ^ . : \
  415. These keywords are also operators:
  416. ``and or not xor shl shr div mod in notin is isnot of``.
  417. `.`:tok: `=`:tok:, `:`:tok:, `::`:tok: are not available as general operators; they
  418. are used for other notational purposes.
  419. ``*:`` is as a special case treated as the two tokens `*`:tok: and `:`:tok:
  420. (to support ``var v*: T``).
  421. The ``not`` keyword is always a unary operator, ``a not b`` is parsed
  422. as ``a(not b)``, not as ``(a) not (b)``.
  423. Other tokens
  424. ------------
  425. The following strings denote other tokens::
  426. ` ( ) { } [ ] , ; [. .] {. .} (. .) [:
  427. The `slice`:idx: operator `..`:tok: takes precedence over other tokens that
  428. contain a dot: `{..}`:tok: are the three tokens `{`:tok:, `..`:tok:, `}`:tok:
  429. and not the two tokens `{.`:tok:, `.}`:tok:.
  430. Syntax
  431. ======
  432. This section lists Nim's standard syntax. How the parser handles
  433. the indentation is already described in the `Lexical Analysis`_ section.
  434. Nim allows user-definable operators.
  435. Binary operators have 11 different levels of precedence.
  436. Associativity
  437. -------------
  438. Binary operators whose first character is ``^`` are right-associative, all
  439. other binary operators are left-associative.
  440. .. code-block:: nim
  441. proc `^/`(x, y: float): float =
  442. # a right-associative division operator
  443. result = x / y
  444. echo 12 ^/ 4 ^/ 8 # 24.0 (4 / 8 = 0.5, then 12 / 0.5 = 24.0)
  445. echo 12 / 4 / 8 # 0.375 (12 / 4 = 3.0, then 3 / 8 = 0.375)
  446. Precedence
  447. ----------
  448. Unary operators always bind stronger than any binary
  449. operator: ``$a + b`` is ``($a) + b`` and not ``$(a + b)``.
  450. If an unary operator's first character is ``@`` it is a `sigil-like`:idx:
  451. operator which binds stronger than a ``primarySuffix``: ``@x.abc`` is parsed
  452. as ``(@x).abc`` whereas ``$x.abc`` is parsed as ``$(x.abc)``.
  453. For binary operators that are not keywords the precedence is determined by the
  454. following rules:
  455. Operators ending in either ``->``, ``~>`` or ``=>`` are called
  456. `arrow like`:idx:, and have the lowest precedence of all operators.
  457. If the operator ends with ``=`` and its first character is none of
  458. ``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which
  459. has the second lowest precedence.
  460. Otherwise precedence is determined by the first character.
  461. ================ =============================================== ================== ===============
  462. Precedence level Operators First character Terminal symbol
  463. ================ =============================================== ================== ===============
  464. 10 (highest) ``$ ^`` OP10
  465. 9 ``* / div mod shl shr %`` ``* % \ /`` OP9
  466. 8 ``+ -`` ``+ - ~ |`` OP8
  467. 7 ``&`` ``&`` OP7
  468. 6 ``..`` ``.`` OP6
  469. 5 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP5
  470. 4 ``and`` OP4
  471. 3 ``or xor`` OP3
  472. 2 ``@ : ?`` OP2
  473. 1 *assignment operator* (like ``+=``, ``*=``) OP1
  474. 0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
  475. ================ =============================================== ================== ===============
  476. Whether an operator is used a prefix operator is also affected by preceding
  477. whitespace (this parsing change was introduced with version 0.13.0):
  478. .. code-block:: nim
  479. echo $foo
  480. # is parsed as
  481. echo($foo)
  482. Spacing also determines whether ``(a, b)`` is parsed as an the argument list
  483. of a call or whether it is parsed as a tuple constructor:
  484. .. code-block:: nim
  485. echo(1, 2) # pass 1 and 2 to echo
  486. .. code-block:: nim
  487. echo (1, 2) # pass the tuple (1, 2) to echo
  488. Grammar
  489. -------
  490. The grammar's start symbol is ``module``.
  491. .. include:: grammar.txt
  492. :literal:
  493. Order of evaluation
  494. ===================
  495. Order of evaluation is strictly left-to-right, inside-out as it is typical for most others
  496. imperative programming languages:
  497. .. code-block:: nim
  498. :test: "nim c $1"
  499. var s = ""
  500. proc p(arg: int): int =
  501. s.add $arg
  502. result = arg
  503. discard p(p(1) + p(2))
  504. doAssert s == "123"
  505. Assignments are not special, the left-hand-side expression is evaluated before the
  506. right-hand side:
  507. .. code-block:: nim
  508. :test: "nim c $1"
  509. var v = 0
  510. proc getI(): int =
  511. result = v
  512. inc v
  513. var a, b: array[0..2, int]
  514. proc someCopy(a: var int; b: int) = a = b
  515. a[getI()] = getI()
  516. doAssert a == [1, 0, 0]
  517. v = 0
  518. someCopy(b[getI()], getI())
  519. doAssert b == [1, 0, 0]
  520. Rationale: Consistency with overloaded assignment or assignment-like operations,
  521. ``a = b`` can be read as ``performSomeCopy(a, b)``.
  522. Constants and Constant Expressions
  523. ==================================
  524. A `constant`:idx: is a symbol that is bound to the value of a constant
  525. expression. Constant expressions are restricted to depend only on the following
  526. categories of values and operations, because these are either built into the
  527. language or declared and evaluated before semantic analysis of the constant
  528. expression:
  529. * literals
  530. * built-in operators
  531. * previously declared constants and compile-time variables
  532. * previously declared macros and templates
  533. * previously declared procedures that have no side effects beyond
  534. possibly modifying compile-time variables
  535. A constant expression can contain code blocks that may internally use all Nim
  536. features supported at compile time (as detailed in the next section below).
  537. Within such a code block, it is possible to declare variables and then later
  538. read and update them, or declare variables and pass them to procedures that
  539. modify them. However, the code in such a block must still adhere to the
  540. retrictions listed above for referencing values and operations outside the
  541. block.
  542. The ability to access and modify compile-time variables adds flexibility to
  543. constant expressions that may be surprising to those coming from other
  544. statically typed languages. For example, the following code echoes the beginning
  545. of the Fibonacci series **at compile time**. (This is a demonstration of
  546. flexibility in defining constants, not a recommended style for solving this
  547. problem!)
  548. .. code-block:: nim
  549. :test: "nim c $1"
  550. import strformat
  551. var fib_n {.compileTime.}: int
  552. var fib_prev {.compileTime.}: int
  553. var fib_prev_prev {.compileTime.}: int
  554. proc next_fib(): int =
  555. result = if fib_n < 2:
  556. fib_n
  557. else:
  558. fib_prev_prev + fib_prev
  559. inc(fib_n)
  560. fib_prev_prev = fib_prev
  561. fib_prev = result
  562. const f0 = next_fib()
  563. const f1 = next_fib()
  564. const display_fib = block:
  565. const f2 = next_fib()
  566. var result = fmt"Fibonacci sequence: {f0}, {f1}, {f2}"
  567. for i in 3..12:
  568. add(result, fmt", {next_fib()}")
  569. result
  570. static:
  571. echo display_fib
  572. Restrictions on Compile-Time Execution
  573. ======================================
  574. Nim code that will be executed at compile time cannot use the following
  575. language features:
  576. * methods
  577. * closure iterators
  578. * the ``cast`` operator
  579. * reference (pointer) types
  580. * the FFI
  581. Some or all of these restrictions are likely to be lifted over time.
  582. Types
  583. =====
  584. All expressions have a type which is known during semantic analysis. Nim
  585. is statically typed. One can declare new types, which is in essence defining
  586. an identifier that can be used to denote this custom type.
  587. These are the major type classes:
  588. * ordinal types (consist of integer, bool, character, enumeration
  589. (and subranges thereof) types)
  590. * floating point types
  591. * string type
  592. * structured types
  593. * reference (pointer) type
  594. * procedural type
  595. * generic type
  596. Ordinal types
  597. -------------
  598. Ordinal types have the following characteristics:
  599. - Ordinal types are countable and ordered. This property allows
  600. the operation of functions as ``inc``, ``ord``, ``dec`` on ordinal types to
  601. be defined.
  602. - Ordinal values have a smallest possible value. Trying to count further
  603. down than the smallest value gives a checked runtime or static error.
  604. - Ordinal values have a largest possible value. Trying to count further
  605. than the largest value gives a checked runtime or static error.
  606. Integers, bool, characters and enumeration types (and subranges of these
  607. types) belong to ordinal types. For reasons of simplicity of implementation
  608. the types ``uint`` and ``uint64`` are not ordinal types. (This will be changed
  609. in later versions of the language.)
  610. A distinct type is an ordinal type if its base type is an ordinal type.
  611. Pre-defined integer types
  612. -------------------------
  613. These integer types are pre-defined:
  614. ``int``
  615. the generic signed integer type; its size is platform dependent and has the
  616. same size as a pointer. This type should be used in general. An integer
  617. literal that has no type suffix is of this type if it is in the range
  618. ``low(int32)..high(int32)`` otherwise the literal's type is ``int64``.
  619. intXX
  620. additional signed integer types of XX bits use this naming scheme
  621. (example: int16 is a 16 bit wide integer).
  622. The current implementation supports ``int8``, ``int16``, ``int32``, ``int64``.
  623. Literals of these types have the suffix 'iXX.
  624. ``uint``
  625. the generic `unsigned integer`:idx: type; its size is platform dependent and
  626. has the same size as a pointer. An integer literal with the type
  627. suffix ``'u`` is of this type.
  628. uintXX
  629. additional unsigned integer types of XX bits use this naming scheme
  630. (example: uint16 is a 16 bit wide unsigned integer).
  631. The current implementation supports ``uint8``, ``uint16``, ``uint32``,
  632. ``uint64``. Literals of these types have the suffix 'uXX.
  633. Unsigned operations all wrap around; they cannot lead to over- or
  634. underflow errors.
  635. In addition to the usual arithmetic operators for signed and unsigned integers
  636. (``+ - *`` etc.) there are also operators that formally work on *signed*
  637. integers but treat their arguments as *unsigned*: They are mostly provided
  638. for backwards compatibility with older versions of the language that lacked
  639. unsigned integer types. These unsigned operations for signed integers use
  640. the ``%`` suffix as convention:
  641. ====================== ======================================================
  642. operation meaning
  643. ====================== ======================================================
  644. ``a +% b`` unsigned integer addition
  645. ``a -% b`` unsigned integer subtraction
  646. ``a *% b`` unsigned integer multiplication
  647. ``a /% b`` unsigned integer division
  648. ``a %% b`` unsigned integer modulo operation
  649. ``a <% b`` treat ``a`` and ``b`` as unsigned and compare
  650. ``a <=% b`` treat ``a`` and ``b`` as unsigned and compare
  651. ``ze(a)`` extends the bits of ``a`` with zeros until it has the
  652. width of the ``int`` type
  653. ``toU8(a)`` treats ``a`` as unsigned and converts it to an
  654. unsigned integer of 8 bits (but still the
  655. ``int8`` type)
  656. ``toU16(a)`` treats ``a`` as unsigned and converts it to an
  657. unsigned integer of 16 bits (but still the
  658. ``int16`` type)
  659. ``toU32(a)`` treats ``a`` as unsigned and converts it to an
  660. unsigned integer of 32 bits (but still the
  661. ``int32`` type)
  662. ====================== ======================================================
  663. `Automatic type conversion`:idx: is performed in expressions where different
  664. kinds of integer types are used: the smaller type is converted to the larger.
  665. A `narrowing type conversion`:idx: converts a larger to a smaller type (for
  666. example ``int32 -> int16``. A `widening type conversion`:idx: converts a
  667. smaller type to a larger type (for example ``int16 -> int32``). In Nim only
  668. widening type conversions are *implicit*:
  669. .. code-block:: nim
  670. var myInt16 = 5i16
  671. var myInt: int
  672. myInt16 + 34 # of type ``int16``
  673. myInt16 + myInt # of type ``int``
  674. myInt16 + 2i32 # of type ``int32``
  675. However, ``int`` literals are implicitly convertible to a smaller integer type
  676. if the literal's value fits this smaller type and such a conversion is less
  677. expensive than other implicit conversions, so ``myInt16 + 34`` produces
  678. an ``int16`` result.
  679. For further details, see `Convertible relation
  680. <#type-relations-convertible-relation>`_.
  681. Subrange types
  682. --------------
  683. A subrange type is a range of values from an ordinal or floating point type (the base
  684. type). To define a subrange type, one must specify its limiting values -- the
  685. lowest and highest value of the type. For example:
  686. .. code-block:: nim
  687. type
  688. Subrange = range[0..5]
  689. PositiveFloat = range[0.0..Inf]
  690. ``Subrange`` is a subrange of an integer which can only hold the values 0
  691. to 5. ``PositiveFloat`` defines a subrange of all positive floating point values.
  692. NaN does not belong to any subrange of floating point types.
  693. Assigning any other value to a variable of type ``Subrange`` is a
  694. checked runtime error (or static error if it can be determined during
  695. semantic analysis). Assignments from the base type to one of its subrange types
  696. (and vice versa) are allowed.
  697. A subrange type has the same size as its base type (``int`` in the
  698. Subrange example).
  699. Pre-defined floating point types
  700. --------------------------------
  701. The following floating point types are pre-defined:
  702. ``float``
  703. the generic floating point type; its size used to be platform dependent,
  704. but now it is always mapped to ``float64``.
  705. This type should be used in general.
  706. floatXX
  707. an implementation may define additional floating point types of XX bits using
  708. this naming scheme (example: float64 is a 64 bit wide float). The current
  709. implementation supports ``float32`` and ``float64``. Literals of these types
  710. have the suffix 'fXX.
  711. Automatic type conversion in expressions with different kinds
  712. of floating point types is performed: See `Convertible relation`_ for further
  713. details. Arithmetic performed on floating point types follows the IEEE
  714. standard. Integer types are not converted to floating point types automatically
  715. and vice versa.
  716. The IEEE standard defines five types of floating-point exceptions:
  717. * Invalid: operations with mathematically invalid operands,
  718. for example 0.0/0.0, sqrt(-1.0), and log(-37.8).
  719. * Division by zero: divisor is zero and dividend is a finite nonzero number,
  720. for example 1.0/0.0.
  721. * Overflow: operation produces a result that exceeds the range of the exponent,
  722. for example MAXDOUBLE+0.0000000000001e308.
  723. * Underflow: operation produces a result that is too small to be represented
  724. as a normal number, for example, MINDOUBLE * MINDOUBLE.
  725. * Inexact: operation produces a result that cannot be represented with infinite
  726. precision, for example, 2.0 / 3.0, log(1.1) and 0.1 in input.
  727. The IEEE exceptions are either ignored during execution or mapped to the
  728. Nim exceptions: `FloatInvalidOpError`:idx:, `FloatDivByZeroError`:idx:,
  729. `FloatOverflowError`:idx:, `FloatUnderflowError`:idx:,
  730. and `FloatInexactError`:idx:.
  731. These exceptions inherit from the `FloatingPointError`:idx: base class.
  732. Nim provides the pragmas `nanChecks`:idx: and `infChecks`:idx: to control
  733. whether the IEEE exceptions are ignored or trap a Nim exception:
  734. .. code-block:: nim
  735. {.nanChecks: on, infChecks: on.}
  736. var a = 1.0
  737. var b = 0.0
  738. echo b / b # raises FloatInvalidOpError
  739. echo a / b # raises FloatOverflowError
  740. In the current implementation ``FloatDivByZeroError`` and ``FloatInexactError``
  741. are never raised. ``FloatOverflowError`` is raised instead of
  742. ``FloatDivByZeroError``.
  743. There is also a `floatChecks`:idx: pragma that is a short-cut for the
  744. combination of ``nanChecks`` and ``infChecks`` pragmas. ``floatChecks`` are
  745. turned off as default.
  746. The only operations that are affected by the ``floatChecks`` pragma are
  747. the ``+``, ``-``, ``*``, ``/`` operators for floating point types.
  748. An implementation should always use the maximum precision available to evaluate
  749. floating pointer values during semantic analysis; this means expressions like
  750. ``0.09'f32 + 0.01'f32 == 0.09'f64 + 0.01'f64`` that are evaluating during
  751. constant folding are true.
  752. Boolean type
  753. ------------
  754. The boolean type is named `bool`:idx: in Nim and can be one of the two
  755. pre-defined values ``true`` and ``false``. Conditions in ``while``,
  756. ``if``, ``elif``, ``when``-statements need to be of type ``bool``.
  757. This condition holds::
  758. ord(false) == 0 and ord(true) == 1
  759. The operators ``not, and, or, xor, <, <=, >, >=, !=, ==`` are defined
  760. for the bool type. The ``and`` and ``or`` operators perform short-cut
  761. evaluation. Example:
  762. .. code-block:: nim
  763. while p != nil and p.name != "xyz":
  764. # p.name is not evaluated if p == nil
  765. p = p.next
  766. The size of the bool type is one byte.
  767. Character type
  768. --------------
  769. The character type is named ``char`` in Nim. Its size is one byte.
  770. Thus it cannot represent an UTF-8 character, but a part of it.
  771. The reason for this is efficiency: for the overwhelming majority of use-cases,
  772. the resulting programs will still handle UTF-8 properly as UTF-8 was specially
  773. designed for this.
  774. Another reason is that Nim can support ``array[char, int]`` or
  775. ``set[char]`` efficiently as many algorithms rely on this feature. The
  776. `Rune` type is used for Unicode characters, it can represent any Unicode
  777. character. ``Rune`` is declared in the `unicode module <unicode.html>`_.
  778. Enumeration types
  779. -----------------
  780. Enumeration types define a new type whose values consist of the ones
  781. specified. The values are ordered. Example:
  782. .. code-block:: nim
  783. type
  784. Direction = enum
  785. north, east, south, west
  786. Now the following holds::
  787. ord(north) == 0
  788. ord(east) == 1
  789. ord(south) == 2
  790. ord(west) == 3
  791. # Also allowed:
  792. ord(Direction.west) == 3
  793. Thus, north < east < south < west. The comparison operators can be used
  794. with enumeration types. Instead of ``north`` etc, the enum value can also
  795. be qualified with the enum type that it resides in, ``Direction.north``.
  796. For better interfacing to other programming languages, the fields of enum
  797. types can be assigned an explicit ordinal value. However, the ordinal values
  798. have to be in ascending order. A field whose ordinal value is not
  799. explicitly given is assigned the value of the previous field + 1.
  800. An explicit ordered enum can have *holes*:
  801. .. code-block:: nim
  802. type
  803. TokenType = enum
  804. a = 2, b = 4, c = 89 # holes are valid
  805. However, it is then not an ordinal anymore, so it is not possible to use these
  806. enums as an index type for arrays. The procedures ``inc``, ``dec``, ``succ``
  807. and ``pred`` are not available for them either.
  808. The compiler supports the built-in stringify operator ``$`` for enumerations.
  809. The stringify's result can be controlled by explicitly giving the string
  810. values to use:
  811. .. code-block:: nim
  812. type
  813. MyEnum = enum
  814. valueA = (0, "my value A"),
  815. valueB = "value B",
  816. valueC = 2,
  817. valueD = (3, "abc")
  818. As can be seen from the example, it is possible to both specify a field's
  819. ordinal value and its string value by using a tuple. It is also
  820. possible to only specify one of them.
  821. An enum can be marked with the ``pure`` pragma so that it's fields are
  822. added to a special module specific hidden scope that is only queried
  823. as the last attempt. Only non-ambiguous symbols are added to this scope.
  824. But one can always access these via type qualification written
  825. as ``MyEnum.value``:
  826. .. code-block:: nim
  827. type
  828. MyEnum {.pure.} = enum
  829. valueA, valueB, valueC, valueD, amb
  830. OtherEnum {.pure.} = enum
  831. valueX, valueY, valueZ, amb
  832. echo valueA # MyEnum.valueA
  833. echo amb # Error: Unclear whether it's MyEnum.amb or OtherEnum.amb
  834. echo MyEnum.amb # OK.
  835. To implement bit fields with enums see `Bit fields <#set-type-bit-fields>`_
  836. String type
  837. -----------
  838. All string literals are of the type ``string``. A string in Nim is very
  839. similar to a sequence of characters. However, strings in Nim are both
  840. zero-terminated and have a length field. One can retrieve the length with the
  841. builtin ``len`` procedure; the length never counts the terminating zero.
  842. The terminating zero cannot be accessed unless the string is converted
  843. to the ``cstring`` type first. The terminating zero assures that this
  844. conversion can be done in O(1) and without any allocations.
  845. The assignment operator for strings always copies the string.
  846. The ``&`` operator concatenates strings.
  847. Most native Nim types support conversion to strings with the special ``$`` proc.
  848. When calling the ``echo`` proc, for example, the built-in stringify operation
  849. for the parameter is called:
  850. .. code-block:: nim
  851. echo 3 # calls `$` for `int`
  852. Whenever a user creates a specialized object, implementation of this procedure
  853. provides for ``string`` representation.
  854. .. code-block:: nim
  855. type
  856. Person = object
  857. name: string
  858. age: int
  859. proc `$`(p: Person): string = # `$` always returns a string
  860. result = p.name & " is " &
  861. $p.age & # we *need* the `$` in front of p.age which
  862. # is natively an integer to convert it to
  863. # a string
  864. " years old."
  865. While ``$p.name`` can also be used, the ``$`` operation on a string does
  866. nothing. Note that we cannot rely on automatic conversion from an ``int`` to
  867. a ``string`` like we can for the ``echo`` proc.
  868. Strings are compared by their lexicographical order. All comparison operators
  869. are available. Strings can be indexed like arrays (lower bound is 0). Unlike
  870. arrays, they can be used in case statements:
  871. .. code-block:: nim
  872. case paramStr(i)
  873. of "-v": incl(options, optVerbose)
  874. of "-h", "-?": incl(options, optHelp)
  875. else: write(stdout, "invalid command line option!\n")
  876. Per convention, all strings are UTF-8 strings, but this is not enforced. For
  877. example, when reading strings from binary files, they are merely a sequence of
  878. bytes. The index operation ``s[i]`` means the i-th *char* of ``s``, not the
  879. i-th *unichar*. The iterator ``runes`` from the `unicode module
  880. <unicode.html>`_ can be used for iteration over all Unicode characters.
  881. cstring type
  882. ------------
  883. The ``cstring`` type meaning `compatible string` is the native representation
  884. of a string for the compilation backend. For the C backend the ``cstring`` type
  885. represents a pointer to a zero-terminated char array
  886. compatible to the type ``char*`` in Ansi C. Its primary purpose lies in easy
  887. interfacing with C. The index operation ``s[i]`` means the i-th *char* of
  888. ``s``; however no bounds checking for ``cstring`` is performed making the
  889. index operation unsafe.
  890. A Nim ``string`` is implicitly convertible
  891. to ``cstring`` for convenience. If a Nim string is passed to a C-style
  892. variadic proc, it is implicitly converted to ``cstring`` too:
  893. .. code-block:: nim
  894. proc printf(formatstr: cstring) {.importc: "printf", varargs,
  895. header: "<stdio.h>".}
  896. printf("This works %s", "as expected")
  897. Even though the conversion is implicit, it is not *safe*: The garbage collector
  898. does not consider a ``cstring`` to be a root and may collect the underlying
  899. memory. However in practice this almost never happens as the GC considers
  900. stack roots conservatively. One can use the builtin procs ``GC_ref`` and
  901. ``GC_unref`` to keep the string data alive for the rare cases where it does
  902. not work.
  903. A `$` proc is defined for cstrings that returns a string. Thus to get a nim
  904. string from a cstring:
  905. .. code-block:: nim
  906. var str: string = "Hello!"
  907. var cstr: cstring = str
  908. var newstr: string = $cstr
  909. Structured types
  910. ----------------
  911. A variable of a structured type can hold multiple values at the same
  912. time. Structured types can be nested to unlimited levels. Arrays, sequences,
  913. tuples, objects and sets belong to the structured types.
  914. Array and sequence types
  915. ------------------------
  916. Arrays are a homogeneous type, meaning that each element in the array has the
  917. same type. Arrays always have a fixed length specified as a constant expression
  918. (except for open arrays). They can be indexed by any ordinal type.
  919. A parameter ``A`` may be an *open array*, in which case it is indexed by
  920. integers from 0 to ``len(A)-1``. An array expression may be constructed by the
  921. array constructor ``[]``. The element type of this array expression is
  922. inferred from the type of the first element. All other elements need to be
  923. implicitly convertable to this type.
  924. Sequences are similar to arrays but of dynamic length which may change
  925. during runtime (like strings). Sequences are implemented as growable arrays,
  926. allocating pieces of memory as items are added. A sequence ``S`` is always
  927. indexed by integers from 0 to ``len(S)-1`` and its bounds are checked.
  928. Sequences can be constructed by the array constructor ``[]`` in conjunction
  929. with the array to sequence operator ``@``. Another way to allocate space for a
  930. sequence is to call the built-in ``newSeq`` procedure.
  931. A sequence may be passed to a parameter that is of type *open array*.
  932. Example:
  933. .. code-block:: nim
  934. type
  935. IntArray = array[0..5, int] # an array that is indexed with 0..5
  936. IntSeq = seq[int] # a sequence of integers
  937. var
  938. x: IntArray
  939. y: IntSeq
  940. x = [1, 2, 3, 4, 5, 6] # [] is the array constructor
  941. y = @[1, 2, 3, 4, 5, 6] # the @ turns the array into a sequence
  942. let z = [1.0, 2, 3, 4] # the type of z is array[0..3, float]
  943. The lower bound of an array or sequence may be received by the built-in proc
  944. ``low()``, the higher bound by ``high()``. The length may be
  945. received by ``len()``. ``low()`` for a sequence or an open array always returns
  946. 0, as this is the first valid index.
  947. One can append elements to a sequence with the ``add()`` proc or the ``&``
  948. operator, and remove (and get) the last element of a sequence with the
  949. ``pop()`` proc.
  950. The notation ``x[i]`` can be used to access the i-th element of ``x``.
  951. Arrays are always bounds checked (statically or at runtime). These
  952. checks can be disabled via pragmas or invoking the compiler with the
  953. ``--boundChecks:off`` command line switch.
  954. An array constructor can have explicit indexes for readability:
  955. .. code-block:: nim
  956. type
  957. Values = enum
  958. valA, valB, valC
  959. const
  960. lookupTable = [
  961. valA: "A",
  962. valB: "B",
  963. valC: "C"
  964. ]
  965. If an index is left out, ``succ(lastIndex)`` is used as the index
  966. value:
  967. .. code-block:: nim
  968. type
  969. Values = enum
  970. valA, valB, valC, valD, valE
  971. const
  972. lookupTable = [
  973. valA: "A",
  974. "B",
  975. valC: "C",
  976. "D", "e"
  977. ]
  978. Open arrays
  979. -----------
  980. Often fixed size arrays turn out to be too inflexible; procedures should
  981. be able to deal with arrays of different sizes. The `openarray`:idx: type
  982. allows this; it can only be used for parameters. Openarrays are always
  983. indexed with an ``int`` starting at position 0. The ``len``, ``low``
  984. and ``high`` operations are available for open arrays too. Any array with
  985. a compatible base type can be passed to an openarray parameter, the index
  986. type does not matter. In addition to arrays sequences can also be passed
  987. to an open array parameter.
  988. The openarray type cannot be nested: multidimensional openarrays are not
  989. supported because this is seldom needed and cannot be done efficiently.
  990. .. code-block:: nim
  991. proc testOpenArray(x: openArray[int]) = echo repr(x)
  992. testOpenArray([1,2,3]) # array[]
  993. testOpenArray(@[1,2,3]) # seq[]
  994. Varargs
  995. -------
  996. A ``varargs`` parameter is an openarray parameter that additionally
  997. allows to pass a variable number of arguments to a procedure. The compiler
  998. converts the list of arguments to an array implicitly:
  999. .. code-block:: nim
  1000. proc myWriteln(f: File, a: varargs[string]) =
  1001. for s in items(a):
  1002. write(f, s)
  1003. write(f, "\n")
  1004. myWriteln(stdout, "abc", "def", "xyz")
  1005. # is transformed to:
  1006. myWriteln(stdout, ["abc", "def", "xyz"])
  1007. This transformation is only done if the varargs parameter is the
  1008. last parameter in the procedure header. It is also possible to perform
  1009. type conversions in this context:
  1010. .. code-block:: nim
  1011. proc myWriteln(f: File, a: varargs[string, `$`]) =
  1012. for s in items(a):
  1013. write(f, s)
  1014. write(f, "\n")
  1015. myWriteln(stdout, 123, "abc", 4.0)
  1016. # is transformed to:
  1017. myWriteln(stdout, [$123, $"def", $4.0])
  1018. In this example ``$`` is applied to any argument that is passed to the
  1019. parameter ``a``. (Note that ``$`` applied to strings is a nop.)
  1020. Note that an explicit array constructor passed to a ``varargs`` parameter is
  1021. not wrapped in another implicit array construction:
  1022. .. code-block:: nim
  1023. proc takeV[T](a: varargs[T]) = discard
  1024. takeV([123, 2, 1]) # takeV's T is "int", not "array of int"
  1025. ``varargs[typed]`` is treated specially: It matches a variable list of arguments
  1026. of arbitrary type but *always* constructs an implicit array. This is required
  1027. so that the builtin ``echo`` proc does what is expected:
  1028. .. code-block:: nim
  1029. proc echo*(x: varargs[typed, `$`]) {...}
  1030. echo @[1, 2, 3]
  1031. # prints "@[1, 2, 3]" and not "123"
  1032. Unchecked arrays
  1033. ----------------
  1034. The ``UncheckedArray[T]`` type is a special kind of ``array`` where its bounds
  1035. are not checked. This is often useful to implement customized flexibly sized
  1036. arrays. Additionally an unchecked array is translated into a C array of
  1037. undetermined size:
  1038. .. code-block:: nim
  1039. type
  1040. MySeq = object
  1041. len, cap: int
  1042. data: UncheckedArray[int]
  1043. Produces roughly this C code:
  1044. .. code-block:: C
  1045. typedef struct {
  1046. NI len;
  1047. NI cap;
  1048. NI data[];
  1049. } MySeq;
  1050. The base type of the unchecked array may not contain any GC'ed memory but this
  1051. is currently not checked.
  1052. **Future directions**: GC'ed memory should be allowed in unchecked arrays and
  1053. there should be an explicit annotation of how the GC is to determine the
  1054. runtime size of the array.
  1055. Tuples and object types
  1056. -----------------------
  1057. A variable of a tuple or object type is a heterogeneous storage
  1058. container.
  1059. A tuple or object defines various named *fields* of a type. A tuple also
  1060. defines an *order* of the fields. Tuples are meant for heterogeneous storage
  1061. types with no overhead and few abstraction possibilities. The constructor ``()``
  1062. can be used to construct tuples. The order of the fields in the constructor
  1063. must match the order of the tuple's definition. Different tuple-types are
  1064. *equivalent* if they specify the same fields of the same type in the same
  1065. order. The *names* of the fields also have to be identical.
  1066. The assignment operator for tuples copies each component.
  1067. The default assignment operator for objects copies each component. Overloading
  1068. of the assignment operator is described in `type-bound-operations-operator`_.
  1069. .. code-block:: nim
  1070. type
  1071. Person = tuple[name: string, age: int] # type representing a person:
  1072. # a person consists of a name
  1073. # and an age
  1074. var
  1075. person: Person
  1076. person = (name: "Peter", age: 30)
  1077. # the same, but less readable:
  1078. person = ("Peter", 30)
  1079. A tuple with one unnamed field can be constructed with the parentheses and a
  1080. trailing comma:
  1081. .. code-block:: nim
  1082. proc echoUnaryTuple(a: (int,)) =
  1083. echo a[0]
  1084. echoUnaryTuple (1,)
  1085. In fact, a trailing comma is allowed for every tuple construction.
  1086. The implementation aligns the fields for best access performance. The alignment
  1087. is compatible with the way the C compiler does it.
  1088. For consistency with ``object`` declarations, tuples in a ``type`` section
  1089. can also be defined with indentation instead of ``[]``:
  1090. .. code-block:: nim
  1091. type
  1092. Person = tuple # type representing a person
  1093. name: string # a person consists of a name
  1094. age: natural # and an age
  1095. Objects provide many features that tuples do not. Object provide inheritance and
  1096. information hiding. Objects have access to their type during at runtime, so that
  1097. the ``of`` operator can be used to determine the object's type. The ``of``
  1098. operator is similar to the ``instanceof`` operator in Java.
  1099. .. code-block:: nim
  1100. type
  1101. Person = object of RootObj
  1102. name*: string # the * means that `name` is accessible from other modules
  1103. age: int # no * means that the field is hidden
  1104. Student = ref object of Person # a student is a person
  1105. id: int # with an id field
  1106. var
  1107. student: Student
  1108. person: Person
  1109. assert(student of Student) # is true
  1110. assert(student of Person) # also true
  1111. Object fields that should be visible from outside the defining module, have to
  1112. be marked by ``*``. In contrast to tuples, different object types are
  1113. never *equivalent*. Objects that have no ancestor are implicitly ``final``
  1114. and thus have no hidden type field. One can use the ``inheritable`` pragma to
  1115. introduce new object roots apart from ``system.RootObj``.
  1116. Object construction
  1117. -------------------
  1118. Objects can also be created with an `object construction expression`:idx: that
  1119. has the syntax ``T(fieldA: valueA, fieldB: valueB, ...)`` where ``T`` is
  1120. an ``object`` type or a ``ref object`` type:
  1121. .. code-block:: nim
  1122. var student = Student(name: "Anton", age: 5, id: 3)
  1123. Note that, unlike tuples, objects require the field names along with their values.
  1124. For a ``ref object`` type ``system.new`` is invoked implicitly.
  1125. Object variants
  1126. ---------------
  1127. Often an object hierarchy is overkill in certain situations where simple
  1128. variant types are needed.
  1129. An example:
  1130. .. code-block:: nim
  1131. # This is an example how an abstract syntax tree could be modelled in Nim
  1132. type
  1133. NodeKind = enum # the different node types
  1134. nkInt, # a leaf with an integer value
  1135. nkFloat, # a leaf with a float value
  1136. nkString, # a leaf with a string value
  1137. nkAdd, # an addition
  1138. nkSub, # a subtraction
  1139. nkIf # an if statement
  1140. Node = ref NodeObj
  1141. NodeObj = object
  1142. case kind: NodeKind # the ``kind`` field is the discriminator
  1143. of nkInt: intVal: int
  1144. of nkFloat: floatVal: float
  1145. of nkString: strVal: string
  1146. of nkAdd, nkSub:
  1147. leftOp, rightOp: Node
  1148. of nkIf:
  1149. condition, thenPart, elsePart: Node
  1150. # create a new case object:
  1151. var n = Node(kind: nkIf, condition: nil)
  1152. # accessing n.thenPart is valid because the ``nkIf`` branch is active:
  1153. n.thenPart = Node(kind: nkFloat, floatVal: 2.0)
  1154. # the following statement raises an `FieldError` exception, because
  1155. # n.kind's value does not fit and the ``nkString`` branch is not active:
  1156. n.strVal = ""
  1157. # invalid: would change the active object branch:
  1158. n.kind = nkInt
  1159. var x = Node(kind: nkAdd, leftOp: Node(kind: nkInt, intVal: 4),
  1160. rightOp: Node(kind: nkInt, intVal: 2))
  1161. # valid: does not change the active object branch:
  1162. x.kind = nkSub
  1163. As can been seen from the example, an advantage to an object hierarchy is that
  1164. no casting between different object types is needed. Yet, access to invalid
  1165. object fields raises an exception.
  1166. The syntax of ``case`` in an object declaration follows closely the syntax of
  1167. the ``case`` statement: The branches in a ``case`` section may be indented too.
  1168. In the example the ``kind`` field is called the `discriminator`:idx:\: For
  1169. safety its address cannot be taken and assignments to it are restricted: The
  1170. new value must not lead to a change of the active object branch. Also, when the
  1171. fields of a particular branch are specified during object construction, the
  1172. corresponding discriminator value must be specified as a constant expression.
  1173. Instead of changing the active object branch, replace the old object in memory
  1174. with a new one completely:
  1175. .. code-block:: nim
  1176. var x = Node(kind: nkAdd, leftOp: Node(kind: nkInt, intVal: 4),
  1177. rightOp: Node(kind: nkInt, intVal: 2))
  1178. # change the node's contents:
  1179. x[] = NodeObj(kind: nkString, strVal: "abc")
  1180. Starting with version 0.20 ``system.reset`` cannot be used anymore to support
  1181. object branch changes as this never was completely memory safe.
  1182. As a special rule, the discriminator kind can also be bounded using a ``case``
  1183. statement. If possible values of the discriminator variable in a
  1184. ``case`` statement branch are a subset of discriminator values for the selected
  1185. object branch, the initialization is considered valid. This analysis only works
  1186. for immutable discriminators of an ordinal type and disregards ``elif``
  1187. branches.
  1188. A small example:
  1189. .. code-block:: nim
  1190. let unknownKind = nkSub
  1191. # invalid: unsafe initialization because the kind field is not statically known:
  1192. var y = Node(kind: unknownKind, strVal: "y")
  1193. var z = Node()
  1194. case unknownKind
  1195. of nkAdd, nkSub:
  1196. # valid: possible values of this branch are a subset of nkAdd/nkSub object branch:
  1197. z = Node(kind: unknownKind, leftOp: Node(), rightOp: Node())
  1198. else:
  1199. echo "ignoring: ", unknownKind
  1200. Set type
  1201. --------
  1202. .. include:: sets_fragment.txt
  1203. Reference and pointer types
  1204. ---------------------------
  1205. References (similar to pointers in other programming languages) are a
  1206. way to introduce many-to-one relationships. This means different references can
  1207. point to and modify the same location in memory (also called `aliasing`:idx:).
  1208. Nim distinguishes between `traced`:idx: and `untraced`:idx: references.
  1209. Untraced references are also called *pointers*. Traced references point to
  1210. objects of a garbage collected heap, untraced references point to
  1211. manually allocated objects or to objects somewhere else in memory. Thus
  1212. untraced references are *unsafe*. However for certain low-level operations
  1213. (accessing the hardware) untraced references are unavoidable.
  1214. Traced references are declared with the **ref** keyword, untraced references
  1215. are declared with the **ptr** keyword. In general, a `ptr T` is implicitly
  1216. convertible to the `pointer` type.
  1217. An empty subscript ``[]`` notation can be used to derefer a reference,
  1218. the ``addr`` procedure returns the address of an item. An address is always
  1219. an untraced reference.
  1220. Thus the usage of ``addr`` is an *unsafe* feature.
  1221. The ``.`` (access a tuple/object field operator)
  1222. and ``[]`` (array/string/sequence index operator) operators perform implicit
  1223. dereferencing operations for reference types:
  1224. .. code-block:: nim
  1225. type
  1226. Node = ref NodeObj
  1227. NodeObj = object
  1228. le, ri: Node
  1229. data: int
  1230. var
  1231. n: Node
  1232. new(n)
  1233. n.data = 9
  1234. # no need to write n[].data; in fact n[].data is highly discouraged!
  1235. Automatic dereferencing is also performed for the first argument of a routine
  1236. call. But currently this feature has to be only enabled
  1237. via ``{.experimental: "implicitDeref".}``:
  1238. .. code-block:: nim
  1239. {.experimental: "implicitDeref".}
  1240. proc depth(x: NodeObj): int = ...
  1241. var
  1242. n: Node
  1243. new(n)
  1244. echo n.depth
  1245. # no need to write n[].depth either
  1246. In order to simplify structural type checking, recursive tuples are not valid:
  1247. .. code-block:: nim
  1248. # invalid recursion
  1249. type MyTuple = tuple[a: ref MyTuple]
  1250. Likewise ``T = ref T`` is an invalid type.
  1251. As a syntactical extension ``object`` types can be anonymous if
  1252. declared in a type section via the ``ref object`` or ``ptr object`` notations.
  1253. This feature is useful if an object should only gain reference semantics:
  1254. .. code-block:: nim
  1255. type
  1256. Node = ref object
  1257. le, ri: Node
  1258. data: int
  1259. To allocate a new traced object, the built-in procedure ``new`` has to be used.
  1260. To deal with untraced memory, the procedures ``alloc``, ``dealloc`` and
  1261. ``realloc`` can be used. The documentation of the system module contains
  1262. further information.
  1263. Nil
  1264. ---
  1265. If a reference points to *nothing*, it has the value ``nil``. ``nil`` is also
  1266. the default value for all ``ref`` and ``ptr`` types. Dereferencing ``nil``
  1267. is an unrecoverable fatal runtime error. A dereferencing operation ``p[]``
  1268. implies that ``p`` is not nil. This can be exploited by the implementation to
  1269. optimize code like:
  1270. .. code-block:: nim
  1271. p[].field = 3
  1272. if p != nil:
  1273. # if p were nil, ``p[]`` would have caused a crash already,
  1274. # so we know ``p`` is always not nil here.
  1275. action()
  1276. Into:
  1277. .. code-block:: nim
  1278. p[].field = 3
  1279. action()
  1280. *Note*: This is not comparable to C's "undefined behavior" for
  1281. dereferencing NULL pointers.
  1282. Mixing GC'ed memory with ``ptr``
  1283. --------------------------------
  1284. Special care has to be taken if an untraced object contains traced objects like
  1285. traced references, strings or sequences: in order to free everything properly,
  1286. the built-in procedure ``GCunref`` has to be called before freeing the untraced
  1287. memory manually:
  1288. .. code-block:: nim
  1289. type
  1290. Data = tuple[x, y: int, s: string]
  1291. # allocate memory for Data on the heap:
  1292. var d = cast[ptr Data](alloc0(sizeof(Data)))
  1293. # create a new string on the garbage collected heap:
  1294. d.s = "abc"
  1295. # tell the GC that the string is not needed anymore:
  1296. GCunref(d.s)
  1297. # free the memory:
  1298. dealloc(d)
  1299. Without the ``GCunref`` call the memory allocated for the ``d.s`` string would
  1300. never be freed. The example also demonstrates two important features for low
  1301. level programming: the ``sizeof`` proc returns the size of a type or value
  1302. in bytes. The ``cast`` operator can circumvent the type system: the compiler
  1303. is forced to treat the result of the ``alloc0`` call (which returns an untyped
  1304. pointer) as if it would have the type ``ptr Data``. Casting should only be
  1305. done if it is unavoidable: it breaks type safety and bugs can lead to
  1306. mysterious crashes.
  1307. **Note**: The example only works because the memory is initialized to zero
  1308. (``alloc0`` instead of ``alloc`` does this): ``d.s`` is thus initialized to
  1309. binary zero which the string assignment can handle. One needs to know low level
  1310. details like this when mixing garbage collected data with unmanaged memory.
  1311. .. XXX finalizers for traced objects
  1312. Not nil annotation
  1313. ------------------
  1314. All types for which ``nil`` is a valid value can be annotated to
  1315. exclude ``nil`` as a valid value with the ``not nil`` annotation:
  1316. .. code-block:: nim
  1317. type
  1318. PObject = ref TObj not nil
  1319. TProc = (proc (x, y: int)) not nil
  1320. proc p(x: PObject) =
  1321. echo "not nil"
  1322. # compiler catches this:
  1323. p(nil)
  1324. # and also this:
  1325. var x: PObject
  1326. p(x)
  1327. The compiler ensures that every code path initializes variables which contain
  1328. non nilable pointers. The details of this analysis are still to be specified
  1329. here.
  1330. Procedural type
  1331. ---------------
  1332. A procedural type is internally a pointer to a procedure. ``nil`` is
  1333. an allowed value for variables of a procedural type. Nim uses procedural
  1334. types to achieve `functional`:idx: programming techniques.
  1335. Examples:
  1336. .. code-block:: nim
  1337. proc printItem(x: int) = ...
  1338. proc forEach(c: proc (x: int) {.cdecl.}) =
  1339. ...
  1340. forEach(printItem) # this will NOT compile because calling conventions differ
  1341. .. code-block:: nim
  1342. type
  1343. OnMouseMove = proc (x, y: int) {.closure.}
  1344. proc onMouseMove(mouseX, mouseY: int) =
  1345. # has default calling convention
  1346. echo "x: ", mouseX, " y: ", mouseY
  1347. proc setOnMouseMove(mouseMoveEvent: OnMouseMove) = discard
  1348. # ok, 'onMouseMove' has the default calling convention, which is compatible
  1349. # to 'closure':
  1350. setOnMouseMove(onMouseMove)
  1351. A subtle issue with procedural types is that the calling convention of the
  1352. procedure influences the type compatibility: procedural types are only
  1353. compatible if they have the same calling convention. As a special extension,
  1354. a procedure of the calling convention ``nimcall`` can be passed to a parameter
  1355. that expects a proc of the calling convention ``closure``.
  1356. Nim supports these `calling conventions`:idx:\:
  1357. `nimcall`:idx:
  1358. is the default convention used for a Nim **proc**. It is the
  1359. same as ``fastcall``, but only for C compilers that support ``fastcall``.
  1360. `closure`:idx:
  1361. is the default calling convention for a **procedural type** that lacks
  1362. any pragma annotations. It indicates that the procedure has a hidden
  1363. implicit parameter (an *environment*). Proc vars that have the calling
  1364. convention ``closure`` take up two machine words: One for the proc pointer
  1365. and another one for the pointer to implicitly passed environment.
  1366. `stdcall`:idx:
  1367. This is the stdcall convention as specified by Microsoft. The generated C
  1368. procedure is declared with the ``__stdcall`` keyword.
  1369. `cdecl`:idx:
  1370. The cdecl convention means that a procedure shall use the same convention
  1371. as the C compiler. Under Windows the generated C procedure is declared with
  1372. the ``__cdecl`` keyword.
  1373. `safecall`:idx:
  1374. This is the safecall convention as specified by Microsoft. The generated C
  1375. procedure is declared with the ``__safecall`` keyword. The word *safe*
  1376. refers to the fact that all hardware registers shall be pushed to the
  1377. hardware stack.
  1378. `inline`:idx:
  1379. The inline convention means the the caller should not call the procedure,
  1380. but inline its code directly. Note that Nim does not inline, but leaves
  1381. this to the C compiler; it generates ``__inline`` procedures. This is
  1382. only a hint for the compiler: it may completely ignore it and
  1383. it may inline procedures that are not marked as ``inline``.
  1384. `fastcall`:idx:
  1385. Fastcall means different things to different C compilers. One gets whatever
  1386. the C ``__fastcall`` means.
  1387. `syscall`:idx:
  1388. The syscall convention is the same as ``__syscall`` in C. It is used for
  1389. interrupts.
  1390. `noconv`:idx:
  1391. The generated C code will not have any explicit calling convention and thus
  1392. use the C compiler's default calling convention. This is needed because
  1393. Nim's default calling convention for procedures is ``fastcall`` to
  1394. improve speed.
  1395. Most calling conventions exist only for the Windows 32-bit platform.
  1396. The default calling convention is ``nimcall``, unless it is an inner proc (a
  1397. proc inside of a proc). For an inner proc an analysis is performed whether it
  1398. accesses its environment. If it does so, it has the calling convention
  1399. ``closure``, otherwise it has the calling convention ``nimcall``.
  1400. Distinct type
  1401. -------------
  1402. A ``distinct`` type is new type derived from a `base type`:idx: that is
  1403. incompatible with its base type. In particular, it is an essential property
  1404. of a distinct type that it **does not** imply a subtype relation between it
  1405. and its base type. Explicit type conversions from a distinct type to its
  1406. base type and vice versa are allowed. See also ``distinctBase`` to get the
  1407. reverse operation.
  1408. A distinct type is an ordinal type if its base type is an ordinal type.
  1409. Modelling currencies
  1410. ~~~~~~~~~~~~~~~~~~~~
  1411. A distinct type can be used to model different physical `units`:idx: with a
  1412. numerical base type, for example. The following example models currencies.
  1413. Different currencies should not be mixed in monetary calculations. Distinct
  1414. types are a perfect tool to model different currencies:
  1415. .. code-block:: nim
  1416. type
  1417. Dollar = distinct int
  1418. Euro = distinct int
  1419. var
  1420. d: Dollar
  1421. e: Euro
  1422. echo d + 12
  1423. # Error: cannot add a number with no unit and a ``Dollar``
  1424. Unfortunately, ``d + 12.Dollar`` is not allowed either,
  1425. because ``+`` is defined for ``int`` (among others), not for ``Dollar``. So
  1426. a ``+`` for dollars needs to be defined:
  1427. .. code-block::
  1428. proc `+` (x, y: Dollar): Dollar =
  1429. result = Dollar(int(x) + int(y))
  1430. It does not make sense to multiply a dollar with a dollar, but with a
  1431. number without unit; and the same holds for division:
  1432. .. code-block::
  1433. proc `*` (x: Dollar, y: int): Dollar =
  1434. result = Dollar(int(x) * y)
  1435. proc `*` (x: int, y: Dollar): Dollar =
  1436. result = Dollar(x * int(y))
  1437. proc `div` ...
  1438. This quickly gets tedious. The implementations are trivial and the compiler
  1439. should not generate all this code only to optimize it away later - after all
  1440. ``+`` for dollars should produce the same binary code as ``+`` for ints.
  1441. The pragma `borrow`:idx: has been designed to solve this problem; in principle
  1442. it generates the above trivial implementations:
  1443. .. code-block:: nim
  1444. proc `*` (x: Dollar, y: int): Dollar {.borrow.}
  1445. proc `*` (x: int, y: Dollar): Dollar {.borrow.}
  1446. proc `div` (x: Dollar, y: int): Dollar {.borrow.}
  1447. The ``borrow`` pragma makes the compiler use the same implementation as
  1448. the proc that deals with the distinct type's base type, so no code is
  1449. generated.
  1450. But it seems all this boilerplate code needs to be repeated for the ``Euro``
  1451. currency. This can be solved with templates_.
  1452. .. code-block:: nim
  1453. :test: "nim c $1"
  1454. template additive(typ: typedesc) =
  1455. proc `+` *(x, y: typ): typ {.borrow.}
  1456. proc `-` *(x, y: typ): typ {.borrow.}
  1457. # unary operators:
  1458. proc `+` *(x: typ): typ {.borrow.}
  1459. proc `-` *(x: typ): typ {.borrow.}
  1460. template multiplicative(typ, base: typedesc) =
  1461. proc `*` *(x: typ, y: base): typ {.borrow.}
  1462. proc `*` *(x: base, y: typ): typ {.borrow.}
  1463. proc `div` *(x: typ, y: base): typ {.borrow.}
  1464. proc `mod` *(x: typ, y: base): typ {.borrow.}
  1465. template comparable(typ: typedesc) =
  1466. proc `<` * (x, y: typ): bool {.borrow.}
  1467. proc `<=` * (x, y: typ): bool {.borrow.}
  1468. proc `==` * (x, y: typ): bool {.borrow.}
  1469. template defineCurrency(typ, base: untyped) =
  1470. type
  1471. typ* = distinct base
  1472. additive(typ)
  1473. multiplicative(typ, base)
  1474. comparable(typ)
  1475. defineCurrency(Dollar, int)
  1476. defineCurrency(Euro, int)
  1477. The borrow pragma can also be used to annotate the distinct type to allow
  1478. certain builtin operations to be lifted:
  1479. .. code-block:: nim
  1480. type
  1481. Foo = object
  1482. a, b: int
  1483. s: string
  1484. Bar {.borrow: `.`.} = distinct Foo
  1485. var bb: ref Bar
  1486. new bb
  1487. # field access now valid
  1488. bb.a = 90
  1489. bb.s = "abc"
  1490. Currently only the dot accessor can be borrowed in this way.
  1491. Avoiding SQL injection attacks
  1492. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1493. An SQL statement that is passed from Nim to an SQL database might be
  1494. modelled as a string. However, using string templates and filling in the
  1495. values is vulnerable to the famous `SQL injection attack`:idx:\:
  1496. .. code-block:: nim
  1497. import strutils
  1498. proc query(db: DbHandle, statement: string) = ...
  1499. var
  1500. username: string
  1501. db.query("SELECT FROM users WHERE name = '$1'" % username)
  1502. # Horrible security hole, but the compiler does not mind!
  1503. This can be avoided by distinguishing strings that contain SQL from strings
  1504. that don't. Distinct types provide a means to introduce a new string type
  1505. ``SQL`` that is incompatible with ``string``:
  1506. .. code-block:: nim
  1507. type
  1508. SQL = distinct string
  1509. proc query(db: DbHandle, statement: SQL) = ...
  1510. var
  1511. username: string
  1512. db.query("SELECT FROM users WHERE name = '$1'" % username)
  1513. # Static error: `query` expects an SQL string!
  1514. It is an essential property of abstract types that they **do not** imply a
  1515. subtype relation between the abstract type and its base type. Explicit type
  1516. conversions from ``string`` to ``SQL`` are allowed:
  1517. .. code-block:: nim
  1518. import strutils, sequtils
  1519. proc properQuote(s: string): SQL =
  1520. # quotes a string properly for an SQL statement
  1521. return SQL(s)
  1522. proc `%` (frmt: SQL, values: openarray[string]): SQL =
  1523. # quote each argument:
  1524. let v = values.mapIt(SQL, properQuote(it))
  1525. # we need a temporary type for the type conversion :-(
  1526. type StrSeq = seq[string]
  1527. # call strutils.`%`:
  1528. result = SQL(string(frmt) % StrSeq(v))
  1529. db.query("SELECT FROM users WHERE name = '$1'".SQL % [username])
  1530. Now we have compile-time checking against SQL injection attacks. Since
  1531. ``"".SQL`` is transformed to ``SQL("")`` no new syntax is needed for nice
  1532. looking ``SQL`` string literals. The hypothetical ``SQL`` type actually
  1533. exists in the library as the `TSqlQuery type <db_sqlite.html#TSqlQuery>`_ of
  1534. modules like `db_sqlite <db_sqlite.html>`_.
  1535. Auto type
  1536. ---------
  1537. The ``auto`` type can only be used for return types and parameters. For return
  1538. types it causes the compiler to infer the type from the routine body:
  1539. .. code-block:: nim
  1540. proc returnsInt(): auto = 1984
  1541. For parameters it currently creates implicitly generic routines:
  1542. .. code-block:: nim
  1543. proc foo(a, b: auto) = discard
  1544. Is the same as:
  1545. .. code-block:: nim
  1546. proc foo[T1, T2](a: T1, b: T2) = discard
  1547. However later versions of the language might change this to mean "infer the
  1548. parameters' types from the body". Then the above ``foo`` would be rejected as
  1549. the parameters' types can not be inferred from an empty ``discard`` statement.
  1550. Type relations
  1551. ==============
  1552. The following section defines several relations on types that are needed to
  1553. describe the type checking done by the compiler.
  1554. Type equality
  1555. -------------
  1556. Nim uses structural type equivalence for most types. Only for objects,
  1557. enumerations and distinct types name equivalence is used. The following
  1558. algorithm, *in pseudo-code*, determines type equality:
  1559. .. code-block:: nim
  1560. proc typeEqualsAux(a, b: PType,
  1561. s: var HashSet[(PType, PType)]): bool =
  1562. if (a,b) in s: return true
  1563. incl(s, (a,b))
  1564. if a.kind == b.kind:
  1565. case a.kind
  1566. of int, intXX, float, floatXX, char, string, cstring, pointer,
  1567. bool, nil, void:
  1568. # leaf type: kinds identical; nothing more to check
  1569. result = true
  1570. of ref, ptr, var, set, seq, openarray:
  1571. result = typeEqualsAux(a.baseType, b.baseType, s)
  1572. of range:
  1573. result = typeEqualsAux(a.baseType, b.baseType, s) and
  1574. (a.rangeA == b.rangeA) and (a.rangeB == b.rangeB)
  1575. of array:
  1576. result = typeEqualsAux(a.baseType, b.baseType, s) and
  1577. typeEqualsAux(a.indexType, b.indexType, s)
  1578. of tuple:
  1579. if a.tupleLen == b.tupleLen:
  1580. for i in 0..a.tupleLen-1:
  1581. if not typeEqualsAux(a[i], b[i], s): return false
  1582. result = true
  1583. of object, enum, distinct:
  1584. result = a == b
  1585. of proc:
  1586. result = typeEqualsAux(a.parameterTuple, b.parameterTuple, s) and
  1587. typeEqualsAux(a.resultType, b.resultType, s) and
  1588. a.callingConvention == b.callingConvention
  1589. proc typeEquals(a, b: PType): bool =
  1590. var s: HashSet[(PType, PType)] = {}
  1591. result = typeEqualsAux(a, b, s)
  1592. Since types are graphs which can have cycles, the above algorithm needs an
  1593. auxiliary set ``s`` to detect this case.
  1594. Type equality modulo type distinction
  1595. -------------------------------------
  1596. The following algorithm (in pseudo-code) determines whether two types
  1597. are equal with no respect to ``distinct`` types. For brevity the cycle check
  1598. with an auxiliary set ``s`` is omitted:
  1599. .. code-block:: nim
  1600. proc typeEqualsOrDistinct(a, b: PType): bool =
  1601. if a.kind == b.kind:
  1602. case a.kind
  1603. of int, intXX, float, floatXX, char, string, cstring, pointer,
  1604. bool, nil, void:
  1605. # leaf type: kinds identical; nothing more to check
  1606. result = true
  1607. of ref, ptr, var, set, seq, openarray:
  1608. result = typeEqualsOrDistinct(a.baseType, b.baseType)
  1609. of range:
  1610. result = typeEqualsOrDistinct(a.baseType, b.baseType) and
  1611. (a.rangeA == b.rangeA) and (a.rangeB == b.rangeB)
  1612. of array:
  1613. result = typeEqualsOrDistinct(a.baseType, b.baseType) and
  1614. typeEqualsOrDistinct(a.indexType, b.indexType)
  1615. of tuple:
  1616. if a.tupleLen == b.tupleLen:
  1617. for i in 0..a.tupleLen-1:
  1618. if not typeEqualsOrDistinct(a[i], b[i]): return false
  1619. result = true
  1620. of distinct:
  1621. result = typeEqualsOrDistinct(a.baseType, b.baseType)
  1622. of object, enum:
  1623. result = a == b
  1624. of proc:
  1625. result = typeEqualsOrDistinct(a.parameterTuple, b.parameterTuple) and
  1626. typeEqualsOrDistinct(a.resultType, b.resultType) and
  1627. a.callingConvention == b.callingConvention
  1628. elif a.kind == distinct:
  1629. result = typeEqualsOrDistinct(a.baseType, b)
  1630. elif b.kind == distinct:
  1631. result = typeEqualsOrDistinct(a, b.baseType)
  1632. Subtype relation
  1633. ----------------
  1634. If object ``a`` inherits from ``b``, ``a`` is a subtype of ``b``. This subtype
  1635. relation is extended to the types ``var``, ``ref``, ``ptr``:
  1636. .. code-block:: nim
  1637. proc isSubtype(a, b: PType): bool =
  1638. if a.kind == b.kind:
  1639. case a.kind
  1640. of object:
  1641. var aa = a.baseType
  1642. while aa != nil and aa != b: aa = aa.baseType
  1643. result = aa == b
  1644. of var, ref, ptr:
  1645. result = isSubtype(a.baseType, b.baseType)
  1646. .. XXX nil is a special value!
  1647. Convertible relation
  1648. --------------------
  1649. A type ``a`` is **implicitly** convertible to type ``b`` iff the following
  1650. algorithm returns true:
  1651. .. code-block:: nim
  1652. proc isImplicitlyConvertible(a, b: PType): bool =
  1653. if isSubtype(a, b) or isCovariant(a, b):
  1654. return true
  1655. case a.kind
  1656. of int: result = b in {int8, int16, int32, int64, uint, uint8, uint16,
  1657. uint32, uint64, float, float32, float64}
  1658. of int8: result = b in {int16, int32, int64, int}
  1659. of int16: result = b in {int32, int64, int}
  1660. of int32: result = b in {int64, int}
  1661. of uint: result = b in {uint32, uint64}
  1662. of uint8: result = b in {uint16, uint32, uint64}
  1663. of uint16: result = b in {uint32, uint64}
  1664. of uint32: result = b in {uint64}
  1665. of float: result = b in {float32, float64}
  1666. of float32: result = b in {float64, float}
  1667. of float64: result = b in {float32, float}
  1668. of seq:
  1669. result = b == openArray and typeEquals(a.baseType, b.baseType)
  1670. of array:
  1671. result = b == openArray and typeEquals(a.baseType, b.baseType)
  1672. if a.baseType == char and a.indexType.rangeA == 0:
  1673. result = b == cstring
  1674. of cstring, ptr:
  1675. result = b == pointer
  1676. of string:
  1677. result = b == cstring
  1678. Implicit conversions are also performed for Nim's ``range`` type
  1679. constructor.
  1680. Let ``a0``, ``b0`` of type ``T``.
  1681. Let ``A = range[a0..b0]`` be the argument's type, ``F`` the formal
  1682. parameter's type. Then an implicit conversion from ``A`` to ``F``
  1683. exists if ``a0 >= low(F) and b0 <= high(F)`` and both ``T`` and ``F``
  1684. are signed integers or if both are unsigned integers.
  1685. A type ``a`` is **explicitly** convertible to type ``b`` iff the following
  1686. algorithm returns true:
  1687. .. code-block:: nim
  1688. proc isIntegralType(t: PType): bool =
  1689. result = isOrdinal(t) or t.kind in {float, float32, float64}
  1690. proc isExplicitlyConvertible(a, b: PType): bool =
  1691. result = false
  1692. if isImplicitlyConvertible(a, b): return true
  1693. if typeEqualsOrDistinct(a, b): return true
  1694. if isIntegralType(a) and isIntegralType(b): return true
  1695. if isSubtype(a, b) or isSubtype(b, a): return true
  1696. The convertible relation can be relaxed by a user-defined type
  1697. `converter`:idx:.
  1698. .. code-block:: nim
  1699. converter toInt(x: char): int = result = ord(x)
  1700. var
  1701. x: int
  1702. chr: char = 'a'
  1703. # implicit conversion magic happens here
  1704. x = chr
  1705. echo x # => 97
  1706. # you can use the explicit form too
  1707. x = chr.toInt
  1708. echo x # => 97
  1709. The type conversion ``T(a)`` is an L-value if ``a`` is an L-value and
  1710. ``typeEqualsOrDistinct(T, type(a))`` holds.
  1711. Assignment compatibility
  1712. ------------------------
  1713. An expression ``b`` can be assigned to an expression ``a`` iff ``a`` is an
  1714. `l-value` and ``isImplicitlyConvertible(b.typ, a.typ)`` holds.
  1715. Overloading resolution
  1716. ======================
  1717. In a call ``p(args)`` the routine ``p`` that matches best is selected. If
  1718. multiple routines match equally well, the ambiguity is reported during
  1719. semantic analysis.
  1720. Every arg in args needs to match. There are multiple different categories how an
  1721. argument can match. Let ``f`` be the formal parameter's type and ``a`` the type
  1722. of the argument.
  1723. 1. Exact match: ``a`` and ``f`` are of the same type.
  1724. 2. Literal match: ``a`` is an integer literal of value ``v``
  1725. and ``f`` is a signed or unsigned integer type and ``v`` is in ``f``'s
  1726. range. Or: ``a`` is a floating point literal of value ``v``
  1727. and ``f`` is a floating point type and ``v`` is in ``f``'s
  1728. range.
  1729. 3. Generic match: ``f`` is a generic type and ``a`` matches, for
  1730. instance ``a`` is ``int`` and ``f`` is a generic (constrained) parameter
  1731. type (like in ``[T]`` or ``[T: int|char]``.
  1732. 4. Subrange or subtype match: ``a`` is a ``range[T]`` and ``T``
  1733. matches ``f`` exactly. Or: ``a`` is a subtype of ``f``.
  1734. 5. Integral conversion match: ``a`` is convertible to ``f`` and ``f`` and ``a``
  1735. is some integer or floating point type.
  1736. 6. Conversion match: ``a`` is convertible to ``f``, possibly via a user
  1737. defined ``converter``.
  1738. These matching categories have a priority: An exact match is better than a
  1739. literal match and that is better than a generic match etc. In the following
  1740. ``count(p, m)`` counts the number of matches of the matching category ``m``
  1741. for the routine ``p``.
  1742. A routine ``p`` matches better than a routine ``q`` if the following
  1743. algorithm returns true::
  1744. for each matching category m in ["exact match", "literal match",
  1745. "generic match", "subtype match",
  1746. "integral match", "conversion match"]:
  1747. if count(p, m) > count(q, m): return true
  1748. elif count(p, m) == count(q, m):
  1749. discard "continue with next category m"
  1750. else:
  1751. return false
  1752. return "ambiguous"
  1753. Some examples:
  1754. .. code-block:: nim
  1755. proc takesInt(x: int) = echo "int"
  1756. proc takesInt[T](x: T) = echo "T"
  1757. proc takesInt(x: int16) = echo "int16"
  1758. takesInt(4) # "int"
  1759. var x: int32
  1760. takesInt(x) # "T"
  1761. var y: int16
  1762. takesInt(y) # "int16"
  1763. var z: range[0..4] = 0
  1764. takesInt(z) # "T"
  1765. If this algorithm returns "ambiguous" further disambiguation is performed:
  1766. If the argument ``a`` matches both the parameter type ``f`` of ``p``
  1767. and ``g`` of ``q`` via a subtyping relation, the inheritance depth is taken
  1768. into account:
  1769. .. code-block:: nim
  1770. type
  1771. A = object of RootObj
  1772. B = object of A
  1773. C = object of B
  1774. proc p(obj: A) =
  1775. echo "A"
  1776. proc p(obj: B) =
  1777. echo "B"
  1778. var c = C()
  1779. # not ambiguous, calls 'B', not 'A' since B is a subtype of A
  1780. # but not vice versa:
  1781. p(c)
  1782. proc pp(obj: A, obj2: B) = echo "A B"
  1783. proc pp(obj: B, obj2: A) = echo "B A"
  1784. # but this is ambiguous:
  1785. pp(c, c)
  1786. Likewise for generic matches the most specialized generic type (that still
  1787. matches) is preferred:
  1788. .. code-block:: nim
  1789. proc gen[T](x: ref ref T) = echo "ref ref T"
  1790. proc gen[T](x: ref T) = echo "ref T"
  1791. proc gen[T](x: T) = echo "T"
  1792. var ri: ref int
  1793. gen(ri) # "ref T"
  1794. Overloading based on 'var T'
  1795. ----------------------------
  1796. If the formal parameter ``f`` is of type ``var T`` in addition to the ordinary
  1797. type checking, the argument is checked to be an `l-value`:idx:. ``var T``
  1798. matches better than just ``T`` then.
  1799. .. code-block:: nim
  1800. proc sayHi(x: int): string =
  1801. # matches a non-var int
  1802. result = $x
  1803. proc sayHi(x: var int): string =
  1804. # matches a var int
  1805. result = $(x + 10)
  1806. proc sayHello(x: int) =
  1807. var m = x # a mutable version of x
  1808. echo sayHi(x) # matches the non-var version of sayHi
  1809. echo sayHi(m) # matches the var version of sayHi
  1810. sayHello(3) # 3
  1811. # 13
  1812. Lazy type resolution for untyped
  1813. --------------------------------
  1814. **Note**: An `unresolved`:idx: expression is an expression for which no symbol
  1815. lookups and no type checking have been performed.
  1816. Since templates and macros that are not declared as ``immediate`` participate
  1817. in overloading resolution it's essential to have a way to pass unresolved
  1818. expressions to a template or macro. This is what the meta-type ``untyped``
  1819. accomplishes:
  1820. .. code-block:: nim
  1821. template rem(x: untyped) = discard
  1822. rem unresolvedExpression(undeclaredIdentifier)
  1823. A parameter of type ``untyped`` always matches any argument (as long as there is
  1824. any argument passed to it).
  1825. But one has to watch out because other overloads might trigger the
  1826. argument's resolution:
  1827. .. code-block:: nim
  1828. template rem(x: untyped) = discard
  1829. proc rem[T](x: T) = discard
  1830. # undeclared identifier: 'unresolvedExpression'
  1831. rem unresolvedExpression(undeclaredIdentifier)
  1832. ``untyped`` and ``varargs[untyped]`` are the only metatype that are lazy in this sense, the other
  1833. metatypes ``typed`` and ``typedesc`` are not lazy.
  1834. Varargs matching
  1835. ----------------
  1836. See `Varargs <#types-varargs>`_.
  1837. Statements and expressions
  1838. ==========================
  1839. Nim uses the common statement/expression paradigm: Statements do not
  1840. produce a value in contrast to expressions. However, some expressions are
  1841. statements.
  1842. Statements are separated into `simple statements`:idx: and
  1843. `complex statements`:idx:.
  1844. Simple statements are statements that cannot contain other statements like
  1845. assignments, calls or the ``return`` statement; complex statements can
  1846. contain other statements. To avoid the `dangling else problem`:idx:, complex
  1847. statements always have to be indented. The details can be found in the grammar.
  1848. Statement list expression
  1849. -------------------------
  1850. Statements can also occur in an expression context that looks
  1851. like ``(stmt1; stmt2; ...; ex)``. This is called
  1852. an statement list expression or ``(;)``. The type
  1853. of ``(stmt1; stmt2; ...; ex)`` is the type of ``ex``. All the other statements
  1854. must be of type ``void``. (One can use ``discard`` to produce a ``void`` type.)
  1855. ``(;)`` does not introduce a new scope.
  1856. Discard statement
  1857. -----------------
  1858. Example:
  1859. .. code-block:: nim
  1860. proc p(x, y: int): int =
  1861. result = x + y
  1862. discard p(3, 4) # discard the return value of `p`
  1863. The ``discard`` statement evaluates its expression for side-effects and
  1864. throws the expression's resulting value away.
  1865. Ignoring the return value of a procedure without using a discard statement is
  1866. a static error.
  1867. The return value can be ignored implicitly if the called proc/iterator has
  1868. been declared with the `discardable`:idx: pragma:
  1869. .. code-block:: nim
  1870. proc p(x, y: int): int {.discardable.} =
  1871. result = x + y
  1872. p(3, 4) # now valid
  1873. An empty ``discard`` statement is often used as a null statement:
  1874. .. code-block:: nim
  1875. proc classify(s: string) =
  1876. case s[0]
  1877. of SymChars, '_': echo "an identifier"
  1878. of '0'..'9': echo "a number"
  1879. else: discard
  1880. Void context
  1881. ------------
  1882. In a list of statements every expression except the last one needs to have the
  1883. type ``void``. In addition to this rule an assignment to the builtin ``result``
  1884. symbol also triggers a mandatory ``void`` context for the subsequent expressions:
  1885. .. code-block:: nim
  1886. proc invalid*(): string =
  1887. result = "foo"
  1888. "invalid" # Error: value of type 'string' has to be discarded
  1889. .. code-block:: nim
  1890. proc valid*(): string =
  1891. let x = 317
  1892. "valid"
  1893. Var statement
  1894. -------------
  1895. Var statements declare new local and global variables and
  1896. initialize them. A comma separated list of variables can be used to specify
  1897. variables of the same type:
  1898. .. code-block:: nim
  1899. var
  1900. a: int = 0
  1901. x, y, z: int
  1902. If an initializer is given the type can be omitted: the variable is then of the
  1903. same type as the initializing expression. Variables are always initialized
  1904. with a default value if there is no initializing expression. The default
  1905. value depends on the type and is always a zero in binary.
  1906. ============================ ==============================================
  1907. Type default value
  1908. ============================ ==============================================
  1909. any integer type 0
  1910. any float 0.0
  1911. char '\\0'
  1912. bool false
  1913. ref or pointer type nil
  1914. procedural type nil
  1915. sequence ``@[]``
  1916. string ``""``
  1917. tuple[x: A, y: B, ...] (default(A), default(B), ...)
  1918. (analogous for objects)
  1919. array[0..., T] [default(T), ...]
  1920. range[T] default(T); this may be out of the valid range
  1921. T = enum cast[T](0); this may be an invalid value
  1922. ============================ ==============================================
  1923. The implicit initialization can be avoided for optimization reasons with the
  1924. `noinit`:idx: pragma:
  1925. .. code-block:: nim
  1926. var
  1927. a {.noInit.}: array[0..1023, char]
  1928. If a proc is annotated with the ``noinit`` pragma this refers to its implicit
  1929. ``result`` variable:
  1930. .. code-block:: nim
  1931. proc returnUndefinedValue: int {.noinit.} = discard
  1932. The implicit initialization can be also prevented by the `requiresInit`:idx:
  1933. type pragma. The compiler requires an explicit initialization for the object
  1934. and all of its fields. However it does a `control flow analysis`:idx: to prove
  1935. the variable has been initialized and does not rely on syntactic properties:
  1936. .. code-block:: nim
  1937. type
  1938. MyObject = object {.requiresInit.}
  1939. proc p() =
  1940. # the following is valid:
  1941. var x: MyObject
  1942. if someCondition():
  1943. x = a()
  1944. else:
  1945. x = a()
  1946. # use x
  1947. Let statement
  1948. -------------
  1949. A ``let`` statement declares new local and global `single assignment`:idx:
  1950. variables and binds a value to them. The syntax is the same as that of the ``var``
  1951. statement, except that the keyword ``var`` is replaced by the keyword ``let``.
  1952. Let variables are not l-values and can thus not be passed to ``var`` parameters
  1953. nor can their address be taken. They cannot be assigned new values.
  1954. For let variables the same pragmas are available as for ordinary variables.
  1955. Tuple unpacking
  1956. ---------------
  1957. In a ``var`` or ``let`` statement tuple unpacking can be performed. The special
  1958. identifier ``_`` can be used to ignore some parts of the tuple:
  1959. .. code-block:: nim
  1960. proc returnsTuple(): (int, int, int) = (4, 2, 3)
  1961. let (x, _, z) = returnsTuple()
  1962. Const section
  1963. -------------
  1964. A const section declares constants whose values are constant expressions:
  1965. .. code-block::
  1966. import strutils
  1967. const
  1968. roundPi = 3.1415
  1969. constEval = contains("abc", 'b') # computed at compile time!
  1970. Once declared, a constant's symbol can be used as a constant expression.
  1971. See `Constants and Constant Expressions <#constants-and-constant-expressions>`_
  1972. for details.
  1973. Static statement/expression
  1974. ---------------------------
  1975. A static statement/expression explicitly requires compile-time execution.
  1976. Even some code that has side effects is permitted in a static block:
  1977. .. code-block::
  1978. static:
  1979. echo "echo at compile time"
  1980. There are limitations on what Nim code can be executed at compile time;
  1981. see `Restrictions on Compile-Time Execution
  1982. <#restrictions-on-compileminustime-execution>`_ for details.
  1983. It's a static error if the compiler cannot execute the block at compile
  1984. time.
  1985. If statement
  1986. ------------
  1987. Example:
  1988. .. code-block:: nim
  1989. var name = readLine(stdin)
  1990. if name == "Andreas":
  1991. echo "What a nice name!"
  1992. elif name == "":
  1993. echo "Don't you have a name?"
  1994. else:
  1995. echo "Boring name..."
  1996. The ``if`` statement is a simple way to make a branch in the control flow:
  1997. The expression after the keyword ``if`` is evaluated, if it is true
  1998. the corresponding statements after the ``:`` are executed. Otherwise
  1999. the expression after the ``elif`` is evaluated (if there is an
  2000. ``elif`` branch), if it is true the corresponding statements after
  2001. the ``:`` are executed. This goes on until the last ``elif``. If all
  2002. conditions fail, the ``else`` part is executed. If there is no ``else``
  2003. part, execution continues with the next statement.
  2004. In ``if`` statements new scopes begin immediately after
  2005. the ``if``/``elif``/``else`` keywords and ends after the
  2006. corresponding *then* block.
  2007. For visualization purposes the scopes have been enclosed
  2008. in ``{| |}`` in the following example:
  2009. .. code-block:: nim
  2010. if {| (let m = input =~ re"(\w+)=\w+"; m.isMatch):
  2011. echo "key ", m[0], " value ", m[1] |}
  2012. elif {| (let m = input =~ re""; m.isMatch):
  2013. echo "new m in this scope" |}
  2014. else: {|
  2015. echo "m not declared here" |}
  2016. Case statement
  2017. --------------
  2018. Example:
  2019. .. code-block:: nim
  2020. case readline(stdin)
  2021. of "delete-everything", "restart-computer":
  2022. echo "permission denied"
  2023. of "go-for-a-walk": echo "please yourself"
  2024. else: echo "unknown command"
  2025. # indentation of the branches is also allowed; and so is an optional colon
  2026. # after the selecting expression:
  2027. case readline(stdin):
  2028. of "delete-everything", "restart-computer":
  2029. echo "permission denied"
  2030. of "go-for-a-walk": echo "please yourself"
  2031. else: echo "unknown command"
  2032. The ``case`` statement is similar to the if statement, but it represents
  2033. a multi-branch selection. The expression after the keyword ``case`` is
  2034. evaluated and if its value is in a *slicelist* the corresponding statements
  2035. (after the ``of`` keyword) are executed. If the value is not in any
  2036. given *slicelist* the ``else`` part is executed. If there is no ``else``
  2037. part and not all possible values that ``expr`` can hold occur in a
  2038. ``slicelist``, a static error occurs. This holds only for expressions of
  2039. ordinal types. "All possible values" of ``expr`` are determined by ``expr``'s
  2040. type. To suppress the static error an ``else`` part with an
  2041. empty ``discard`` statement should be used.
  2042. For non ordinal types it is not possible to list every possible value and so
  2043. these always require an ``else`` part.
  2044. Because case statements are checked for exhaustiveness during semantic analysis,
  2045. the value in every ``of`` branch must be a constant expression.
  2046. This restriction also allows the compiler to generate more performant code.
  2047. As a special semantic extension, an expression in an ``of`` branch of a case
  2048. statement may evaluate to a set or array constructor; the set or array is then
  2049. expanded into a list of its elements:
  2050. .. code-block:: nim
  2051. const
  2052. SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
  2053. proc classify(s: string) =
  2054. case s[0]
  2055. of SymChars, '_': echo "an identifier"
  2056. of '0'..'9': echo "a number"
  2057. else: echo "other"
  2058. # is equivalent to:
  2059. proc classify(s: string) =
  2060. case s[0]
  2061. of 'a'..'z', 'A'..'Z', '\x80'..'\xFF', '_': echo "an identifier"
  2062. of '0'..'9': echo "a number"
  2063. else: echo "other"
  2064. When statement
  2065. --------------
  2066. Example:
  2067. .. code-block:: nim
  2068. when sizeof(int) == 2:
  2069. echo "running on a 16 bit system!"
  2070. elif sizeof(int) == 4:
  2071. echo "running on a 32 bit system!"
  2072. elif sizeof(int) == 8:
  2073. echo "running on a 64 bit system!"
  2074. else:
  2075. echo "cannot happen!"
  2076. The ``when`` statement is almost identical to the ``if`` statement with some
  2077. exceptions:
  2078. * Each condition (``expr``) has to be a constant expression (of type ``bool``).
  2079. * The statements do not open a new scope.
  2080. * The statements that belong to the expression that evaluated to true are
  2081. translated by the compiler, the other statements are not checked for
  2082. semantics! However, each condition is checked for semantics.
  2083. The ``when`` statement enables conditional compilation techniques. As
  2084. a special syntactic extension, the ``when`` construct is also available
  2085. within ``object`` definitions.
  2086. When nimvm statement
  2087. --------------------
  2088. ``nimvm`` is a special symbol, that may be used as expression of ``when nimvm``
  2089. statement to differentiate execution path between compile time and the
  2090. executable.
  2091. Example:
  2092. .. code-block:: nim
  2093. proc someProcThatMayRunInCompileTime(): bool =
  2094. when nimvm:
  2095. # This branch is taken at compile time.
  2096. result = true
  2097. else:
  2098. # This branch is taken in the executable.
  2099. result = false
  2100. const ctValue = someProcThatMayRunInCompileTime()
  2101. let rtValue = someProcThatMayRunInCompileTime()
  2102. assert(ctValue == true)
  2103. assert(rtValue == false)
  2104. ``when nimvm`` statement must meet the following requirements:
  2105. * Its expression must always be ``nimvm``. More complex expressions are not
  2106. allowed.
  2107. * It must not contain ``elif`` branches.
  2108. * It must contain ``else`` branch.
  2109. * Code in branches must not affect semantics of the code that follows the
  2110. ``when nimvm`` statement. E.g. it must not define symbols that are used in
  2111. the following code.
  2112. Return statement
  2113. ----------------
  2114. Example:
  2115. .. code-block:: nim
  2116. return 40+2
  2117. The ``return`` statement ends the execution of the current procedure.
  2118. It is only allowed in procedures. If there is an ``expr``, this is syntactic
  2119. sugar for:
  2120. .. code-block:: nim
  2121. result = expr
  2122. return result
  2123. ``return`` without an expression is a short notation for ``return result`` if
  2124. the proc has a return type. The `result`:idx: variable is always the return
  2125. value of the procedure. It is automatically declared by the compiler. As all
  2126. variables, ``result`` is initialized to (binary) zero:
  2127. .. code-block:: nim
  2128. proc returnZero(): int =
  2129. # implicitly returns 0
  2130. Yield statement
  2131. ---------------
  2132. Example:
  2133. .. code-block:: nim
  2134. yield (1, 2, 3)
  2135. The ``yield`` statement is used instead of the ``return`` statement in
  2136. iterators. It is only valid in iterators. Execution is returned to the body
  2137. of the for loop that called the iterator. Yield does not end the iteration
  2138. process, but execution is passed back to the iterator if the next iteration
  2139. starts. See the section about iterators (`Iterators and the for statement`_)
  2140. for further information.
  2141. Block statement
  2142. ---------------
  2143. Example:
  2144. .. code-block:: nim
  2145. var found = false
  2146. block myblock:
  2147. for i in 0..3:
  2148. for j in 0..3:
  2149. if a[j][i] == 7:
  2150. found = true
  2151. break myblock # leave the block, in this case both for-loops
  2152. echo found
  2153. The block statement is a means to group statements to a (named) ``block``.
  2154. Inside the block, the ``break`` statement is allowed to leave the block
  2155. immediately. A ``break`` statement can contain a name of a surrounding
  2156. block to specify which block is to leave.
  2157. Break statement
  2158. ---------------
  2159. Example:
  2160. .. code-block:: nim
  2161. break
  2162. The ``break`` statement is used to leave a block immediately. If ``symbol``
  2163. is given, it is the name of the enclosing block that is to leave. If it is
  2164. absent, the innermost block is left.
  2165. While statement
  2166. ---------------
  2167. Example:
  2168. .. code-block:: nim
  2169. echo "Please tell me your password:"
  2170. var pw = readLine(stdin)
  2171. while pw != "12345":
  2172. echo "Wrong password! Next try:"
  2173. pw = readLine(stdin)
  2174. The ``while`` statement is executed until the ``expr`` evaluates to false.
  2175. Endless loops are no error. ``while`` statements open an `implicit block`,
  2176. so that they can be left with a ``break`` statement.
  2177. Continue statement
  2178. ------------------
  2179. A ``continue`` statement leads to the immediate next iteration of the
  2180. surrounding loop construct. It is only allowed within a loop. A continue
  2181. statement is syntactic sugar for a nested block:
  2182. .. code-block:: nim
  2183. while expr1:
  2184. stmt1
  2185. continue
  2186. stmt2
  2187. Is equivalent to:
  2188. .. code-block:: nim
  2189. while expr1:
  2190. block myBlockName:
  2191. stmt1
  2192. break myBlockName
  2193. stmt2
  2194. Assembler statement
  2195. -------------------
  2196. The direct embedding of assembler code into Nim code is supported
  2197. by the unsafe ``asm`` statement. Identifiers in the assembler code that refer to
  2198. Nim identifiers shall be enclosed in a special character which can be
  2199. specified in the statement's pragmas. The default special character is ``'`'``:
  2200. .. code-block:: nim
  2201. {.push stackTrace:off.}
  2202. proc addInt(a, b: int): int =
  2203. # a in eax, and b in edx
  2204. asm """
  2205. mov eax, `a`
  2206. add eax, `b`
  2207. jno theEnd
  2208. call `raiseOverflow`
  2209. theEnd:
  2210. """
  2211. {.pop.}
  2212. If the GNU assembler is used, quotes and newlines are inserted automatically:
  2213. .. code-block:: nim
  2214. proc addInt(a, b: int): int =
  2215. asm """
  2216. addl %%ecx, %%eax
  2217. jno 1
  2218. call `raiseOverflow`
  2219. 1:
  2220. :"=a"(`result`)
  2221. :"a"(`a`), "c"(`b`)
  2222. """
  2223. Instead of:
  2224. .. code-block:: nim
  2225. proc addInt(a, b: int): int =
  2226. asm """
  2227. "addl %%ecx, %%eax\n"
  2228. "jno 1\n"
  2229. "call `raiseOverflow`\n"
  2230. "1: \n"
  2231. :"=a"(`result`)
  2232. :"a"(`a`), "c"(`b`)
  2233. """
  2234. Using statement
  2235. ---------------
  2236. The using statement provides syntactic convenience in modules where
  2237. the same parameter names and types are used over and over. Instead of:
  2238. .. code-block:: nim
  2239. proc foo(c: Context; n: Node) = ...
  2240. proc bar(c: Context; n: Node, counter: int) = ...
  2241. proc baz(c: Context; n: Node) = ...
  2242. One can tell the compiler about the convention that a parameter of
  2243. name ``c`` should default to type ``Context``, ``n`` should default to
  2244. ``Node`` etc.:
  2245. .. code-block:: nim
  2246. using
  2247. c: Context
  2248. n: Node
  2249. counter: int
  2250. proc foo(c, n) = ...
  2251. proc bar(c, n, counter) = ...
  2252. proc baz(c, n) = ...
  2253. proc mixedMode(c, n; x, y: int) =
  2254. # 'c' is inferred to be of the type 'Context'
  2255. # 'n' is inferred to be of the type 'Node'
  2256. # But 'x' and 'y' are of type 'int'.
  2257. The ``using`` section uses the same indentation based grouping syntax as
  2258. a ``var`` or ``let`` section.
  2259. Note that ``using`` is not applied for ``template`` since untyped template
  2260. parameters default to the type ``system.untyped``.
  2261. Mixing parameters that should use the ``using`` declaration with parameters
  2262. that are explicitly typed is possible and requires a semicolon between them.
  2263. If expression
  2264. -------------
  2265. An `if expression` is almost like an if statement, but it is an expression.
  2266. Example:
  2267. .. code-block:: nim
  2268. var y = if x > 8: 9 else: 10
  2269. An if expression always results in a value, so the ``else`` part is
  2270. required. ``Elif`` parts are also allowed.
  2271. When expression
  2272. ---------------
  2273. Just like an `if expression`, but corresponding to the when statement.
  2274. Case expression
  2275. ---------------
  2276. The `case expression` is again very similar to the case statement:
  2277. .. code-block:: nim
  2278. var favoriteFood = case animal
  2279. of "dog": "bones"
  2280. of "cat": "mice"
  2281. elif animal.endsWith"whale": "plankton"
  2282. else:
  2283. echo "I'm not sure what to serve, but everybody loves ice cream"
  2284. "ice cream"
  2285. As seen in the above example, the case expression can also introduce side
  2286. effects. When multiple statements are given for a branch, Nim will use
  2287. the last expression as the result value.
  2288. Table constructor
  2289. -----------------
  2290. A table constructor is syntactic sugar for an array constructor:
  2291. .. code-block:: nim
  2292. {"key1": "value1", "key2", "key3": "value2"}
  2293. # is the same as:
  2294. [("key1", "value1"), ("key2", "value2"), ("key3", "value2")]
  2295. The empty table can be written ``{:}`` (in contrast to the empty set
  2296. which is ``{}``) which is thus another way to write as the empty array
  2297. constructor ``[]``. This slightly unusual way of supporting tables
  2298. has lots of advantages:
  2299. * The order of the (key,value)-pairs is preserved, thus it is easy to
  2300. support ordered dicts with for example ``{key: val}.newOrderedTable``.
  2301. * A table literal can be put into a ``const`` section and the compiler
  2302. can easily put it into the executable's data section just like it can
  2303. for arrays and the generated data section requires a minimal amount
  2304. of memory.
  2305. * Every table implementation is treated equal syntactically.
  2306. * Apart from the minimal syntactic sugar the language core does not need to
  2307. know about tables.
  2308. Type conversions
  2309. ----------------
  2310. Syntactically a `type conversion` is like a procedure call, but a
  2311. type name replaces the procedure name. A type conversion is always
  2312. safe in the sense that a failure to convert a type to another
  2313. results in an exception (if it cannot be determined statically).
  2314. Ordinary procs are often preferred over type conversions in Nim: For instance,
  2315. ``$`` is the ``toString`` operator by convention and ``toFloat`` and ``toInt``
  2316. can be used to convert from floating point to integer or vice versa.
  2317. A type conversion can also be used to disambiguate overloaded routines:
  2318. .. code-block:: nim
  2319. proc p(x: int) = echo "int"
  2320. proc p(x: string) = echo "string"
  2321. let procVar = (proc(x: string))(p)
  2322. procVar("a")
  2323. Type casts
  2324. ----------
  2325. Example:
  2326. .. code-block:: nim
  2327. cast[int](x)
  2328. Type casts are a crude mechanism to interpret the bit pattern of
  2329. an expression as if it would be of another type. Type casts are
  2330. only needed for low-level programming and are inherently unsafe.
  2331. The addr operator
  2332. -----------------
  2333. The ``addr`` operator returns the address of an l-value. If the type of the
  2334. location is ``T``, the `addr` operator result is of the type ``ptr T``. An
  2335. address is always an untraced reference. Taking the address of an object that
  2336. resides on the stack is **unsafe**, as the pointer may live longer than the
  2337. object on the stack and can thus reference a non-existing object. One can get
  2338. the address of variables, but one can't use it on variables declared through
  2339. ``let`` statements:
  2340. .. code-block:: nim
  2341. let t1 = "Hello"
  2342. var
  2343. t2 = t1
  2344. t3 : pointer = addr(t2)
  2345. echo repr(addr(t2))
  2346. # --> ref 0x7fff6b71b670 --> 0x10bb81050"Hello"
  2347. echo cast[ptr string](t3)[]
  2348. # --> Hello
  2349. # The following line doesn't compile:
  2350. echo repr(addr(t1))
  2351. # Error: expression has no address
  2352. The unsafeAddr operator
  2353. -----------------------
  2354. For easier interoperability with other compiled languages such as C, retrieving
  2355. the address of a ``let`` variable, a parameter or a ``for`` loop variable, the
  2356. ``unsafeAddr`` operation can be used:
  2357. .. code-block:: nim
  2358. let myArray = [1, 2, 3]
  2359. foreignProcThatTakesAnAddr(unsafeAddr myArray)
  2360. Procedures
  2361. ==========
  2362. What most programming languages call `methods`:idx: or `functions`:idx: are
  2363. called `procedures`:idx: in Nim. A procedure
  2364. declaration consists of an identifier, zero or more formal parameters, a return
  2365. value type and a block of code. Formal parameters are declared as a list of
  2366. identifiers separated by either comma or semicolon. A parameter is given a type
  2367. by ``: typename``. The type applies to all parameters immediately before it,
  2368. until either the beginning of the parameter list, a semicolon separator or an
  2369. already typed parameter, is reached. The semicolon can be used to make
  2370. separation of types and subsequent identifiers more distinct.
  2371. .. code-block:: nim
  2372. # Using only commas
  2373. proc foo(a, b: int, c, d: bool): int
  2374. # Using semicolon for visual distinction
  2375. proc foo(a, b: int; c, d: bool): int
  2376. # Will fail: a is untyped since ';' stops type propagation.
  2377. proc foo(a; b: int; c, d: bool): int
  2378. A parameter may be declared with a default value which is used if the caller
  2379. does not provide a value for the argument.
  2380. .. code-block:: nim
  2381. # b is optional with 47 as its default value
  2382. proc foo(a: int, b: int = 47): int
  2383. Parameters can be declared mutable and so allow the proc to modify those
  2384. arguments, by using the type modifier `var`.
  2385. .. code-block:: nim
  2386. # "returning" a value to the caller through the 2nd argument
  2387. # Notice that the function uses no actual return value at all (ie void)
  2388. proc foo(inp: int, outp: var int) =
  2389. outp = inp + 47
  2390. If the proc declaration has no body, it is a `forward`:idx: declaration. If the
  2391. proc returns a value, the procedure body can access an implicitly declared
  2392. variable named `result`:idx: that represents the return value. Procs can be
  2393. overloaded. The overloading resolution algorithm determines which proc is the
  2394. best match for the arguments. Example:
  2395. .. code-block:: nim
  2396. proc toLower(c: char): char = # toLower for characters
  2397. if c in {'A'..'Z'}:
  2398. result = chr(ord(c) + (ord('a') - ord('A')))
  2399. else:
  2400. result = c
  2401. proc toLower(s: string): string = # toLower for strings
  2402. result = newString(len(s))
  2403. for i in 0..len(s) - 1:
  2404. result[i] = toLower(s[i]) # calls toLower for characters; no recursion!
  2405. Calling a procedure can be done in many different ways:
  2406. .. code-block:: nim
  2407. proc callme(x, y: int, s: string = "", c: char, b: bool = false) = ...
  2408. # call with positional arguments # parameter bindings:
  2409. callme(0, 1, "abc", '\t', true) # (x=0, y=1, s="abc", c='\t', b=true)
  2410. # call with named and positional arguments:
  2411. callme(y=1, x=0, "abd", '\t') # (x=0, y=1, s="abd", c='\t', b=false)
  2412. # call with named arguments (order is not relevant):
  2413. callme(c='\t', y=1, x=0) # (x=0, y=1, s="", c='\t', b=false)
  2414. # call as a command statement: no () needed:
  2415. callme 0, 1, "abc", '\t' # (x=0, y=1, s="abc", c='\t', b=false)
  2416. A procedure may call itself recursively.
  2417. `Operators`:idx: are procedures with a special operator symbol as identifier:
  2418. .. code-block:: nim
  2419. proc `$` (x: int): string =
  2420. # converts an integer to a string; this is a prefix operator.
  2421. result = intToStr(x)
  2422. Operators with one parameter are prefix operators, operators with two
  2423. parameters are infix operators. (However, the parser distinguishes these from
  2424. the operator's position within an expression.) There is no way to declare
  2425. postfix operators: all postfix operators are built-in and handled by the
  2426. grammar explicitly.
  2427. Any operator can be called like an ordinary proc with the '`opr`'
  2428. notation. (Thus an operator can have more than two parameters):
  2429. .. code-block:: nim
  2430. proc `*+` (a, b, c: int): int =
  2431. # Multiply and add
  2432. result = a * b + c
  2433. assert `*+`(3, 4, 6) == `+`(`*`(a, b), c)
  2434. Export marker
  2435. -------------
  2436. If a declared symbol is marked with an `asterisk`:idx: it is exported from the
  2437. current module:
  2438. .. code-block:: nim
  2439. proc exportedEcho*(s: string) = echo s
  2440. proc `*`*(a: string; b: int): string =
  2441. result = newStringOfCap(a.len * b)
  2442. for i in 1..b: result.add a
  2443. var exportedVar*: int
  2444. const exportedConst* = 78
  2445. type
  2446. ExportedType* = object
  2447. exportedField*: int
  2448. Method call syntax
  2449. ------------------
  2450. For object oriented programming, the syntax ``obj.method(args)`` can be used
  2451. instead of ``method(obj, args)``. The parentheses can be omitted if there are no
  2452. remaining arguments: ``obj.len`` (instead of ``len(obj)``).
  2453. This method call syntax is not restricted to objects, it can be used
  2454. to supply any type of first argument for procedures:
  2455. .. code-block:: nim
  2456. echo "abc".len # is the same as echo len "abc"
  2457. echo "abc".toUpper()
  2458. echo {'a', 'b', 'c'}.card
  2459. stdout.writeLine("Hallo") # the same as writeLine(stdout, "Hallo")
  2460. Another way to look at the method call syntax is that it provides the missing
  2461. postfix notation.
  2462. The method call syntax conflicts with explicit generic instantiations:
  2463. ``p[T](x)`` cannot be written as ``x.p[T]`` because ``x.p[T]`` is always
  2464. parsed as ``(x.p)[T]``.
  2465. See also: `Limitations of the method call syntax
  2466. <#templates-limitations-of-the-method-call-syntax>`_.
  2467. The ``[: ]`` notation has been designed to mitigate this issue: ``x.p[:T]``
  2468. is rewritten by the parser to ``p[T](x)``, ``x.p[:T](y)`` is rewritten to
  2469. ``p[T](x, y)``. Note that ``[: ]`` has no AST representation, the rewrite
  2470. is performed directly in the parsing step.
  2471. Properties
  2472. ----------
  2473. Nim has no need for *get-properties*: Ordinary get-procedures that are called
  2474. with the *method call syntax* achieve the same. But setting a value is
  2475. different; for this a special setter syntax is needed:
  2476. .. code-block:: nim
  2477. # Module asocket
  2478. type
  2479. Socket* = ref object of RootObj
  2480. host: int # cannot be accessed from the outside of the module
  2481. proc `host=`*(s: var Socket, value: int) {.inline.} =
  2482. ## setter of hostAddr.
  2483. ## This accesses the 'host' field and is not a recursive call to
  2484. ## ``host=`` because the builtin dot access is preferred if it is
  2485. ## avaliable:
  2486. s.host = value
  2487. proc host*(s: Socket): int {.inline.} =
  2488. ## getter of hostAddr
  2489. ## This accesses the 'host' field and is not a recursive call to
  2490. ## ``host`` because the builtin dot access is preferred if it is
  2491. ## avaliable:
  2492. s.host
  2493. .. code-block:: nim
  2494. # module B
  2495. import asocket
  2496. var s: Socket
  2497. new s
  2498. s.host = 34 # same as `host=`(s, 34)
  2499. A proc defined as ``f=`` (with the trailing ``=``) is called
  2500. a `setter`:idx:. A setter can be called explicitly via the common
  2501. backticks notation:
  2502. .. code-block:: nim
  2503. proc `f=`(x: MyObject; value: string) =
  2504. discard
  2505. `f=`(myObject, "value")
  2506. ``f=`` can be called implicitly in the pattern
  2507. ``x.f = value`` if and only if the type of ``x`` does not have a field
  2508. named ``f`` or if ``f`` is not visible in the current module. These rules
  2509. ensure that object fields and accessors can have the same name. Within the
  2510. module ``x.f`` is then always interpreted as field access and outside the
  2511. module it is interpreted as an accessor proc call.
  2512. Command invocation syntax
  2513. -------------------------
  2514. Routines can be invoked without the ``()`` if the call is syntactically
  2515. a statement. This command invocation syntax also works for
  2516. expressions, but then only a single argument may follow. This restriction
  2517. means ``echo f 1, f 2`` is parsed as ``echo(f(1), f(2))`` and not as
  2518. ``echo(f(1, f(2)))``. The method call syntax may be used to provide one
  2519. more argument in this case:
  2520. .. code-block:: nim
  2521. proc optarg(x: int, y: int = 0): int = x + y
  2522. proc singlearg(x: int): int = 20*x
  2523. echo optarg 1, " ", singlearg 2 # prints "1 40"
  2524. let fail = optarg 1, optarg 8 # Wrong. Too many arguments for a command call
  2525. let x = optarg(1, optarg 8) # traditional procedure call with 2 arguments
  2526. let y = 1.optarg optarg 8 # same thing as above, w/o the parenthesis
  2527. assert x == y
  2528. The command invocation syntax also can't have complex expressions as arguments.
  2529. For example: (`anonymous procs`_), ``if``, ``case`` or ``try``.
  2530. Function calls with no arguments still needs () to
  2531. distinguish between a call and the function itself as a first class value.
  2532. Closures
  2533. --------
  2534. Procedures can appear at the top level in a module as well as inside other
  2535. scopes, in which case they are called nested procs. A nested proc can access
  2536. local variables from its enclosing scope and if it does so it becomes a
  2537. closure. Any captured variables are stored in a hidden additional argument
  2538. to the closure (its environment) and they are accessed by reference by both
  2539. the closure and its enclosing scope (i.e. any modifications made to them are
  2540. visible in both places). The closure environment may be allocated on the heap
  2541. or on the stack if the compiler determines that this would be safe.
  2542. Creating closures in loops
  2543. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  2544. Since closures capture local variables by reference it is often not wanted
  2545. behavior inside loop bodies. See `closureScope <system.html#closureScope>`_
  2546. for details on how to change this behavior.
  2547. Anonymous Procs
  2548. ---------------
  2549. Procs can also be treated as expressions, in which case it's allowed to omit
  2550. the proc's name.
  2551. .. code-block:: nim
  2552. var cities = @["Frankfurt", "Tokyo", "New York", "Kyiv"]
  2553. cities.sort(proc (x,y: string): int =
  2554. cmp(x.len, y.len))
  2555. Procs as expressions can appear both as nested procs and inside top level
  2556. executable code.
  2557. Func
  2558. ----
  2559. The ``func`` keyword introduces a shortcut for a `noSideEffect`:idx: proc.
  2560. .. code-block:: nim
  2561. func binarySearch[T](a: openArray[T]; elem: T): int
  2562. Is short for:
  2563. .. code-block:: nim
  2564. proc binarySearch[T](a: openArray[T]; elem: T): int {.noSideEffect.}
  2565. Nonoverloadable builtins
  2566. ------------------------
  2567. The following builtin procs cannot be overloaded for reasons of implementation
  2568. simplicity (they require specialized semantic checking)::
  2569. declared, defined, definedInScope, compiles, sizeOf,
  2570. is, shallowCopy, getAst, astToStr, spawn, procCall
  2571. Thus they act more like keywords than like ordinary identifiers; unlike a
  2572. keyword however, a redefinition may `shadow`:idx: the definition in
  2573. the ``system`` module. From this list the following should not be written in dot
  2574. notation ``x.f`` since ``x`` cannot be type checked before it gets passed
  2575. to ``f``::
  2576. declared, defined, definedInScope, compiles, getAst, astToStr
  2577. Var parameters
  2578. --------------
  2579. The type of a parameter may be prefixed with the ``var`` keyword:
  2580. .. code-block:: nim
  2581. proc divmod(a, b: int; res, remainder: var int) =
  2582. res = a div b
  2583. remainder = a mod b
  2584. var
  2585. x, y: int
  2586. divmod(8, 5, x, y) # modifies x and y
  2587. assert x == 1
  2588. assert y == 3
  2589. In the example, ``res`` and ``remainder`` are `var parameters`.
  2590. Var parameters can be modified by the procedure and the changes are
  2591. visible to the caller. The argument passed to a var parameter has to be
  2592. an l-value. Var parameters are implemented as hidden pointers. The
  2593. above example is equivalent to:
  2594. .. code-block:: nim
  2595. proc divmod(a, b: int; res, remainder: ptr int) =
  2596. res[] = a div b
  2597. remainder[] = a mod b
  2598. var
  2599. x, y: int
  2600. divmod(8, 5, addr(x), addr(y))
  2601. assert x == 1
  2602. assert y == 3
  2603. In the examples, var parameters or pointers are used to provide two
  2604. return values. This can be done in a cleaner way by returning a tuple:
  2605. .. code-block:: nim
  2606. proc divmod(a, b: int): tuple[res, remainder: int] =
  2607. (a div b, a mod b)
  2608. var t = divmod(8, 5)
  2609. assert t.res == 1
  2610. assert t.remainder == 3
  2611. One can use `tuple unpacking`:idx: to access the tuple's fields:
  2612. .. code-block:: nim
  2613. var (x, y) = divmod(8, 5) # tuple unpacking
  2614. assert x == 1
  2615. assert y == 3
  2616. **Note**: ``var`` parameters are never necessary for efficient parameter
  2617. passing. Since non-var parameters cannot be modified the compiler is always
  2618. free to pass arguments by reference if it considers it can speed up execution.
  2619. Var return type
  2620. ---------------
  2621. A proc, converter or iterator may return a ``var`` type which means that the
  2622. returned value is an l-value and can be modified by the caller:
  2623. .. code-block:: nim
  2624. var g = 0
  2625. proc writeAccessToG(): var int =
  2626. result = g
  2627. writeAccessToG() = 6
  2628. assert g == 6
  2629. It is a static error if the implicitly introduced pointer could be
  2630. used to access a location beyond its lifetime:
  2631. .. code-block:: nim
  2632. proc writeAccessToG(): var int =
  2633. var g = 0
  2634. result = g # Error!
  2635. For iterators, a component of a tuple return type can have a ``var`` type too:
  2636. .. code-block:: nim
  2637. iterator mpairs(a: var seq[string]): tuple[key: int, val: var string] =
  2638. for i in 0..a.high:
  2639. yield (i, a[i])
  2640. In the standard library every name of a routine that returns a ``var`` type
  2641. starts with the prefix ``m`` per convention.
  2642. .. include:: manual/var_t_return.rst
  2643. Future directions
  2644. ~~~~~~~~~~~~~~~~~
  2645. Later versions of Nim can be more precise about the borrowing rule with
  2646. a syntax like:
  2647. .. code-block:: nim
  2648. proc foo(other: Y; container: var X): var T from container
  2649. Here ``var T from container`` explicitly exposes that the
  2650. location is deviated from the second parameter (called
  2651. 'container' in this case). The syntax ``var T from p`` specifies a type
  2652. ``varTy[T, 2]`` which is incompatible with ``varTy[T, 1]``.
  2653. Overloading of the subscript operator
  2654. -------------------------------------
  2655. The ``[]`` subscript operator for arrays/openarrays/sequences can be overloaded.
  2656. Multi-methods
  2657. =============
  2658. **Note:** Starting from Nim 0.20, to use multi-methods one must explicitly pass
  2659. ``--multimethods:on`` when compiling.
  2660. Procedures always use static dispatch. Multi-methods use dynamic
  2661. dispatch. For dynamic dispatch to work on an object it should be a reference
  2662. type.
  2663. .. code-block:: nim
  2664. type
  2665. Expression = ref object of RootObj ## abstract base class for an expression
  2666. Literal = ref object of Expression
  2667. x: int
  2668. PlusExpr = ref object of Expression
  2669. a, b: Expression
  2670. method eval(e: Expression): int {.base.} =
  2671. # override this base method
  2672. quit "to override!"
  2673. method eval(e: Literal): int = return e.x
  2674. method eval(e: PlusExpr): int =
  2675. # watch out: relies on dynamic binding
  2676. result = eval(e.a) + eval(e.b)
  2677. proc newLit(x: int): Literal =
  2678. new(result)
  2679. result.x = x
  2680. proc newPlus(a, b: Expression): PlusExpr =
  2681. new(result)
  2682. result.a = a
  2683. result.b = b
  2684. echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4)))
  2685. In the example the constructors ``newLit`` and ``newPlus`` are procs
  2686. because they should use static binding, but ``eval`` is a method because it
  2687. requires dynamic binding.
  2688. As can be seen in the example, base methods have to be annotated with
  2689. the `base`:idx: pragma. The ``base`` pragma also acts as a reminder for the
  2690. programmer that a base method ``m`` is used as the foundation to determine all
  2691. the effects that a call to ``m`` might cause.
  2692. **Note**: Compile-time execution is not (yet) supported for methods.
  2693. **Note**: Starting from Nim 0.20, generic methods are deprecated.
  2694. Inhibit dynamic method resolution via procCall
  2695. -----------------------------------------------
  2696. Dynamic method resolution can be inhibited via the builtin `system.procCall`:idx:.
  2697. This is somewhat comparable to the `super`:idx: keyword that traditional OOP
  2698. languages offer.
  2699. .. code-block:: nim
  2700. :test: "nim c $1"
  2701. type
  2702. Thing = ref object of RootObj
  2703. Unit = ref object of Thing
  2704. x: int
  2705. method m(a: Thing) {.base.} =
  2706. echo "base"
  2707. method m(a: Unit) =
  2708. # Call the base method:
  2709. procCall m(Thing(a))
  2710. echo "1"
  2711. Iterators and the for statement
  2712. ===============================
  2713. The `for`:idx: statement is an abstract mechanism to iterate over the elements
  2714. of a container. It relies on an `iterator`:idx: to do so. Like ``while``
  2715. statements, ``for`` statements open an `implicit block`:idx:, so that they
  2716. can be left with a ``break`` statement.
  2717. The ``for`` loop declares iteration variables - their scope reaches until the
  2718. end of the loop body. The iteration variables' types are inferred by the
  2719. return type of the iterator.
  2720. An iterator is similar to a procedure, except that it can be called in the
  2721. context of a ``for`` loop. Iterators provide a way to specify the iteration over
  2722. an abstract type. A key role in the execution of a ``for`` loop plays the
  2723. ``yield`` statement in the called iterator. Whenever a ``yield`` statement is
  2724. reached the data is bound to the ``for`` loop variables and control continues
  2725. in the body of the ``for`` loop. The iterator's local variables and execution
  2726. state are automatically saved between calls. Example:
  2727. .. code-block:: nim
  2728. # this definition exists in the system module
  2729. iterator items*(a: string): char {.inline.} =
  2730. var i = 0
  2731. while i < len(a):
  2732. yield a[i]
  2733. inc(i)
  2734. for ch in items("hello world"): # `ch` is an iteration variable
  2735. echo ch
  2736. The compiler generates code as if the programmer would have written this:
  2737. .. code-block:: nim
  2738. var i = 0
  2739. while i < len(a):
  2740. var ch = a[i]
  2741. echo ch
  2742. inc(i)
  2743. If the iterator yields a tuple, there can be as many iteration variables
  2744. as there are components in the tuple. The i'th iteration variable's type is
  2745. the type of the i'th component. In other words, implicit tuple unpacking in a
  2746. for loop context is supported.
  2747. Implict items/pairs invocations
  2748. -------------------------------
  2749. If the for loop expression ``e`` does not denote an iterator and the for loop
  2750. has exactly 1 variable, the for loop expression is rewritten to ``items(e)``;
  2751. ie. an ``items`` iterator is implicitly invoked:
  2752. .. code-block:: nim
  2753. for x in [1,2,3]: echo x
  2754. If the for loop has exactly 2 variables, a ``pairs`` iterator is implicitly
  2755. invoked.
  2756. Symbol lookup of the identifiers ``items``/``pairs`` is performed after
  2757. the rewriting step, so that all overloads of ``items``/``pairs`` are taken
  2758. into account.
  2759. First class iterators
  2760. ---------------------
  2761. There are 2 kinds of iterators in Nim: *inline* and *closure* iterators.
  2762. An `inline iterator`:idx: is an iterator that's always inlined by the compiler
  2763. leading to zero overhead for the abstraction, but may result in a heavy
  2764. increase in code size.
  2765. Caution: the body of a for loop over an inline iterator is inlined into
  2766. each ``yield`` statement appearing in the iterator code,
  2767. so ideally the code should be refactored to contain a single yield when possible
  2768. to avoid code bloat.
  2769. Inline iterators are second class citizens;
  2770. They can be passed as parameters only to other inlining code facilities like
  2771. templates, macros and other inline iterators.
  2772. In contrast to that, a `closure iterator`:idx: can be passed around more freely:
  2773. .. code-block:: nim
  2774. iterator count0(): int {.closure.} =
  2775. yield 0
  2776. iterator count2(): int {.closure.} =
  2777. var x = 1
  2778. yield x
  2779. inc x
  2780. yield x
  2781. proc invoke(iter: iterator(): int {.closure.}) =
  2782. for x in iter(): echo x
  2783. invoke(count0)
  2784. invoke(count2)
  2785. Closure iterators and inline iterators have some restrictions:
  2786. 1. For now, a closure iterator cannot be executed at compile time.
  2787. 2. ``return`` is allowed in a closure iterator but not in an inline iterator
  2788. (but rarely useful) and ends the iteration.
  2789. 3. Neither inline nor closure iterators can be recursive.
  2790. 4. Neither inline nor closure iterators have the special ``result`` variable.
  2791. 5. Closure iterators are not supported by the js backend.
  2792. Iterators that are neither marked ``{.closure.}`` nor ``{.inline.}`` explicitly
  2793. default to being inline, but this may change in future versions of the
  2794. implementation.
  2795. The ``iterator`` type is always of the calling convention ``closure``
  2796. implicitly; the following example shows how to use iterators to implement
  2797. a `collaborative tasking`:idx: system:
  2798. .. code-block:: nim
  2799. # simple tasking:
  2800. type
  2801. Task = iterator (ticker: int)
  2802. iterator a1(ticker: int) {.closure.} =
  2803. echo "a1: A"
  2804. yield
  2805. echo "a1: B"
  2806. yield
  2807. echo "a1: C"
  2808. yield
  2809. echo "a1: D"
  2810. iterator a2(ticker: int) {.closure.} =
  2811. echo "a2: A"
  2812. yield
  2813. echo "a2: B"
  2814. yield
  2815. echo "a2: C"
  2816. proc runTasks(t: varargs[Task]) =
  2817. var ticker = 0
  2818. while true:
  2819. let x = t[ticker mod t.len]
  2820. if finished(x): break
  2821. x(ticker)
  2822. inc ticker
  2823. runTasks(a1, a2)
  2824. The builtin ``system.finished`` can be used to determine if an iterator has
  2825. finished its operation; no exception is raised on an attempt to invoke an
  2826. iterator that has already finished its work.
  2827. Note that ``system.finished`` is error prone to use because it only returns
  2828. ``true`` one iteration after the iterator has finished:
  2829. .. code-block:: nim
  2830. iterator mycount(a, b: int): int {.closure.} =
  2831. var x = a
  2832. while x <= b:
  2833. yield x
  2834. inc x
  2835. var c = mycount # instantiate the iterator
  2836. while not finished(c):
  2837. echo c(1, 3)
  2838. # Produces
  2839. 1
  2840. 2
  2841. 3
  2842. 0
  2843. Instead this code has to be used:
  2844. .. code-block:: nim
  2845. var c = mycount # instantiate the iterator
  2846. while true:
  2847. let value = c(1, 3)
  2848. if finished(c): break # and discard 'value'!
  2849. echo value
  2850. It helps to think that the iterator actually returns a
  2851. pair ``(value, done)`` and ``finished`` is used to access the hidden ``done``
  2852. field.
  2853. Closure iterators are *resumable functions* and so one has to provide the
  2854. arguments to every call. To get around this limitation one can capture
  2855. parameters of an outer factory proc:
  2856. .. code-block:: nim
  2857. proc mycount(a, b: int): iterator (): int =
  2858. result = iterator (): int =
  2859. var x = a
  2860. while x <= b:
  2861. yield x
  2862. inc x
  2863. let foo = mycount(1, 4)
  2864. for f in foo():
  2865. echo f
  2866. Converters
  2867. ==========
  2868. A converter is like an ordinary proc except that it enhances
  2869. the "implicitly convertible" type relation (see `Convertible relation`_):
  2870. .. code-block:: nim
  2871. # bad style ahead: Nim is not C.
  2872. converter toBool(x: int): bool = x != 0
  2873. if 4:
  2874. echo "compiles"
  2875. A converter can also be explicitly invoked for improved readability. Note that
  2876. implicit converter chaining is not supported: If there is a converter from
  2877. type A to type B and from type B to type C the implicit conversion from A to C
  2878. is not provided.
  2879. Type sections
  2880. =============
  2881. Example:
  2882. .. code-block:: nim
  2883. type # example demonstrating mutually recursive types
  2884. Node = ref object # an object managed by the garbage collector (ref)
  2885. le, ri: Node # left and right subtrees
  2886. sym: ref Sym # leaves contain a reference to a Sym
  2887. Sym = object # a symbol
  2888. name: string # the symbol's name
  2889. line: int # the line the symbol was declared in
  2890. code: Node # the symbol's abstract syntax tree
  2891. A type section begins with the ``type`` keyword. It contains multiple
  2892. type definitions. A type definition binds a type to a name. Type definitions
  2893. can be recursive or even mutually recursive. Mutually recursive types are only
  2894. possible within a single ``type`` section. Nominal types like ``objects``
  2895. or ``enums`` can only be defined in a ``type`` section.
  2896. Exception handling
  2897. ==================
  2898. Try statement
  2899. -------------
  2900. Example:
  2901. .. code-block:: nim
  2902. # read the first two lines of a text file that should contain numbers
  2903. # and tries to add them
  2904. var
  2905. f: File
  2906. if open(f, "numbers.txt"):
  2907. try:
  2908. var a = readLine(f)
  2909. var b = readLine(f)
  2910. echo "sum: " & $(parseInt(a) + parseInt(b))
  2911. except OverflowError:
  2912. echo "overflow!"
  2913. except ValueError:
  2914. echo "could not convert string to integer"
  2915. except IOError:
  2916. echo "IO error!"
  2917. except:
  2918. echo "Unknown exception!"
  2919. finally:
  2920. close(f)
  2921. The statements after the ``try`` are executed in sequential order unless
  2922. an exception ``e`` is raised. If the exception type of ``e`` matches any
  2923. listed in an ``except`` clause the corresponding statements are executed.
  2924. The statements following the ``except`` clauses are called
  2925. `exception handlers`:idx:.
  2926. The empty `except`:idx: clause is executed if there is an exception that is
  2927. not listed otherwise. It is similar to an ``else`` clause in ``if`` statements.
  2928. If there is a `finally`:idx: clause, it is always executed after the
  2929. exception handlers.
  2930. The exception is *consumed* in an exception handler. However, an
  2931. exception handler may raise another exception. If the exception is not
  2932. handled, it is propagated through the call stack. This means that often
  2933. the rest of the procedure - that is not within a ``finally`` clause -
  2934. is not executed (if an exception occurs).
  2935. Try expression
  2936. --------------
  2937. Try can also be used as an expression; the type of the ``try`` branch then
  2938. needs to fit the types of ``except`` branches, but the type of the ``finally``
  2939. branch always has to be ``void``:
  2940. .. code-block:: nim
  2941. let x = try: parseInt("133a")
  2942. except: -1
  2943. finally: echo "hi"
  2944. To prevent confusing code there is a parsing limitation; if the ``try``
  2945. follows a ``(`` it has to be written as a one liner:
  2946. .. code-block:: nim
  2947. let x = (try: parseInt("133a") except: -1)
  2948. Except clauses
  2949. --------------
  2950. Within an ``except`` clause it is possible to access the current exception
  2951. using the following syntax:
  2952. .. code-block:: nim
  2953. try:
  2954. # ...
  2955. except IOError as e:
  2956. # Now use "e"
  2957. echo "I/O error: " & e.msg
  2958. Alternatively, it is possible to use ``getCurrentException`` to retrieve the
  2959. exception that has been raised:
  2960. .. code-block:: nim
  2961. try:
  2962. # ...
  2963. except IOError:
  2964. let e = getCurrentException()
  2965. # Now use "e"
  2966. Note that ``getCurrentException`` always returns a ``ref Exception``
  2967. type. If a variable of the proper type is needed (in the example
  2968. above, ``IOError``), one must convert it explicitly:
  2969. .. code-block:: nim
  2970. try:
  2971. # ...
  2972. except IOError:
  2973. let e = (ref IOError)(getCurrentException())
  2974. # "e" is now of the proper type
  2975. However, this is seldom needed. The most common case is to extract an
  2976. error message from ``e``, and for such situations it is enough to use
  2977. ``getCurrentExceptionMsg``:
  2978. .. code-block:: nim
  2979. try:
  2980. # ...
  2981. except:
  2982. echo getCurrentExceptionMsg()
  2983. Defer statement
  2984. ---------------
  2985. Instead of a ``try finally`` statement a ``defer`` statement can be used.
  2986. Any statements following the ``defer`` in the current block will be considered
  2987. to be in an implicit try block:
  2988. .. code-block:: nim
  2989. :test: "nim c $1"
  2990. proc main =
  2991. var f = open("numbers.txt")
  2992. defer: close(f)
  2993. f.write "abc"
  2994. f.write "def"
  2995. Is rewritten to:
  2996. .. code-block:: nim
  2997. :test: "nim c $1"
  2998. proc main =
  2999. var f = open("numbers.txt")
  3000. try:
  3001. f.write "abc"
  3002. f.write "def"
  3003. finally:
  3004. close(f)
  3005. Top level ``defer`` statements are not supported
  3006. since it's unclear what such a statement should refer to.
  3007. Raise statement
  3008. ---------------
  3009. Example:
  3010. .. code-block:: nim
  3011. raise newEOS("operating system failed")
  3012. Apart from built-in operations like array indexing, memory allocation, etc.
  3013. the ``raise`` statement is the only way to raise an exception.
  3014. .. XXX document this better!
  3015. If no exception name is given, the current exception is `re-raised`:idx:. The
  3016. `ReraiseError`:idx: exception is raised if there is no exception to
  3017. re-raise. It follows that the ``raise`` statement *always* raises an
  3018. exception.
  3019. Exception hierarchy
  3020. -------------------
  3021. The exception tree is defined in the `system <system.html>`_ module.
  3022. Every exception inherits from ``system.Exception``. Exceptions that indicate
  3023. programming bugs inherit from ``system.Defect`` (which is a subtype of ``Exception``)
  3024. and are stricly speaking not catchable as they can also be mapped to an operation
  3025. that terminates the whole process. Exceptions that indicate any other runtime
  3026. error that can be caught inherit from ``system.CatchableError``
  3027. (which is a subtype of ``Exception``).
  3028. Imported exceptions
  3029. -------------------
  3030. It is possible to raise/catch imported C++ exceptions. Types imported using
  3031. `importcpp` can be raised or caught. Exceptions are raised by value and
  3032. caught by reference. Example:
  3033. .. code-block:: nim
  3034. type
  3035. std_exception {.importcpp: "std::exception", header: "<exception>".} = object
  3036. proc what(s: std_exception): cstring {.importcpp: "((char *)#.what())".}
  3037. try:
  3038. raise std_exception()
  3039. except std_exception as ex:
  3040. echo ex.what()
  3041. Effect system
  3042. =============
  3043. Exception tracking
  3044. ------------------
  3045. Nim supports exception tracking. The `raises`:idx: pragma can be used
  3046. to explicitly define which exceptions a proc/iterator/method/converter is
  3047. allowed to raise. The compiler verifies this:
  3048. .. code-block:: nim
  3049. :test: "nim c $1"
  3050. proc p(what: bool) {.raises: [IOError, OSError].} =
  3051. if what: raise newException(IOError, "IO")
  3052. else: raise newException(OSError, "OS")
  3053. An empty ``raises`` list (``raises: []``) means that no exception may be raised:
  3054. .. code-block:: nim
  3055. proc p(): bool {.raises: [].} =
  3056. try:
  3057. unsafeCall()
  3058. result = true
  3059. except:
  3060. result = false
  3061. A ``raises`` list can also be attached to a proc type. This affects type
  3062. compatibility:
  3063. .. code-block:: nim
  3064. :test: "nim c $1"
  3065. :status: 1
  3066. type
  3067. Callback = proc (s: string) {.raises: [IOError].}
  3068. var
  3069. c: Callback
  3070. proc p(x: string) =
  3071. raise newException(OSError, "OS")
  3072. c = p # type error
  3073. For a routine ``p`` the compiler uses inference rules to determine the set of
  3074. possibly raised exceptions; the algorithm operates on ``p``'s call graph:
  3075. 1. Every indirect call via some proc type ``T`` is assumed to
  3076. raise ``system.Exception`` (the base type of the exception hierarchy) and
  3077. thus any exception unless ``T`` has an explicit ``raises`` list.
  3078. However if the call is of the form ``f(...)`` where ``f`` is a parameter
  3079. of the currently analysed routine it is ignored. The call is optimistically
  3080. assumed to have no effect. Rule 2 compensates for this case.
  3081. 2. Every expression of some proc type within a call that is not a call
  3082. itself (and not nil) is assumed to be called indirectly somehow and thus
  3083. its raises list is added to ``p``'s raises list.
  3084. 3. Every call to a proc ``q`` which has an unknown body (due to a forward
  3085. declaration or an ``importc`` pragma) is assumed to
  3086. raise ``system.Exception`` unless ``q`` has an explicit ``raises`` list.
  3087. 4. Every call to a method ``m`` is assumed to
  3088. raise ``system.Exception`` unless ``m`` has an explicit ``raises`` list.
  3089. 5. For every other call the analysis can determine an exact ``raises`` list.
  3090. 6. For determining a ``raises`` list, the ``raise`` and ``try`` statements
  3091. of ``p`` are taken into consideration.
  3092. Rules 1-2 ensure the following works:
  3093. .. code-block:: nim
  3094. proc noRaise(x: proc()) {.raises: [].} =
  3095. # unknown call that might raise anything, but valid:
  3096. x()
  3097. proc doRaise() {.raises: [IOError].} =
  3098. raise newException(IOError, "IO")
  3099. proc use() {.raises: [].} =
  3100. # doesn't compile! Can raise IOError!
  3101. noRaise(doRaise)
  3102. So in many cases a callback does not cause the compiler to be overly
  3103. conservative in its effect analysis.
  3104. Tag tracking
  3105. ------------
  3106. The exception tracking is part of Nim's `effect system`:idx:. Raising an
  3107. exception is an *effect*. Other effects can also be defined. A user defined
  3108. effect is a means to *tag* a routine and to perform checks against this tag:
  3109. .. code-block:: nim
  3110. :test: "nim c $1"
  3111. :status: 1
  3112. type IO = object ## input/output effect
  3113. proc readLine(): string {.tags: [IO].} = discard
  3114. proc no_IO_please() {.tags: [].} =
  3115. # the compiler prevents this:
  3116. let x = readLine()
  3117. A tag has to be a type name. A ``tags`` list - like a ``raises`` list - can
  3118. also be attached to a proc type. This affects type compatibility.
  3119. The inference for tag tracking is analogous to the inference for
  3120. exception tracking.
  3121. Effects pragma
  3122. --------------
  3123. The ``effects`` pragma has been designed to assist the programmer with the
  3124. effects analysis. It is a statement that makes the compiler output all inferred
  3125. effects up to the ``effects``'s position:
  3126. .. code-block:: nim
  3127. proc p(what: bool) =
  3128. if what:
  3129. raise newException(IOError, "IO")
  3130. {.effects.}
  3131. else:
  3132. raise newException(OSError, "OS")
  3133. The compiler produces a hint message that ``IOError`` can be raised. ``OSError``
  3134. is not listed as it cannot be raised in the branch the ``effects`` pragma
  3135. appears in.
  3136. Generics
  3137. ========
  3138. Generics are Nim's means to parametrize procs, iterators or types with
  3139. `type parameters`:idx:. Depending on context, the brackets are used either to
  3140. introduce type parameters or to instantiate a generic proc, iterator or type.
  3141. The following example shows a generic binary tree can be modelled:
  3142. .. code-block:: nim
  3143. :test: "nim c $1"
  3144. type
  3145. BinaryTree*[T] = ref object # BinaryTree is a generic type with
  3146. # generic param ``T``
  3147. le, ri: BinaryTree[T] # left and right subtrees; may be nil
  3148. data: T # the data stored in a node
  3149. proc newNode*[T](data: T): BinaryTree[T] =
  3150. # constructor for a node
  3151. result = BinaryTree[T](le: nil, ri: nil, data: data)
  3152. proc add*[T](root: var BinaryTree[T], n: BinaryTree[T]) =
  3153. # insert a node into the tree
  3154. if root == nil:
  3155. root = n
  3156. else:
  3157. var it = root
  3158. while it != nil:
  3159. # compare the data items; uses the generic ``cmp`` proc
  3160. # that works for any type that has a ``==`` and ``<`` operator
  3161. var c = cmp(it.data, n.data)
  3162. if c < 0:
  3163. if it.le == nil:
  3164. it.le = n
  3165. return
  3166. it = it.le
  3167. else:
  3168. if it.ri == nil:
  3169. it.ri = n
  3170. return
  3171. it = it.ri
  3172. proc add*[T](root: var BinaryTree[T], data: T) =
  3173. # convenience proc:
  3174. add(root, newNode(data))
  3175. iterator preorder*[T](root: BinaryTree[T]): T =
  3176. # Preorder traversal of a binary tree.
  3177. # Since recursive iterators are not yet implemented,
  3178. # this uses an explicit stack (which is more efficient anyway):
  3179. var stack: seq[BinaryTree[T]] = @[root]
  3180. while stack.len > 0:
  3181. var n = stack.pop()
  3182. while n != nil:
  3183. yield n.data
  3184. add(stack, n.ri) # push right subtree onto the stack
  3185. n = n.le # and follow the left pointer
  3186. var
  3187. root: BinaryTree[string] # instantiate a BinaryTree with ``string``
  3188. add(root, newNode("hello")) # instantiates ``newNode`` and ``add``
  3189. add(root, "world") # instantiates the second ``add`` proc
  3190. for str in preorder(root):
  3191. stdout.writeLine(str)
  3192. The ``T`` is called a `generic type parameter`:idx: or
  3193. a `type variable`:idx:.
  3194. Is operator
  3195. -----------
  3196. The ``is`` operator is evaluated during semantic analysis to check for type
  3197. equivalence. It is therefore very useful for type specialization within generic
  3198. code:
  3199. .. code-block:: nim
  3200. type
  3201. Table[Key, Value] = object
  3202. keys: seq[Key]
  3203. values: seq[Value]
  3204. when not (Key is string): # empty value for strings used for optimization
  3205. deletedKeys: seq[bool]
  3206. Type Classes
  3207. ------------
  3208. A type class is a special pseudo-type that can be used to match against
  3209. types in the context of overload resolution or the ``is`` operator.
  3210. Nim supports the following built-in type classes:
  3211. ================== ===================================================
  3212. type class matches
  3213. ================== ===================================================
  3214. ``object`` any object type
  3215. ``tuple`` any tuple type
  3216. ``enum`` any enumeration
  3217. ``proc`` any proc type
  3218. ``ref`` any ``ref`` type
  3219. ``ptr`` any ``ptr`` type
  3220. ``var`` any ``var`` type
  3221. ``distinct`` any distinct type
  3222. ``array`` any array type
  3223. ``set`` any set type
  3224. ``seq`` any seq type
  3225. ``auto`` any type
  3226. ``any`` distinct auto (see below)
  3227. ================== ===================================================
  3228. Furthermore, every generic type automatically creates a type class of the same
  3229. name that will match any instantiation of the generic type.
  3230. Type classes can be combined using the standard boolean operators to form
  3231. more complex type classes:
  3232. .. code-block:: nim
  3233. # create a type class that will match all tuple and object types
  3234. type RecordType = tuple or object
  3235. proc printFields(rec: RecordType) =
  3236. for key, value in fieldPairs(rec):
  3237. echo key, " = ", value
  3238. Procedures utilizing type classes in such manner are considered to be
  3239. `implicitly generic`:idx:. They will be instantiated once for each unique
  3240. combination of param types used within the program.
  3241. Nim also allows for type classes and regular types to be specified
  3242. as `type constraints`:idx: of the generic type parameter:
  3243. .. code-block:: nim
  3244. proc onlyIntOrString[T: int|string](x, y: T) = discard
  3245. onlyIntOrString(450, 616) # valid
  3246. onlyIntOrString(5.0, 0.0) # type mismatch
  3247. onlyIntOrString("xy", 50) # invalid as 'T' cannot be both at the same time
  3248. By default, during overload resolution each named type class will bind to
  3249. exactly one concrete type. We call such type classes `bind once`:idx: types.
  3250. Here is an example taken directly from the system module to illustrate this:
  3251. .. code-block:: nim
  3252. proc `==`*(x, y: tuple): bool =
  3253. ## requires `x` and `y` to be of the same tuple type
  3254. ## generic ``==`` operator for tuples that is lifted from the components
  3255. ## of `x` and `y`.
  3256. result = true
  3257. for a, b in fields(x, y):
  3258. if a != b: result = false
  3259. Alternatively, the ``distinct`` type modifier can be applied to the type class
  3260. to allow each param matching the type class to bind to a different type. Such
  3261. type classes are called `bind many`:idx: types.
  3262. Procs written with the implicitly generic style will often need to refer to the
  3263. type parameters of the matched generic type. They can be easily accessed using
  3264. the dot syntax:
  3265. .. code-block:: nim
  3266. type Matrix[T, Rows, Columns] = object
  3267. ...
  3268. proc `[]`(m: Matrix, row, col: int): Matrix.T =
  3269. m.data[col * high(Matrix.Columns) + row]
  3270. Alternatively, the `type` operator can be used over the proc params for similar
  3271. effect when anonymous or distinct type classes are used.
  3272. When a generic type is instantiated with a type class instead of a concrete
  3273. type, this results in another more specific type class:
  3274. .. code-block:: nim
  3275. seq[ref object] # Any sequence storing references to any object type
  3276. type T1 = auto
  3277. proc foo(s: seq[T1], e: T1)
  3278. # seq[T1] is the same as just `seq`, but T1 will be allowed to bind
  3279. # to a single type, while the signature is being matched
  3280. Matrix[Ordinal] # Any Matrix instantiation using integer values
  3281. As seen in the previous example, in such instantiations, it's not necessary to
  3282. supply all type parameters of the generic type, because any missing ones will
  3283. be inferred to have the equivalent of the `any` type class and thus they will
  3284. match anything without discrimination.
  3285. Generic inference restrictions
  3286. ------------------------------
  3287. The types ``var T`` and ``typedesc[T]`` cannot be inferred in a generic
  3288. instantiation. The following is not allowed:
  3289. .. code-block:: nim
  3290. :test: "nim c $1"
  3291. :status: 1
  3292. proc g[T](f: proc(x: T); x: T) =
  3293. f(x)
  3294. proc c(y: int) = echo y
  3295. proc v(y: var int) =
  3296. y += 100
  3297. var i: int
  3298. # allowed: infers 'T' to be of type 'int'
  3299. g(c, 42)
  3300. # not valid: 'T' is not inferred to be of type 'var int'
  3301. g(v, i)
  3302. # also not allowed: explict instantiation via 'var int'
  3303. g[var int](v, i)
  3304. Symbol lookup in generics
  3305. -------------------------
  3306. Open and Closed symbols
  3307. ~~~~~~~~~~~~~~~~~~~~~~~
  3308. The symbol binding rules in generics are slightly subtle: There are "open" and
  3309. "closed" symbols. A "closed" symbol cannot be re-bound in the instantiation
  3310. context, an "open" symbol can. Per default overloaded symbols are open
  3311. and every other symbol is closed.
  3312. Open symbols are looked up in two different contexts: Both the context
  3313. at definition and the context at instantiation are considered:
  3314. .. code-block:: nim
  3315. :test: "nim c $1"
  3316. type
  3317. Index = distinct int
  3318. proc `==` (a, b: Index): bool {.borrow.}
  3319. var a = (0, 0.Index)
  3320. var b = (0, 0.Index)
  3321. echo a == b # works!
  3322. In the example the generic ``==`` for tuples (as defined in the system module)
  3323. uses the ``==`` operators of the tuple's components. However, the ``==`` for
  3324. the ``Index`` type is defined *after* the ``==`` for tuples; yet the example
  3325. compiles as the instantiation takes the currently defined symbols into account
  3326. too.
  3327. Mixin statement
  3328. ---------------
  3329. A symbol can be forced to be open by a `mixin`:idx: declaration:
  3330. .. code-block:: nim
  3331. :test: "nim c $1"
  3332. proc create*[T](): ref T =
  3333. # there is no overloaded 'init' here, so we need to state that it's an
  3334. # open symbol explicitly:
  3335. mixin init
  3336. new result
  3337. init result
  3338. ``mixin`` statements only make sense in templates and generics.
  3339. Bind statement
  3340. --------------
  3341. The ``bind`` statement is the counterpart to the ``mixin`` statement. It
  3342. can be used to explicitly declare identifiers that should be bound early (i.e.
  3343. the identifiers should be looked up in the scope of the template/generic
  3344. definition):
  3345. .. code-block:: nim
  3346. # Module A
  3347. var
  3348. lastId = 0
  3349. template genId*: untyped =
  3350. bind lastId
  3351. inc(lastId)
  3352. lastId
  3353. .. code-block:: nim
  3354. # Module B
  3355. import A
  3356. echo genId()
  3357. But a ``bind`` is rarely useful because symbol binding from the definition
  3358. scope is the default.
  3359. ``bind`` statements only make sense in templates and generics.
  3360. Templates
  3361. =========
  3362. A template is a simple form of a macro: It is a simple substitution
  3363. mechanism that operates on Nim's abstract syntax trees. It is processed in
  3364. the semantic pass of the compiler.
  3365. The syntax to *invoke* a template is the same as calling a procedure.
  3366. Example:
  3367. .. code-block:: nim
  3368. template `!=` (a, b: untyped): untyped =
  3369. # this definition exists in the System module
  3370. not (a == b)
  3371. assert(5 != 6) # the compiler rewrites that to: assert(not (5 == 6))
  3372. The ``!=``, ``>``, ``>=``, ``in``, ``notin``, ``isnot`` operators are in fact
  3373. templates:
  3374. | ``a > b`` is transformed into ``b < a``.
  3375. | ``a in b`` is transformed into ``contains(b, a)``.
  3376. | ``notin`` and ``isnot`` have the obvious meanings.
  3377. The "types" of templates can be the symbols ``untyped``,
  3378. ``typed`` or ``typedesc``. These are "meta types", they can only be used in certain
  3379. contexts. Regular types can be used too; this implies that ``typed`` expressions
  3380. are expected.
  3381. Typed vs untyped parameters
  3382. ---------------------------
  3383. An ``untyped`` parameter means that symbol lookups and type resolution is not
  3384. performed before the expression is passed to the template. This means that for
  3385. example *undeclared* identifiers can be passed to the template:
  3386. .. code-block:: nim
  3387. :test: "nim c $1"
  3388. template declareInt(x: untyped) =
  3389. var x: int
  3390. declareInt(x) # valid
  3391. x = 3
  3392. .. code-block:: nim
  3393. :test: "nim c $1"
  3394. :status: 1
  3395. template declareInt(x: typed) =
  3396. var x: int
  3397. declareInt(x) # invalid, because x has not been declared and so has no type
  3398. A template where every parameter is ``untyped`` is called an `immediate`:idx:
  3399. template. For historical reasons templates can be explicitly annotated with
  3400. an ``immediate`` pragma and then these templates do not take part in
  3401. overloading resolution and the parameters' types are *ignored* by the
  3402. compiler. Explicit immediate templates are now deprecated.
  3403. **Note**: For historical reasons ``stmt`` was an alias for ``typed`` and
  3404. ``expr`` was an alias for ``untyped``, but they are removed.
  3405. Passing a code block to a template
  3406. ----------------------------------
  3407. You can pass a block of statements as the last argument to a template
  3408. following the special ``:`` syntax:
  3409. .. code-block:: nim
  3410. :test: "nim c $1"
  3411. template withFile(f, fn, mode, actions: untyped): untyped =
  3412. var f: File
  3413. if open(f, fn, mode):
  3414. try:
  3415. actions
  3416. finally:
  3417. close(f)
  3418. else:
  3419. quit("cannot open: " & fn)
  3420. withFile(txt, "ttempl3.txt", fmWrite): # special colon
  3421. txt.writeLine("line 1")
  3422. txt.writeLine("line 2")
  3423. In the example, the two ``writeLine`` statements are bound to the ``actions``
  3424. parameter.
  3425. Usually to pass a block of code to a template the parameter that accepts
  3426. the block needs to be of type ``untyped``. Because symbol lookups are then
  3427. delayed until template instantiation time:
  3428. .. code-block:: nim
  3429. :test: "nim c $1"
  3430. :status: 1
  3431. template t(body: typed) =
  3432. block:
  3433. body
  3434. t:
  3435. var i = 1
  3436. echo i
  3437. t:
  3438. var i = 2 # fails with 'attempt to redeclare i'
  3439. echo i
  3440. The above code fails with the mysterious error message that ``i`` has already
  3441. been declared. The reason for this is that the ``var i = ...`` bodies need to
  3442. be type-checked before they are passed to the ``body`` parameter and type
  3443. checking in Nim implies symbol lookups. For the symbol lookups to succeed
  3444. ``i`` needs to be added to the current (i.e. outer) scope. After type checking
  3445. these additions to the symbol table are not rolled back (for better or worse).
  3446. The same code works with ``untyped`` as the passed body is not required to be
  3447. type-checked:
  3448. .. code-block:: nim
  3449. :test: "nim c $1"
  3450. template t(body: untyped) =
  3451. block:
  3452. body
  3453. t:
  3454. var i = 1
  3455. echo i
  3456. t:
  3457. var i = 2 # compiles
  3458. echo i
  3459. Varargs of untyped
  3460. ------------------
  3461. In addition to the ``untyped`` meta-type that prevents type checking there is
  3462. also ``varargs[untyped]`` so that not even the number of parameters is fixed:
  3463. .. code-block:: nim
  3464. :test: "nim c $1"
  3465. template hideIdentifiers(x: varargs[untyped]) = discard
  3466. hideIdentifiers(undeclared1, undeclared2)
  3467. However, since a template cannot iterate over varargs, this feature is
  3468. generally much more useful for macros.
  3469. Symbol binding in templates
  3470. ---------------------------
  3471. A template is a `hygienic`:idx: macro and so opens a new scope. Most symbols are
  3472. bound from the definition scope of the template:
  3473. .. code-block:: nim
  3474. # Module A
  3475. var
  3476. lastId = 0
  3477. template genId*: untyped =
  3478. inc(lastId)
  3479. lastId
  3480. .. code-block:: nim
  3481. # Module B
  3482. import A
  3483. echo genId() # Works as 'lastId' has been bound in 'genId's defining scope
  3484. As in generics symbol binding can be influenced via ``mixin`` or ``bind``
  3485. statements.
  3486. Identifier construction
  3487. -----------------------
  3488. In templates identifiers can be constructed with the backticks notation:
  3489. .. code-block:: nim
  3490. :test: "nim c $1"
  3491. template typedef(name: untyped, typ: typedesc) =
  3492. type
  3493. `T name`* {.inject.} = typ
  3494. `P name`* {.inject.} = ref `T name`
  3495. typedef(myint, int)
  3496. var x: PMyInt
  3497. In the example ``name`` is instantiated with ``myint``, so \`T name\` becomes
  3498. ``Tmyint``.
  3499. Lookup rules for template parameters
  3500. ------------------------------------
  3501. A parameter ``p`` in a template is even substituted in the expression ``x.p``.
  3502. Thus template arguments can be used as field names and a global symbol can be
  3503. shadowed by the same argument name even when fully qualified:
  3504. .. code-block:: nim
  3505. # module 'm'
  3506. type
  3507. Lev = enum
  3508. levA, levB
  3509. var abclev = levB
  3510. template tstLev(abclev: Lev) =
  3511. echo abclev, " ", m.abclev
  3512. tstLev(levA)
  3513. # produces: 'levA levA'
  3514. But the global symbol can properly be captured by a ``bind`` statement:
  3515. .. code-block:: nim
  3516. # module 'm'
  3517. type
  3518. Lev = enum
  3519. levA, levB
  3520. var abclev = levB
  3521. template tstLev(abclev: Lev) =
  3522. bind m.abclev
  3523. echo abclev, " ", m.abclev
  3524. tstLev(levA)
  3525. # produces: 'levA levB'
  3526. Hygiene in templates
  3527. --------------------
  3528. Per default templates are `hygienic`:idx:\: Local identifiers declared in a
  3529. template cannot be accessed in the instantiation context:
  3530. .. code-block:: nim
  3531. :test: "nim c $1"
  3532. template newException*(exceptn: typedesc, message: string): untyped =
  3533. var
  3534. e: ref exceptn # e is implicitly gensym'ed here
  3535. new(e)
  3536. e.msg = message
  3537. e
  3538. # so this works:
  3539. let e = "message"
  3540. raise newException(IoError, e)
  3541. Whether a symbol that is declared in a template is exposed to the instantiation
  3542. scope is controlled by the `inject`:idx: and `gensym`:idx: pragmas: gensym'ed
  3543. symbols are not exposed but inject'ed are.
  3544. The default for symbols of entity ``type``, ``var``, ``let`` and ``const``
  3545. is ``gensym`` and for ``proc``, ``iterator``, ``converter``, ``template``,
  3546. ``macro`` is ``inject``. However, if the name of the entity is passed as a
  3547. template parameter, it is an inject'ed symbol:
  3548. .. code-block:: nim
  3549. template withFile(f, fn, mode: untyped, actions: untyped): untyped =
  3550. block:
  3551. var f: File # since 'f' is a template param, it's injected implicitly
  3552. ...
  3553. withFile(txt, "ttempl3.txt", fmWrite):
  3554. txt.writeLine("line 1")
  3555. txt.writeLine("line 2")
  3556. The ``inject`` and ``gensym`` pragmas are second class annotations; they have
  3557. no semantics outside of a template definition and cannot be abstracted over:
  3558. .. code-block:: nim
  3559. {.pragma myInject: inject.}
  3560. template t() =
  3561. var x {.myInject.}: int # does NOT work
  3562. To get rid of hygiene in templates, one can use the `dirty`:idx: pragma for
  3563. a template. ``inject`` and ``gensym`` have no effect in ``dirty`` templates.
  3564. Limitations of the method call syntax
  3565. -------------------------------------
  3566. The expression ``x`` in ``x.f`` needs to be semantically checked (that means
  3567. symbol lookup and type checking) before it can be decided that it needs to be
  3568. rewritten to ``f(x)``. Therefore the dot syntax has some limitations when it
  3569. is used to invoke templates/macros:
  3570. .. code-block:: nim
  3571. :test: "nim c $1"
  3572. :status: 1
  3573. template declareVar(name: untyped) =
  3574. const name {.inject.} = 45
  3575. # Doesn't compile:
  3576. unknownIdentifier.declareVar
  3577. Another common example is this:
  3578. .. code-block:: nim
  3579. :test: "nim c $1"
  3580. :status: 1
  3581. from sequtils import toSeq
  3582. iterator something: string =
  3583. yield "Hello"
  3584. yield "World"
  3585. var info = something().toSeq
  3586. The problem here is that the compiler already decided that ``something()`` as
  3587. an iterator is not callable in this context before ``toSeq`` gets its
  3588. chance to convert it into a sequence.
  3589. Macros
  3590. ======
  3591. A macro is a special function that is executed at compile time.
  3592. Normally the input for a macro is an abstract syntax
  3593. tree (AST) of the code that is passed to it. The macro can then do
  3594. transformations on it and return the transformed AST. This can be used to
  3595. add custom language features and implement `domain specific languages`:idx:.
  3596. Macro invocation is a case where semantic analyis does **not** entirely proceed
  3597. top to bottom and left to right. Instead, semantic analysis happens at least
  3598. twice:
  3599. * Semantic analysis recognizes and resolves the macro invocation.
  3600. * The compiler executes the macro body (which may invoke other procs).
  3601. * It replaces the AST of the macro invocation with the AST returned by the macro.
  3602. * It repeats semantic analysis of that region of the code.
  3603. * If the AST returned by the macro contains other macro invocations,
  3604. this process iterates.
  3605. While macros enable advanced compile-time code transformations, they
  3606. cannot change Nim's syntax. However, this is no real restriction because
  3607. Nim's syntax is flexible enough anyway.
  3608. Debug Example
  3609. -------------
  3610. The following example implements a powerful ``debug`` command that accepts a
  3611. variable number of arguments:
  3612. .. code-block:: nim
  3613. :test: "nim c $1"
  3614. # to work with Nim syntax trees, we need an API that is defined in the
  3615. # ``macros`` module:
  3616. import macros
  3617. macro debug(args: varargs[untyped]): untyped =
  3618. # `args` is a collection of `NimNode` values that each contain the
  3619. # AST for an argument of the macro. A macro always has to
  3620. # return a `NimNode`. A node of kind `nnkStmtList` is suitable for
  3621. # this use case.
  3622. result = nnkStmtList.newTree()
  3623. # iterate over any argument that is passed to this macro:
  3624. for n in args:
  3625. # add a call to the statement list that writes the expression;
  3626. # `toStrLit` converts an AST to its string representation:
  3627. result.add newCall("write", newIdentNode("stdout"), newLit(n.repr))
  3628. # add a call to the statement list that writes ": "
  3629. result.add newCall("write", newIdentNode("stdout"), newLit(": "))
  3630. # add a call to the statement list that writes the expressions value:
  3631. result.add newCall("writeLine", newIdentNode("stdout"), n)
  3632. var
  3633. a: array[0..10, int]
  3634. x = "some string"
  3635. a[0] = 42
  3636. a[1] = 45
  3637. debug(a[0], a[1], x)
  3638. The macro call expands to:
  3639. .. code-block:: nim
  3640. write(stdout, "a[0]")
  3641. write(stdout, ": ")
  3642. writeLine(stdout, a[0])
  3643. write(stdout, "a[1]")
  3644. write(stdout, ": ")
  3645. writeLine(stdout, a[1])
  3646. write(stdout, "x")
  3647. write(stdout, ": ")
  3648. writeLine(stdout, x)
  3649. Arguments that are passed to a ``varargs`` parameter are wrapped in an array
  3650. constructor expression. This is why ``debug`` iterates over all of ``n``'s
  3651. children.
  3652. BindSym
  3653. -------
  3654. The above ``debug`` macro relies on the fact that ``write``, ``writeLine`` and
  3655. ``stdout`` are declared in the system module and thus visible in the
  3656. instantiating context. There is a way to use bound identifiers
  3657. (aka `symbols`:idx:) instead of using unbound identifiers. The ``bindSym``
  3658. builtin can be used for that:
  3659. .. code-block:: nim
  3660. :test: "nim c $1"
  3661. import macros
  3662. macro debug(n: varargs[typed]): untyped =
  3663. result = newNimNode(nnkStmtList, n)
  3664. for x in n:
  3665. # we can bind symbols in scope via 'bindSym':
  3666. add(result, newCall(bindSym"write", bindSym"stdout", toStrLit(x)))
  3667. add(result, newCall(bindSym"write", bindSym"stdout", newStrLitNode(": ")))
  3668. add(result, newCall(bindSym"writeLine", bindSym"stdout", x))
  3669. var
  3670. a: array[0..10, int]
  3671. x = "some string"
  3672. a[0] = 42
  3673. a[1] = 45
  3674. debug(a[0], a[1], x)
  3675. The macro call expands to:
  3676. .. code-block:: nim
  3677. write(stdout, "a[0]")
  3678. write(stdout, ": ")
  3679. writeLine(stdout, a[0])
  3680. write(stdout, "a[1]")
  3681. write(stdout, ": ")
  3682. writeLine(stdout, a[1])
  3683. write(stdout, "x")
  3684. write(stdout, ": ")
  3685. writeLine(stdout, x)
  3686. However, the symbols ``write``, ``writeLine`` and ``stdout`` are already bound
  3687. and are not looked up again. As the example shows, ``bindSym`` does work with
  3688. overloaded symbols implicitly.
  3689. Case-Of Macro
  3690. -------------
  3691. In Nim it is possible to have a macro with the syntax of a *case-of*
  3692. expression just with the difference that all of branches are passed to
  3693. and processed by the macro implementation. It is then up the macro
  3694. implementation to transform the *of-branches* into a valid Nim
  3695. statement. The following example should show how this feature could be
  3696. used for a lexical analyzer.
  3697. .. code-block:: nim
  3698. import macros
  3699. macro case_token(args: varargs[untyped]): untyped =
  3700. echo args.treeRepr
  3701. # creates a lexical analyzer from regular expressions
  3702. # ... (implementation is an exercise for the reader ;-)
  3703. discard
  3704. case_token: # this colon tells the parser it is a macro statement
  3705. of r"[A-Za-z_]+[A-Za-z_0-9]*":
  3706. return tkIdentifier
  3707. of r"0-9+":
  3708. return tkInteger
  3709. of r"[\+\-\*\?]+":
  3710. return tkOperator
  3711. else:
  3712. return tkUnknown
  3713. **Style note**: For code readability, it is the best idea to use the least
  3714. powerful programming construct that still suffices. So the "check list" is:
  3715. (1) Use an ordinary proc/iterator, if possible.
  3716. (2) Else: Use a generic proc/iterator, if possible.
  3717. (3) Else: Use a template, if possible.
  3718. (4) Else: Use a macro.
  3719. Macros as pragmas
  3720. -----------------
  3721. Whole routines (procs, iterators etc.) can also be passed to a template or
  3722. a macro via the pragma notation:
  3723. .. code-block:: nim
  3724. template m(s: untyped) = discard
  3725. proc p() {.m.} = discard
  3726. This is a simple syntactic transformation into:
  3727. .. code-block:: nim
  3728. template m(s: untyped) = discard
  3729. m:
  3730. proc p() = discard
  3731. For Loop Macro
  3732. --------------
  3733. A macro that takes as its only input parameter an expression of the special
  3734. type ``system.ForLoopStmt`` can rewrite the entirety of a ``for`` loop:
  3735. .. code-block:: nim
  3736. :test: "nim c $1"
  3737. import macros
  3738. {.experimental: "forLoopMacros".}
  3739. macro enumerate(x: ForLoopStmt): untyped =
  3740. expectKind x, nnkForStmt
  3741. # we strip off the first for loop variable and use
  3742. # it as an integer counter:
  3743. result = newStmtList()
  3744. result.add newVarStmt(x[0], newLit(0))
  3745. var body = x[^1]
  3746. if body.kind != nnkStmtList:
  3747. body = newTree(nnkStmtList, body)
  3748. body.add newCall(bindSym"inc", x[0])
  3749. var newFor = newTree(nnkForStmt)
  3750. for i in 1..x.len-3:
  3751. newFor.add x[i]
  3752. # transform enumerate(X) to 'X'
  3753. newFor.add x[^2][1]
  3754. newFor.add body
  3755. result.add newFor
  3756. # now wrap the whole macro in a block to create a new scope
  3757. result = quote do:
  3758. block: `result`
  3759. for a, b in enumerate(items([1, 2, 3])):
  3760. echo a, " ", b
  3761. # without wrapping the macro in a block, we'd need to choose different
  3762. # names for `a` and `b` here to avoid redefinition errors
  3763. for a, b in enumerate([1, 2, 3, 5]):
  3764. echo a, " ", b
  3765. Currently for loop macros must be enabled explicitly
  3766. via ``{.experimental: "forLoopMacros".}``.
  3767. Case statement macros
  3768. ---------------------
  3769. A macro that needs to be called `match`:idx: can be used to rewrite
  3770. ``case`` statements in order to implement `pattern matching`:idx: for
  3771. certain types. The following example implements a simplistic form of
  3772. pattern matching for tuples, leveraging the existing equality operator
  3773. for tuples (as provided in ``system.==``):
  3774. .. code-block:: nim
  3775. :test: "nim c $1"
  3776. {.experimental: "caseStmtMacros".}
  3777. import macros
  3778. macro match(n: tuple): untyped =
  3779. result = newTree(nnkIfStmt)
  3780. let selector = n[0]
  3781. for i in 1 ..< n.len:
  3782. let it = n[i]
  3783. case it.kind
  3784. of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr:
  3785. result.add it
  3786. of nnkOfBranch:
  3787. for j in 0..it.len-2:
  3788. let cond = newCall("==", selector, it[j])
  3789. result.add newTree(nnkElifBranch, cond, it[^1])
  3790. else:
  3791. error "'match' cannot handle this node", it
  3792. echo repr result
  3793. case ("foo", 78)
  3794. of ("foo", 78): echo "yes"
  3795. of ("bar", 88): echo "no"
  3796. else: discard
  3797. Currently case statement macros must be enabled explicitly
  3798. via ``{.experimental: "caseStmtMacros".}``.
  3799. ``match`` macros are subject to overload resolution. First the
  3800. ``case``'s selector expression is used to determine which ``match``
  3801. macro to call. To this macro is then passed the complete ``case``
  3802. statement body and the macro is evaluated.
  3803. In other words, the macro needs to transform the full ``case`` statement
  3804. but only the statement's selector expression is used to determine which
  3805. macro to call.
  3806. Special Types
  3807. =============
  3808. static[T]
  3809. ---------
  3810. As their name suggests, static parameters must be constant expressions:
  3811. .. code-block:: nim
  3812. proc precompiledRegex(pattern: static string): RegEx =
  3813. var res {.global.} = re(pattern)
  3814. return res
  3815. precompiledRegex("/d+") # Replaces the call with a precompiled
  3816. # regex, stored in a global variable
  3817. precompiledRegex(paramStr(1)) # Error, command-line options
  3818. # are not constant expressions
  3819. For the purposes of code generation, all static params are treated as
  3820. generic params - the proc will be compiled separately for each unique
  3821. supplied value (or combination of values).
  3822. Static params can also appear in the signatures of generic types:
  3823. .. code-block:: nim
  3824. type
  3825. Matrix[M,N: static int; T: Number] = array[0..(M*N - 1), T]
  3826. # Note how `Number` is just a type constraint here, while
  3827. # `static int` requires us to supply an int value
  3828. AffineTransform2D[T] = Matrix[3, 3, T]
  3829. AffineTransform3D[T] = Matrix[4, 4, T]
  3830. var m1: AffineTransform3D[float] # OK
  3831. var m2: AffineTransform2D[string] # Error, `string` is not a `Number`
  3832. Please note that ``static T`` is just a syntactic convenience for the underlying
  3833. generic type ``static[T]``. The type param can be omitted to obtain the type
  3834. class of all constant expressions. A more specific type class can be created by
  3835. instantiating ``static`` with another type class.
  3836. You can force an expression to be evaluated at compile time as a constant
  3837. expression by coercing it to a corresponding ``static`` type:
  3838. .. code-block:: nim
  3839. import math
  3840. echo static(fac(5)), " ", static[bool](16.isPowerOfTwo)
  3841. The complier will report any failure to evaluate the expression or a
  3842. possible type mismatch error.
  3843. typedesc[T]
  3844. -----------
  3845. In many contexts, Nim allows you to treat the names of types as regular
  3846. values. These values exists only during the compilation phase, but since
  3847. all values must have a type, ``typedesc`` is considered their special type.
  3848. ``typedesc`` acts like a generic type. For instance, the type of the symbol
  3849. ``int`` is ``typedesc[int]``. Just like with regular generic types, when the
  3850. generic param is ommited, ``typedesc`` denotes the type class of all types.
  3851. As a syntactic convenience, you can also use ``typedesc`` as a modifier.
  3852. Procs featuring ``typedesc`` params are considered implicitly generic.
  3853. They will be instantiated for each unique combination of supplied types
  3854. and within the body of the proc, the name of each param will refer to
  3855. the bound concrete type:
  3856. .. code-block:: nim
  3857. proc new(T: typedesc): ref T =
  3858. echo "allocating ", T.name
  3859. new(result)
  3860. var n = Node.new
  3861. var tree = new(BinaryTree[int])
  3862. When multiple type params are present, they will bind freely to different
  3863. types. To force a bind-once behavior one can use an explicit generic param:
  3864. .. code-block:: nim
  3865. proc acceptOnlyTypePairs[T, U](A, B: typedesc[T]; C, D: typedesc[U])
  3866. Once bound, type params can appear in the rest of the proc signature:
  3867. .. code-block:: nim
  3868. :test: "nim c $1"
  3869. template declareVariableWithType(T: typedesc, value: T) =
  3870. var x: T = value
  3871. declareVariableWithType int, 42
  3872. Overload resolution can be further influenced by constraining the set
  3873. of types that will match the type param. This works in practice to
  3874. attaching attributes to types via templates. The constraint can be a
  3875. concrete type or a type class.
  3876. .. code-block:: nim
  3877. :test: "nim c $1"
  3878. template maxval(T: typedesc[int]): int = high(int)
  3879. template maxval(T: typedesc[float]): float = Inf
  3880. var i = int.maxval
  3881. var f = float.maxval
  3882. when false:
  3883. var s = string.maxval # error, maxval is not implemented for string
  3884. template isNumber(t: typedesc[object]): string = "Don't think so."
  3885. template isNumber(t: typedesc[SomeInteger]): string = "Yes!"
  3886. template isNumber(t: typedesc[SomeFloat]): string = "Maybe, could be NaN."
  3887. echo "is int a number? ", isNumber(int)
  3888. echo "is float a number? ", isNumber(float)
  3889. echo "is RootObj a number? ", isNumber(RootObj)
  3890. Passing ``typedesc`` almost identical, just with the differences that
  3891. the macro is not instantiated generically. The type expression is
  3892. simply passed as a ``NimNode`` to the macro, like everything else.
  3893. .. code-block:: nim
  3894. import macros
  3895. macro forwardType(arg: typedesc): typedesc =
  3896. # ``arg`` is of type ``NimNode``
  3897. let tmp: NimNode = arg
  3898. result = tmp
  3899. var tmp: forwardType(int)
  3900. typeof operator
  3901. ---------------
  3902. **Note**: ``typeof(x)`` can for historical reasons also be written as
  3903. ``type(x)`` but ``type(x)`` is discouraged.
  3904. You can obtain the type of a given expression by constructing a ``typeof``
  3905. value from it (in many other languages this is known as the `typeof`:idx:
  3906. operator):
  3907. .. code-block:: nim
  3908. var x = 0
  3909. var y: typeof(x) # y has type int
  3910. If ``typeof`` is used to determine the result type of a proc/iterator/converter
  3911. call ``c(X)`` (where ``X`` stands for a possibly empty list of arguments), the
  3912. interpretation where ``c`` is an iterator is preferred over the
  3913. other interpretations, but this behavior can be changed by
  3914. passing ``typeOfProc`` as the second argument to ``typeof``:
  3915. .. code-block:: nim
  3916. :test: "nim c $1"
  3917. iterator split(s: string): string = discard
  3918. proc split(s: string): seq[string] = discard
  3919. # since an iterator is the preferred interpretation, `y` has the type ``string``:
  3920. assert typeof("a b c".split) is string
  3921. assert typeof("a b c".split, typeOfProc) is seq[string]
  3922. Modules
  3923. =======
  3924. Nim supports splitting a program into pieces by a module concept.
  3925. Each module needs to be in its own file and has its own `namespace`:idx:.
  3926. Modules enable `information hiding`:idx: and `separate compilation`:idx:.
  3927. A module may gain access to symbols of another module by the `import`:idx:
  3928. statement. `Recursive module dependencies`:idx: are allowed, but slightly
  3929. subtle. Only top-level symbols that are marked with an asterisk (``*``) are
  3930. exported. A valid module name can only be a valid Nim identifier (and thus its
  3931. filename is ``identifier.nim``).
  3932. The algorithm for compiling modules is:
  3933. - compile the whole module as usual, following import statements recursively
  3934. - if there is a cycle only import the already parsed symbols (that are
  3935. exported); if an unknown identifier occurs then abort
  3936. This is best illustrated by an example:
  3937. .. code-block:: nim
  3938. # Module A
  3939. type
  3940. T1* = int # Module A exports the type ``T1``
  3941. import B # the compiler starts parsing B
  3942. proc main() =
  3943. var i = p(3) # works because B has been parsed completely here
  3944. main()
  3945. .. code-block:: nim
  3946. # Module B
  3947. import A # A is not parsed here! Only the already known symbols
  3948. # of A are imported.
  3949. proc p*(x: A.T1): A.T1 =
  3950. # this works because the compiler has already
  3951. # added T1 to A's interface symbol table
  3952. result = x + 1
  3953. Import statement
  3954. ~~~~~~~~~~~~~~~~
  3955. After the ``import`` statement a list of module names can follow or a single
  3956. module name followed by an ``except`` list to prevent some symbols to be
  3957. imported:
  3958. .. code-block:: nim
  3959. :test: "nim c $1"
  3960. :status: 1
  3961. import strutils except `%`, toUpperAscii
  3962. # doesn't work then:
  3963. echo "$1" % "abc".toUpperAscii
  3964. It is not checked that the ``except`` list is really exported from the module.
  3965. This feature allows to compile against an older version of the module that
  3966. does not export these identifiers.
  3967. Include statement
  3968. ~~~~~~~~~~~~~~~~~
  3969. The ``include`` statement does something fundamentally different than
  3970. importing a module: it merely includes the contents of a file. The ``include``
  3971. statement is useful to split up a large module into several files:
  3972. .. code-block:: nim
  3973. include fileA, fileB, fileC
  3974. Module names in imports
  3975. ~~~~~~~~~~~~~~~~~~~~~~~
  3976. A module alias can be introduced via the ``as`` keyword:
  3977. .. code-block:: nim
  3978. import strutils as su, sequtils as qu
  3979. echo su.format("$1", "lalelu")
  3980. The original module name is then not accessible. The notations
  3981. ``path/to/module`` or ``"path/to/module"`` can be used to refer to a module
  3982. in subdirectories:
  3983. .. code-block:: nim
  3984. import lib/pure/os, "lib/pure/times"
  3985. Note that the module name is still ``strutils`` and not ``lib/pure/strutils``
  3986. and so one **cannot** do:
  3987. .. code-block:: nim
  3988. import lib/pure/strutils
  3989. echo lib/pure/strutils.toUpperAscii("abc")
  3990. Likewise the following does not make sense as the name is ``strutils`` already:
  3991. .. code-block:: nim
  3992. import lib/pure/strutils as strutils
  3993. Collective imports from a directory
  3994. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3995. The syntax ``import dir / [moduleA, moduleB]`` can be used to import multiple modules
  3996. from the same directory.
  3997. Path names are syntactically either Nim identifiers or string literals. If the path
  3998. name is not a valid Nim identifier it needs to be a string literal:
  3999. .. code-block:: nim
  4000. import "gfx/3d/somemodule" # in quotes because '3d' is not a valid Nim identifier
  4001. Pseudo import/include paths
  4002. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4003. A directory can also be a so called "pseudo directory". They can be used to
  4004. avoid ambiguity when there are multiple modules with the same path.
  4005. There are two pseudo directories:
  4006. 1. ``std``: The ``std`` pseudo directory is the abstract location of Nim's standard
  4007. library. For example, the syntax ``import std / strutils`` is used to unambiguously
  4008. refer to the standard library's ``strutils`` module.
  4009. 2. ``pkg``: The ``pkg`` pseudo directory is used to unambiguously refer to a Nimble
  4010. package. However, for technical details that lie outside of the scope of this document
  4011. its semantics are: *Use the search path to look for module name but ignore the standard
  4012. library locations*. In other words, it is the opposite of ``std``.
  4013. From import statement
  4014. ~~~~~~~~~~~~~~~~~~~~~
  4015. After the ``from`` statement a module name follows followed by
  4016. an ``import`` to list the symbols one likes to use without explicit
  4017. full qualification:
  4018. .. code-block:: nim
  4019. :test: "nim c $1"
  4020. from strutils import `%`
  4021. echo "$1" % "abc"
  4022. # always possible: full qualification:
  4023. echo strutils.replace("abc", "a", "z")
  4024. It's also possible to use ``from module import nil`` if one wants to import
  4025. the module but wants to enforce fully qualified access to every symbol
  4026. in ``module``.
  4027. Export statement
  4028. ~~~~~~~~~~~~~~~~
  4029. An ``export`` statement can be used for symbol forwarding so that client
  4030. modules don't need to import a module's dependencies:
  4031. .. code-block:: nim
  4032. # module B
  4033. type MyObject* = object
  4034. .. code-block:: nim
  4035. # module A
  4036. import B
  4037. export B.MyObject
  4038. proc `$`*(x: MyObject): string = "my object"
  4039. .. code-block:: nim
  4040. # module C
  4041. import A
  4042. # B.MyObject has been imported implicitly here:
  4043. var x: MyObject
  4044. echo $x
  4045. When the exported symbol is another module, all of its definitions will
  4046. be forwarded. You can use an ``except`` list to exclude some of the symbols.
  4047. Notice that when exporting, you need to specify only the module name:
  4048. .. code-block:: nim
  4049. import foo/bar/baz
  4050. export baz
  4051. Scope rules
  4052. -----------
  4053. Identifiers are valid from the point of their declaration until the end of
  4054. the block in which the declaration occurred. The range where the identifier
  4055. is known is the scope of the identifier. The exact scope of an
  4056. identifier depends on the way it was declared.
  4057. Block scope
  4058. ~~~~~~~~~~~
  4059. The *scope* of a variable declared in the declaration part of a block
  4060. is valid from the point of declaration until the end of the block. If a
  4061. block contains a second block, in which the identifier is redeclared,
  4062. then inside this block, the second declaration will be valid. Upon
  4063. leaving the inner block, the first declaration is valid again. An
  4064. identifier cannot be redefined in the same block, except if valid for
  4065. procedure or iterator overloading purposes.
  4066. Tuple or object scope
  4067. ~~~~~~~~~~~~~~~~~~~~~
  4068. The field identifiers inside a tuple or object definition are valid in the
  4069. following places:
  4070. * To the end of the tuple/object definition.
  4071. * Field designators of a variable of the given tuple/object type.
  4072. * In all descendant types of the object type.
  4073. Module scope
  4074. ~~~~~~~~~~~~
  4075. All identifiers of a module are valid from the point of declaration until
  4076. the end of the module. Identifiers from indirectly dependent modules are *not*
  4077. available. The `system`:idx: module is automatically imported in every module.
  4078. If a module imports an identifier by two different modules, each occurrence of
  4079. the identifier has to be qualified, unless it is an overloaded procedure or
  4080. iterator in which case the overloading resolution takes place:
  4081. .. code-block:: nim
  4082. # Module A
  4083. var x*: string
  4084. .. code-block:: nim
  4085. # Module B
  4086. var x*: int
  4087. .. code-block:: nim
  4088. # Module C
  4089. import A, B
  4090. write(stdout, x) # error: x is ambiguous
  4091. write(stdout, A.x) # no error: qualifier used
  4092. var x = 4
  4093. write(stdout, x) # not ambiguous: uses the module C's x
  4094. Code reordering
  4095. ~~~~~~~~~~~~~~~
  4096. **Note**: Code reordering is experimental and must be enabled via the
  4097. ``{.experimental.}`` pragma.
  4098. The code reordering feature can implicitly rearrange procedure, template, and
  4099. macro definitions along with variable declarations and initializations at the top
  4100. level scope so that, to a large extent, a programmer should not have to worry
  4101. about ordering definitions correctly or be forced to use forward declarations to
  4102. preface definitions inside a module.
  4103. ..
  4104. NOTE: The following was documentation for the code reordering precursor,
  4105. which was {.noForward.}.
  4106. In this mode, procedure definitions may appear out of order and the compiler
  4107. will postpone their semantic analysis and compilation until it actually needs
  4108. to generate code using the definitions. In this regard, this mode is similar
  4109. to the modus operandi of dynamic scripting languages, where the function
  4110. calls are not resolved until the code is executed. Here is the detailed
  4111. algorithm taken by the compiler:
  4112. 1. When a callable symbol is first encountered, the compiler will only note
  4113. the symbol callable name and it will add it to the appropriate overload set
  4114. in the current scope. At this step, it won't try to resolve any of the type
  4115. expressions used in the signature of the symbol (so they can refer to other
  4116. not yet defined symbols).
  4117. 2. When a top level call is encountered (usually at the very end of the
  4118. module), the compiler will try to determine the actual types of all of the
  4119. symbols in the matching overload set. This is a potentially recursive process
  4120. as the signatures of the symbols may include other call expressions, whose
  4121. types will be resolved at this point too.
  4122. 3. Finally, after the best overload is picked, the compiler will start
  4123. compiling the body of the respective symbol. This in turn will lead the
  4124. compiler to discover more call expressions that need to be resolved and steps
  4125. 2 and 3 will be repeated as necessary.
  4126. Please note that if a callable symbol is never used in this scenario, its
  4127. body will never be compiled. This is the default behavior leading to best
  4128. compilation times, but if exhaustive compilation of all definitions is
  4129. required, using ``nim check`` provides this option as well.
  4130. Example:
  4131. .. code-block:: nim
  4132. {.experimental: "codeReordering".}
  4133. proc foo(x: int) =
  4134. bar(x)
  4135. proc bar(x: int) =
  4136. echo(x)
  4137. foo(10)
  4138. Variables can also be reordered as well. Variables that are *initialized* (i.e.
  4139. variables that have their declaration and assignment combined in a single
  4140. statement) can have their entire initialization statement reordered. Be wary of
  4141. what code is executed at the top level:
  4142. .. code-block:: nim
  4143. {.experimental: "codeReordering".}
  4144. proc a() =
  4145. echo(foo)
  4146. var foo = 5
  4147. a() # outputs: "5"
  4148. ..
  4149. TODO: Let's table this for now. This is an *experimental feature* and so the
  4150. specific manner in which ``declared`` operates with it can be decided in
  4151. eventuality, because right now it works a bit weirdly.
  4152. The values of expressions involving ``declared`` are decided *before* the
  4153. code reordering process, and not after. As an example, the output of this
  4154. code is the same as it would be with code reordering disabled.
  4155. .. code-block:: nim
  4156. {.experimental: "codeReordering".}
  4157. proc x() =
  4158. echo(declared(foo))
  4159. var foo = 4
  4160. x() # "false"
  4161. It is important to note that reordering *only* works for symbols at top level
  4162. scope. Therefore, the following will *fail to compile:*
  4163. .. code-block:: nim
  4164. {.experimental: "codeReordering".}
  4165. proc a() =
  4166. b()
  4167. proc b() =
  4168. echo("Hello!")
  4169. a()
  4170. Compiler Messages
  4171. =================
  4172. The Nim compiler emits different kinds of messages: `hint`:idx:,
  4173. `warning`:idx:, and `error`:idx: messages. An *error* message is emitted if
  4174. the compiler encounters any static error.
  4175. Pragmas
  4176. =======
  4177. Pragmas are Nim's method to give the compiler additional information /
  4178. commands without introducing a massive number of new keywords. Pragmas are
  4179. processed on the fly during semantic checking. Pragmas are enclosed in the
  4180. special ``{.`` and ``.}`` curly brackets. Pragmas are also often used as a
  4181. first implementation to play with a language feature before a nicer syntax
  4182. to access the feature becomes available.
  4183. deprecated pragma
  4184. -----------------
  4185. The deprecated pragma is used to mark a symbol as deprecated:
  4186. .. code-block:: nim
  4187. proc p() {.deprecated.}
  4188. var x {.deprecated.}: char
  4189. This pragma can also take in an optional warning string to relay to developers.
  4190. .. code-block:: nim
  4191. proc thing(x: bool) {.deprecated: "use thong instead".}
  4192. noSideEffect pragma
  4193. -------------------
  4194. The ``noSideEffect`` pragma is used to mark a proc/iterator to have no side
  4195. effects. This means that the proc/iterator only changes locations that are
  4196. reachable from its parameters and the return value only depends on the
  4197. arguments. If none of its parameters have the type ``var T``
  4198. or ``ref T`` or ``ptr T`` this means no locations are modified. It is a static
  4199. error to mark a proc/iterator to have no side effect if the compiler cannot
  4200. verify this.
  4201. As a special semantic rule, the built-in `debugEcho <system.html#debugEcho>`_
  4202. pretends to be free of side effects, so that it can be used for debugging
  4203. routines marked as ``noSideEffect``.
  4204. ``func`` is syntactic sugar for a proc with no side effects:
  4205. .. code-block:: nim
  4206. func `+` (x, y: int): int
  4207. To override the compiler's side effect analysis a ``{.noSideEffect.}``
  4208. pragma block can be used:
  4209. .. code-block:: nim
  4210. func f() =
  4211. {.noSideEffect.}:
  4212. echo "test"
  4213. compileTime pragma
  4214. ------------------
  4215. The ``compileTime`` pragma is used to mark a proc or variable to be used only
  4216. during compile-time execution. No code will be generated for it. Compile-time
  4217. procs are useful as helpers for macros. Since version 0.12.0 of the language, a
  4218. proc that uses ``system.NimNode`` within its parameter types is implicitly
  4219. declared ``compileTime``:
  4220. .. code-block:: nim
  4221. proc astHelper(n: NimNode): NimNode =
  4222. result = n
  4223. Is the same as:
  4224. .. code-block:: nim
  4225. proc astHelper(n: NimNode): NimNode {.compileTime.} =
  4226. result = n
  4227. noReturn pragma
  4228. ---------------
  4229. The ``noreturn`` pragma is used to mark a proc that never returns.
  4230. acyclic pragma
  4231. --------------
  4232. The ``acyclic`` pragma applies to type declarations. It is deprecated and
  4233. ignored.
  4234. final pragma
  4235. ------------
  4236. The ``final`` pragma can be used for an object type to specify that it
  4237. cannot be inherited from. Note that inheritance is only available for
  4238. objects that inherit from an existing object (via the ``object of SuperType``
  4239. syntax) or that have been marked as ``inheritable``.
  4240. shallow pragma
  4241. --------------
  4242. The ``shallow`` pragma affects the semantics of a type: The compiler is
  4243. allowed to make a shallow copy. This can cause serious semantic issues and
  4244. break memory safety! However, it can speed up assignments considerably,
  4245. because the semantics of Nim require deep copying of sequences and strings.
  4246. This can be expensive, especially if sequences are used to build a tree
  4247. structure:
  4248. .. code-block:: nim
  4249. type
  4250. NodeKind = enum nkLeaf, nkInner
  4251. Node {.shallow.} = object
  4252. case kind: NodeKind
  4253. of nkLeaf:
  4254. strVal: string
  4255. of nkInner:
  4256. children: seq[Node]
  4257. pure pragma
  4258. -----------
  4259. An object type can be marked with the ``pure`` pragma so that its type field
  4260. which is used for runtime type identification is omitted. This used to be
  4261. necessary for binary compatibility with other compiled languages.
  4262. An enum type can be marked as ``pure``. Then access of its fields always
  4263. requires full qualification.
  4264. asmNoStackFrame pragma
  4265. ----------------------
  4266. A proc can be marked with the ``asmNoStackFrame`` pragma to tell the compiler
  4267. it should not generate a stack frame for the proc. There are also no exit
  4268. statements like ``return result;`` generated and the generated C function is
  4269. declared as ``__declspec(naked)`` or ``__attribute__((naked))`` (depending on
  4270. the used C compiler).
  4271. **Note**: This pragma should only be used by procs which consist solely of
  4272. assembler statements.
  4273. error pragma
  4274. ------------
  4275. The ``error`` pragma is used to make the compiler output an error message
  4276. with the given content. Compilation does not necessarily abort after an error
  4277. though.
  4278. The ``error`` pragma can also be used to
  4279. annotate a symbol (like an iterator or proc). The *usage* of the symbol then
  4280. triggers a static error. This is especially useful to rule out that some
  4281. operation is valid due to overloading and type conversions:
  4282. .. code-block:: nim
  4283. ## check that underlying int values are compared and not the pointers:
  4284. proc `==`(x, y: ptr int): bool {.error.}
  4285. fatal pragma
  4286. ------------
  4287. The ``fatal`` pragma is used to make the compiler output an error message
  4288. with the given content. In contrast to the ``error`` pragma, compilation
  4289. is guaranteed to be aborted by this pragma. Example:
  4290. .. code-block:: nim
  4291. when not defined(objc):
  4292. {.fatal: "Compile this program with the objc command!".}
  4293. warning pragma
  4294. --------------
  4295. The ``warning`` pragma is used to make the compiler output a warning message
  4296. with the given content. Compilation continues after the warning.
  4297. hint pragma
  4298. -----------
  4299. The ``hint`` pragma is used to make the compiler output a hint message with
  4300. the given content. Compilation continues after the hint.
  4301. line pragma
  4302. -----------
  4303. The ``line`` pragma can be used to affect line information of the annotated
  4304. statement as seen in stack backtraces:
  4305. .. code-block:: nim
  4306. template myassert*(cond: untyped, msg = "") =
  4307. if not cond:
  4308. # change run-time line information of the 'raise' statement:
  4309. {.line: instantiationInfo().}:
  4310. raise newException(EAssertionFailed, msg)
  4311. If the ``line`` pragma is used with a parameter, the parameter needs be a
  4312. ``tuple[filename: string, line: int]``. If it is used without a parameter,
  4313. ``system.InstantiationInfo()`` is used.
  4314. linearScanEnd pragma
  4315. --------------------
  4316. The ``linearScanEnd`` pragma can be used to tell the compiler how to
  4317. compile a Nim `case`:idx: statement. Syntactically it has to be used as a
  4318. statement:
  4319. .. code-block:: nim
  4320. case myInt
  4321. of 0:
  4322. echo "most common case"
  4323. of 1:
  4324. {.linearScanEnd.}
  4325. echo "second most common case"
  4326. of 2: echo "unlikely: use branch table"
  4327. else: echo "unlikely too: use branch table for ", myInt
  4328. In the example, the case branches ``0`` and ``1`` are much more common than
  4329. the other cases. Therefore the generated assembler code should test for these
  4330. values first, so that the CPU's branch predictor has a good chance to succeed
  4331. (avoiding an expensive CPU pipeline stall). The other cases might be put into a
  4332. jump table for O(1) overhead, but at the cost of a (very likely) pipeline
  4333. stall.
  4334. The ``linearScanEnd`` pragma should be put into the last branch that should be
  4335. tested against via linear scanning. If put into the last branch of the
  4336. whole ``case`` statement, the whole ``case`` statement uses linear scanning.
  4337. computedGoto pragma
  4338. -------------------
  4339. The ``computedGoto`` pragma can be used to tell the compiler how to
  4340. compile a Nim `case`:idx: in a ``while true`` statement.
  4341. Syntactically it has to be used as a statement inside the loop:
  4342. .. code-block:: nim
  4343. type
  4344. MyEnum = enum
  4345. enumA, enumB, enumC, enumD, enumE
  4346. proc vm() =
  4347. var instructions: array[0..100, MyEnum]
  4348. instructions[2] = enumC
  4349. instructions[3] = enumD
  4350. instructions[4] = enumA
  4351. instructions[5] = enumD
  4352. instructions[6] = enumC
  4353. instructions[7] = enumA
  4354. instructions[8] = enumB
  4355. instructions[12] = enumE
  4356. var pc = 0
  4357. while true:
  4358. {.computedGoto.}
  4359. let instr = instructions[pc]
  4360. case instr
  4361. of enumA:
  4362. echo "yeah A"
  4363. of enumC, enumD:
  4364. echo "yeah CD"
  4365. of enumB:
  4366. echo "yeah B"
  4367. of enumE:
  4368. break
  4369. inc(pc)
  4370. vm()
  4371. As the example shows ``computedGoto`` is mostly useful for interpreters. If
  4372. the underlying backend (C compiler) does not support the computed goto
  4373. extension the pragma is simply ignored.
  4374. unroll pragma
  4375. -------------
  4376. The ``unroll`` pragma can be used to tell the compiler that it should unroll
  4377. a `for`:idx: or `while`:idx: loop for execution efficiency:
  4378. .. code-block:: nim
  4379. proc searchChar(s: string, c: char): int =
  4380. for i in 0 .. s.high:
  4381. {.unroll: 4.}
  4382. if s[i] == c: return i
  4383. result = -1
  4384. In the above example, the search loop is unrolled by a factor 4. The unroll
  4385. factor can be left out too; the compiler then chooses an appropriate unroll
  4386. factor.
  4387. **Note**: Currently the compiler recognizes but ignores this pragma.
  4388. immediate pragma
  4389. ----------------
  4390. The immediate pragma is obsolete. See `Typed vs untyped parameters`_.
  4391. compilation option pragmas
  4392. --------------------------
  4393. The listed pragmas here can be used to override the code generation options
  4394. for a proc/method/converter.
  4395. The implementation currently provides the following possible options (various
  4396. others may be added later).
  4397. =============== =============== ============================================
  4398. pragma allowed values description
  4399. =============== =============== ============================================
  4400. checks on|off Turns the code generation for all runtime
  4401. checks on or off.
  4402. boundChecks on|off Turns the code generation for array bound
  4403. checks on or off.
  4404. overflowChecks on|off Turns the code generation for over- or
  4405. underflow checks on or off.
  4406. nilChecks on|off Turns the code generation for nil pointer
  4407. checks on or off.
  4408. assertions on|off Turns the code generation for assertions
  4409. on or off.
  4410. warnings on|off Turns the warning messages of the compiler
  4411. on or off.
  4412. hints on|off Turns the hint messages of the compiler
  4413. on or off.
  4414. optimization none|speed|size Optimize the code for speed or size, or
  4415. disable optimization.
  4416. patterns on|off Turns the term rewriting templates/macros
  4417. on or off.
  4418. callconv cdecl|... Specifies the default calling convention for
  4419. all procedures (and procedure types) that
  4420. follow.
  4421. =============== =============== ============================================
  4422. Example:
  4423. .. code-block:: nim
  4424. {.checks: off, optimization: speed.}
  4425. # compile without runtime checks and optimize for speed
  4426. push and pop pragmas
  4427. --------------------
  4428. The `push/pop`:idx: pragmas are very similar to the option directive,
  4429. but are used to override the settings temporarily. Example:
  4430. .. code-block:: nim
  4431. {.push checks: off.}
  4432. # compile this section without runtime checks as it is
  4433. # speed critical
  4434. # ... some code ...
  4435. {.pop.} # restore old settings
  4436. register pragma
  4437. ---------------
  4438. The ``register`` pragma is for variables only. It declares the variable as
  4439. ``register``, giving the compiler a hint that the variable should be placed
  4440. in a hardware register for faster access. C compilers usually ignore this
  4441. though and for good reasons: Often they do a better job without it anyway.
  4442. In highly specific cases (a dispatch loop of a bytecode interpreter for
  4443. example) it may provide benefits, though.
  4444. global pragma
  4445. -------------
  4446. The ``global`` pragma can be applied to a variable within a proc to instruct
  4447. the compiler to store it in a global location and initialize it once at program
  4448. startup.
  4449. .. code-block:: nim
  4450. proc isHexNumber(s: string): bool =
  4451. var pattern {.global.} = re"[0-9a-fA-F]+"
  4452. result = s.match(pattern)
  4453. When used within a generic proc, a separate unique global variable will be
  4454. created for each instantiation of the proc. The order of initialization of
  4455. the created global variables within a module is not defined, but all of them
  4456. will be initialized after any top-level variables in their originating module
  4457. and before any variable in a module that imports it.
  4458. pragma pragma
  4459. -------------
  4460. The ``pragma`` pragma can be used to declare user defined pragmas. This is
  4461. useful because Nim's templates and macros do not affect pragmas. User
  4462. defined pragmas are in a different module-wide scope than all other symbols.
  4463. They cannot be imported from a module.
  4464. Example:
  4465. .. code-block:: nim
  4466. when appType == "lib":
  4467. {.pragma: rtl, exportc, dynlib, cdecl.}
  4468. else:
  4469. {.pragma: rtl, importc, dynlib: "client.dll", cdecl.}
  4470. proc p*(a, b: int): int {.rtl.} =
  4471. result = a+b
  4472. In the example a new pragma named ``rtl`` is introduced that either imports
  4473. a symbol from a dynamic library or exports the symbol for dynamic library
  4474. generation.
  4475. Disabling certain messages
  4476. --------------------------
  4477. Nim generates some warnings and hints ("line too long") that may annoy the
  4478. user. A mechanism for disabling certain messages is provided: Each hint
  4479. and warning message contains a symbol in brackets. This is the message's
  4480. identifier that can be used to enable or disable it:
  4481. .. code-block:: Nim
  4482. {.hint[LineTooLong]: off.} # turn off the hint about too long lines
  4483. This is often better than disabling all warnings at once.
  4484. used pragma
  4485. -----------
  4486. Nim produces a warning for symbols that are not exported and not used either.
  4487. The ``used`` pragma can be attached to a symbol to suppress this warning. This
  4488. is particularly useful when the symbol was generated by a macro:
  4489. .. code-block:: nim
  4490. template implementArithOps(T) =
  4491. proc echoAdd(a, b: T) {.used.} =
  4492. echo a + b
  4493. proc echoSub(a, b: T) {.used.} =
  4494. echo a - b
  4495. # no warning produced for the unused 'echoSub'
  4496. implementArithOps(int)
  4497. echoAdd 3, 5
  4498. experimental pragma
  4499. -------------------
  4500. The ``experimental`` pragma enables experimental language features. Depending
  4501. on the concrete feature this means that the feature is either considered
  4502. too unstable for an otherwise stable release or that the future of the feature
  4503. is uncertain (it may be removed any time).
  4504. Example:
  4505. .. code-block:: nim
  4506. {.experimental: "parallel".}
  4507. proc useParallel() =
  4508. parallel:
  4509. for i in 0..4:
  4510. echo "echo in parallel"
  4511. As a top level statement, the experimental pragma enables a feature for the
  4512. rest of the module it's enabled in. This is problematic for macro and generic
  4513. instantiations that cross a module scope. Currently these usages have to be
  4514. put into a ``.push/pop`` environment:
  4515. .. code-block:: nim
  4516. # client.nim
  4517. proc useParallel*[T](unused: T) =
  4518. # use a generic T here to show the problem.
  4519. {.push experimental: "parallel".}
  4520. parallel:
  4521. for i in 0..4:
  4522. echo "echo in parallel"
  4523. {.pop.}
  4524. .. code-block:: nim
  4525. import client
  4526. useParallel(1)
  4527. Implementation Specific Pragmas
  4528. ===============================
  4529. This section describes additional pragmas that the current Nim implementation
  4530. supports but which should not be seen as part of the language specification.
  4531. Bitsize pragma
  4532. --------------
  4533. The ``bitsize`` pragma is for object field members. It declares the field as
  4534. a bitfield in C/C++.
  4535. .. code-block:: Nim
  4536. type
  4537. mybitfield = object
  4538. flag {.bitsize:1.}: cuint
  4539. generates:
  4540. .. code-block:: C
  4541. struct mybitfield {
  4542. unsigned int flag:1;
  4543. };
  4544. Volatile pragma
  4545. ---------------
  4546. The ``volatile`` pragma is for variables only. It declares the variable as
  4547. ``volatile``, whatever that means in C/C++ (its semantics are not well defined
  4548. in C/C++).
  4549. **Note**: This pragma will not exist for the LLVM backend.
  4550. NoDecl pragma
  4551. -------------
  4552. The ``noDecl`` pragma can be applied to almost any symbol (variable, proc,
  4553. type, etc.) and is sometimes useful for interoperability with C:
  4554. It tells Nim that it should not generate a declaration for the symbol in
  4555. the C code. For example:
  4556. .. code-block:: Nim
  4557. var
  4558. EACCES {.importc, noDecl.}: cint # pretend EACCES was a variable, as
  4559. # Nim does not know its value
  4560. However, the ``header`` pragma is often the better alternative.
  4561. **Note**: This will not work for the LLVM backend.
  4562. Header pragma
  4563. -------------
  4564. The ``header`` pragma is very similar to the ``noDecl`` pragma: It can be
  4565. applied to almost any symbol and specifies that it should not be declared
  4566. and instead the generated code should contain an ``#include``:
  4567. .. code-block:: Nim
  4568. type
  4569. PFile {.importc: "FILE*", header: "<stdio.h>".} = distinct pointer
  4570. # import C's FILE* type; Nim will treat it as a new pointer type
  4571. The ``header`` pragma always expects a string constant. The string contant
  4572. contains the header file: As usual for C, a system header file is enclosed
  4573. in angle brackets: ``<>``. If no angle brackets are given, Nim
  4574. encloses the header file in ``""`` in the generated C code.
  4575. **Note**: This will not work for the LLVM backend.
  4576. IncompleteStruct pragma
  4577. -----------------------
  4578. The ``incompleteStruct`` pragma tells the compiler to not use the
  4579. underlying C ``struct`` in a ``sizeof`` expression:
  4580. .. code-block:: Nim
  4581. type
  4582. DIR* {.importc: "DIR", header: "<dirent.h>",
  4583. pure, incompleteStruct.} = object
  4584. Compile pragma
  4585. --------------
  4586. The ``compile`` pragma can be used to compile and link a C/C++ source file
  4587. with the project:
  4588. .. code-block:: Nim
  4589. {.compile: "myfile.cpp".}
  4590. **Note**: Nim computes a SHA1 checksum and only recompiles the file if it
  4591. has changed. You can use the ``-f`` command line option to force recompilation
  4592. of the file.
  4593. Link pragma
  4594. -----------
  4595. The ``link`` pragma can be used to link an additional file with the project:
  4596. .. code-block:: Nim
  4597. {.link: "myfile.o".}
  4598. PassC pragma
  4599. ------------
  4600. The ``passC`` pragma can be used to pass additional parameters to the C
  4601. compiler like you would using the commandline switch ``--passC``:
  4602. .. code-block:: Nim
  4603. {.passC: "-Wall -Werror".}
  4604. Note that you can use ``gorge`` from the `system module <system.html>`_ to
  4605. embed parameters from an external command that will be executed
  4606. during semantic analysis:
  4607. .. code-block:: Nim
  4608. {.passC: gorge("pkg-config --cflags sdl").}
  4609. PassL pragma
  4610. ------------
  4611. The ``passL`` pragma can be used to pass additional parameters to the linker
  4612. like you would using the commandline switch ``--passL``:
  4613. .. code-block:: Nim
  4614. {.passL: "-lSDLmain -lSDL".}
  4615. Note that you can use ``gorge`` from the `system module <system.html>`_ to
  4616. embed parameters from an external command that will be executed
  4617. during semantic analysis:
  4618. .. code-block:: Nim
  4619. {.passL: gorge("pkg-config --libs sdl").}
  4620. Emit pragma
  4621. -----------
  4622. The ``emit`` pragma can be used to directly affect the output of the
  4623. compiler's code generator. So it makes your code unportable to other code
  4624. generators/backends. Its usage is highly discouraged! However, it can be
  4625. extremely useful for interfacing with `C++`:idx: or `Objective C`:idx: code.
  4626. Example:
  4627. .. code-block:: Nim
  4628. {.emit: """
  4629. static int cvariable = 420;
  4630. """.}
  4631. {.push stackTrace:off.}
  4632. proc embedsC() =
  4633. var nimVar = 89
  4634. # access Nim symbols within an emit section outside of string literals:
  4635. {.emit: ["""fprintf(stdout, "%d\n", cvariable + (int)""", nimVar, ");"].}
  4636. {.pop.}
  4637. embedsC()
  4638. ``nimbase.h`` defines ``NIM_EXTERNC`` C macro that can be used for
  4639. ``extern "C"`` code to work with both ``nim c`` and ``nim cpp``, eg:
  4640. .. code-block:: Nim
  4641. proc foobar() {.importc:"$1".}
  4642. {.emit: """
  4643. #include <stdio.h>
  4644. NIM_EXTERNC
  4645. void fun(){}
  4646. """.}
  4647. For backwards compatibility, if the argument to the ``emit`` statement
  4648. is a single string literal, Nim symbols can be referred to via backticks.
  4649. This usage is however deprecated.
  4650. For a toplevel emit statement the section where in the generated C/C++ file
  4651. the code should be emitted can be influenced via the
  4652. prefixes ``/*TYPESECTION*/`` or ``/*VARSECTION*/`` or ``/*INCLUDESECTION*/``:
  4653. .. code-block:: Nim
  4654. {.emit: """/*TYPESECTION*/
  4655. struct Vector3 {
  4656. public:
  4657. Vector3(): x(5) {}
  4658. Vector3(float x_): x(x_) {}
  4659. float x;
  4660. };
  4661. """.}
  4662. type Vector3 {.importcpp: "Vector3", nodecl} = object
  4663. x: cfloat
  4664. proc constructVector3(a: cfloat): Vector3 {.importcpp: "Vector3(@)", nodecl}
  4665. ImportCpp pragma
  4666. ----------------
  4667. **Note**: `c2nim <https://nim-lang.org/docs/c2nim.html>`_ can parse a large subset of C++ and knows
  4668. about the ``importcpp`` pragma pattern language. It is not necessary
  4669. to know all the details described here.
  4670. Similar to the `importc pragma for C
  4671. <#foreign-function-interface-importc-pragma>`_, the
  4672. ``importcpp`` pragma can be used to import `C++`:idx: methods or C++ symbols
  4673. in general. The generated code then uses the C++ method calling
  4674. syntax: ``obj->method(arg)``. In combination with the ``header`` and ``emit``
  4675. pragmas this allows *sloppy* interfacing with libraries written in C++:
  4676. .. code-block:: Nim
  4677. # Horrible example of how to interface with a C++ engine ... ;-)
  4678. {.link: "/usr/lib/libIrrlicht.so".}
  4679. {.emit: """
  4680. using namespace irr;
  4681. using namespace core;
  4682. using namespace scene;
  4683. using namespace video;
  4684. using namespace io;
  4685. using namespace gui;
  4686. """.}
  4687. const
  4688. irr = "<irrlicht/irrlicht.h>"
  4689. type
  4690. IrrlichtDeviceObj {.header: irr,
  4691. importcpp: "IrrlichtDevice".} = object
  4692. IrrlichtDevice = ptr IrrlichtDeviceObj
  4693. proc createDevice(): IrrlichtDevice {.
  4694. header: irr, importcpp: "createDevice(@)".}
  4695. proc run(device: IrrlichtDevice): bool {.
  4696. header: irr, importcpp: "#.run(@)".}
  4697. The compiler needs to be told to generate C++ (command ``cpp``) for
  4698. this to work. The conditional symbol ``cpp`` is defined when the compiler
  4699. emits C++ code.
  4700. Namespaces
  4701. ~~~~~~~~~~
  4702. The *sloppy interfacing* example uses ``.emit`` to produce ``using namespace``
  4703. declarations. It is usually much better to instead refer to the imported name
  4704. via the ``namespace::identifier`` notation:
  4705. .. code-block:: nim
  4706. type
  4707. IrrlichtDeviceObj {.header: irr,
  4708. importcpp: "irr::IrrlichtDevice".} = object
  4709. Importcpp for enums
  4710. ~~~~~~~~~~~~~~~~~~~
  4711. When ``importcpp`` is applied to an enum type the numerical enum values are
  4712. annotated with the C++ enum type, like in this example: ``((TheCppEnum)(3))``.
  4713. (This turned out to be the simplest way to implement it.)
  4714. Importcpp for procs
  4715. ~~~~~~~~~~~~~~~~~~~
  4716. Note that the ``importcpp`` variant for procs uses a somewhat cryptic pattern
  4717. language for maximum flexibility:
  4718. - A hash ``#`` symbol is replaced by the first or next argument.
  4719. - A dot following the hash ``#.`` indicates that the call should use C++'s dot
  4720. or arrow notation.
  4721. - An at symbol ``@`` is replaced by the remaining arguments, separated by
  4722. commas.
  4723. For example:
  4724. .. code-block:: nim
  4725. proc cppMethod(this: CppObj, a, b, c: cint) {.importcpp: "#.CppMethod(@)".}
  4726. var x: ptr CppObj
  4727. cppMethod(x[], 1, 2, 3)
  4728. Produces:
  4729. .. code-block:: C
  4730. x->CppMethod(1, 2, 3)
  4731. As a special rule to keep backwards compatibility with older versions of the
  4732. ``importcpp`` pragma, if there is no special pattern
  4733. character (any of ``# ' @``) at all, C++'s
  4734. dot or arrow notation is assumed, so the above example can also be written as:
  4735. .. code-block:: nim
  4736. proc cppMethod(this: CppObj, a, b, c: cint) {.importcpp: "CppMethod".}
  4737. Note that the pattern language naturally also covers C++'s operator overloading
  4738. capabilities:
  4739. .. code-block:: nim
  4740. proc vectorAddition(a, b: Vec3): Vec3 {.importcpp: "# + #".}
  4741. proc dictLookup(a: Dict, k: Key): Value {.importcpp: "#[#]".}
  4742. - An apostrophe ``'`` followed by an integer ``i`` in the range 0..9
  4743. is replaced by the i'th parameter *type*. The 0th position is the result
  4744. type. This can be used to pass types to C++ function templates. Between
  4745. the ``'`` and the digit an asterisk can be used to get to the base type
  4746. of the type. (So it "takes away a star" from the type; ``T*`` becomes ``T``.)
  4747. Two stars can be used to get to the element type of the element type etc.
  4748. For example:
  4749. .. code-block:: nim
  4750. type Input {.importcpp: "System::Input".} = object
  4751. proc getSubsystem*[T](): ptr T {.importcpp: "SystemManager::getSubsystem<'*0>()", nodecl.}
  4752. let x: ptr Input = getSubsystem[Input]()
  4753. Produces:
  4754. .. code-block:: C
  4755. x = SystemManager::getSubsystem<System::Input>()
  4756. - ``#@`` is a special case to support a ``cnew`` operation. It is required so
  4757. that the call expression is inlined directly, without going through a
  4758. temporary location. This is only required to circumvent a limitation of the
  4759. current code generator.
  4760. For example C++'s ``new`` operator can be "imported" like this:
  4761. .. code-block:: nim
  4762. proc cnew*[T](x: T): ptr T {.importcpp: "(new '*0#@)", nodecl.}
  4763. # constructor of 'Foo':
  4764. proc constructFoo(a, b: cint): Foo {.importcpp: "Foo(@)".}
  4765. let x = cnew constructFoo(3, 4)
  4766. Produces:
  4767. .. code-block:: C
  4768. x = new Foo(3, 4)
  4769. However, depending on the use case ``new Foo`` can also be wrapped like this
  4770. instead:
  4771. .. code-block:: nim
  4772. proc newFoo(a, b: cint): ptr Foo {.importcpp: "new Foo(@)".}
  4773. let x = newFoo(3, 4)
  4774. Wrapping constructors
  4775. ~~~~~~~~~~~~~~~~~~~~~
  4776. Sometimes a C++ class has a private copy constructor and so code like
  4777. ``Class c = Class(1,2);`` must not be generated but instead ``Class c(1,2);``.
  4778. For this purpose the Nim proc that wraps a C++ constructor needs to be
  4779. annotated with the `constructor`:idx: pragma. This pragma also helps to generate
  4780. faster C++ code since construction then doesn't invoke the copy constructor:
  4781. .. code-block:: nim
  4782. # a better constructor of 'Foo':
  4783. proc constructFoo(a, b: cint): Foo {.importcpp: "Foo(@)", constructor.}
  4784. Wrapping destructors
  4785. ~~~~~~~~~~~~~~~~~~~~
  4786. Since Nim generates C++ directly, any destructor is called implicitly by the
  4787. C++ compiler at the scope exits. This means that often one can get away with
  4788. not wrapping the destructor at all! However when it needs to be invoked
  4789. explicitly, it needs to be wrapped. The pattern language provides
  4790. everything that is required:
  4791. .. code-block:: nim
  4792. proc destroyFoo(this: var Foo) {.importcpp: "#.~Foo()".}
  4793. Importcpp for objects
  4794. ~~~~~~~~~~~~~~~~~~~~~
  4795. Generic ``importcpp``'ed objects are mapped to C++ templates. This means that
  4796. you can import C++'s templates rather easily without the need for a pattern
  4797. language for object types:
  4798. .. code-block:: nim
  4799. type
  4800. StdMap {.importcpp: "std::map", header: "<map>".} [K, V] = object
  4801. proc `[]=`[K, V](this: var StdMap[K, V]; key: K; val: V) {.
  4802. importcpp: "#[#] = #", header: "<map>".}
  4803. var x: StdMap[cint, cdouble]
  4804. x[6] = 91.4
  4805. Produces:
  4806. .. code-block:: C
  4807. std::map<int, double> x;
  4808. x[6] = 91.4;
  4809. - If more precise control is needed, the apostrophe ``'`` can be used in the
  4810. supplied pattern to denote the concrete type parameters of the generic type.
  4811. See the usage of the apostrophe operator in proc patterns for more details.
  4812. .. code-block:: nim
  4813. type
  4814. VectorIterator {.importcpp: "std::vector<'0>::iterator".} [T] = object
  4815. var x: VectorIterator[cint]
  4816. Produces:
  4817. .. code-block:: C
  4818. std::vector<int>::iterator x;
  4819. ImportObjC pragma
  4820. -----------------
  4821. Similar to the `importc pragma for C
  4822. <#foreign-function-interface-importc-pragma>`_, the ``importobjc`` pragma can
  4823. be used to import `Objective C`:idx: methods. The generated code then uses the
  4824. Objective C method calling syntax: ``[obj method param1: arg]``.
  4825. In addition with the ``header`` and ``emit`` pragmas this
  4826. allows *sloppy* interfacing with libraries written in Objective C:
  4827. .. code-block:: Nim
  4828. # horrible example of how to interface with GNUStep ...
  4829. {.passL: "-lobjc".}
  4830. {.emit: """
  4831. #include <objc/Object.h>
  4832. @interface Greeter:Object
  4833. {
  4834. }
  4835. - (void)greet:(long)x y:(long)dummy;
  4836. @end
  4837. #include <stdio.h>
  4838. @implementation Greeter
  4839. - (void)greet:(long)x y:(long)dummy
  4840. {
  4841. printf("Hello, World!\n");
  4842. }
  4843. @end
  4844. #include <stdlib.h>
  4845. """.}
  4846. type
  4847. Id {.importc: "id", header: "<objc/Object.h>", final.} = distinct int
  4848. proc newGreeter: Id {.importobjc: "Greeter new", nodecl.}
  4849. proc greet(self: Id, x, y: int) {.importobjc: "greet", nodecl.}
  4850. proc free(self: Id) {.importobjc: "free", nodecl.}
  4851. var g = newGreeter()
  4852. g.greet(12, 34)
  4853. g.free()
  4854. The compiler needs to be told to generate Objective C (command ``objc``) for
  4855. this to work. The conditional symbol ``objc`` is defined when the compiler
  4856. emits Objective C code.
  4857. CodegenDecl pragma
  4858. ------------------
  4859. The ``codegenDecl`` pragma can be used to directly influence Nim's code
  4860. generator. It receives a format string that determines how the variable
  4861. or proc is declared in the generated code.
  4862. For variables $1 in the format string represents the type of the variable
  4863. and $2 is the name of the variable.
  4864. The following Nim code:
  4865. .. code-block:: nim
  4866. var
  4867. a {.codegenDecl: "$# progmem $#".}: int
  4868. will generate this C code:
  4869. .. code-block:: c
  4870. int progmem a
  4871. For procedures $1 is the return type of the procedure, $2 is the name of
  4872. the procedure and $3 is the parameter list.
  4873. The following nim code:
  4874. .. code-block:: nim
  4875. proc myinterrupt() {.codegenDecl: "__interrupt $# $#$#".} =
  4876. echo "realistic interrupt handler"
  4877. will generate this code:
  4878. .. code-block:: c
  4879. __interrupt void myinterrupt()
  4880. InjectStmt pragma
  4881. -----------------
  4882. The ``injectStmt`` pragma can be used to inject a statement before every
  4883. other statement in the current module. It is only supposed to be used for
  4884. debugging:
  4885. .. code-block:: nim
  4886. {.injectStmt: gcInvariants().}
  4887. # ... complex code here that produces crashes ...
  4888. compile time define pragmas
  4889. ---------------------------
  4890. The pragmas listed here can be used to optionally accept values from
  4891. the -d/--define option at compile time.
  4892. The implementation currently provides the following possible options (various
  4893. others may be added later).
  4894. ================= ============================================
  4895. pragma description
  4896. ================= ============================================
  4897. `intdefine`:idx: Reads in a build-time define as an integer
  4898. `strdefine`:idx: Reads in a build-time define as a string
  4899. `booldefine`:idx: Reads in a build-time define as a bool
  4900. ================= ============================================
  4901. .. code-block:: nim
  4902. const FooBar {.intdefine.}: int = 5
  4903. echo FooBar
  4904. ::
  4905. nim c -d:FooBar=42 foobar.nim
  4906. In the above example, providing the -d flag causes the symbol
  4907. ``FooBar`` to be overwritten at compile time, printing out 42. If the
  4908. ``-d:FooBar=42`` were to be omitted, the default value of 5 would be
  4909. used. To see if a value was provided, `defined(FooBar)` can be used.
  4910. The syntax `-d:flag` is actually just a shortcut for `-d:flag=true`.
  4911. Custom annotations
  4912. ------------------
  4913. It is possible to define custom typed pragmas. Custom pragmas do not effect
  4914. code generation directly, but their presence can be detected by macros.
  4915. Custom pragmas are defined using templates annotated with pragma ``pragma``:
  4916. .. code-block:: nim
  4917. template dbTable(name: string, table_space: string = "") {.pragma.}
  4918. template dbKey(name: string = "", primary_key: bool = false) {.pragma.}
  4919. template dbForeignKey(t: typedesc) {.pragma.}
  4920. template dbIgnore {.pragma.}
  4921. Consider stylized example of possible Object Relation Mapping (ORM) implementation:
  4922. .. code-block:: nim
  4923. const tblspace {.strdefine.} = "dev" # switch for dev, test and prod environments
  4924. type
  4925. User {.dbTable("users", tblspace).} = object
  4926. id {.dbKey(primary_key = true).}: int
  4927. name {.dbKey"full_name".}: string
  4928. is_cached {.dbIgnore.}: bool
  4929. age: int
  4930. UserProfile {.dbTable("profiles", tblspace).} = object
  4931. id {.dbKey(primary_key = true).}: int
  4932. user_id {.dbForeignKey: User.}: int
  4933. read_access: bool
  4934. write_access: bool
  4935. admin_acess: bool
  4936. In this example custom pragmas are used to describe how Nim objects are
  4937. mapped to the schema of the relational database. Custom pragmas can have
  4938. zero or more arguments. In order to pass multiple arguments use one of
  4939. template call syntaxes. All arguments are typed and follow standard
  4940. overload resolution rules for templates. Therefore, it is possible to have
  4941. default values for arguments, pass by name, varargs, etc.
  4942. Custom pragmas can be used in all locations where ordinary pragmas can be
  4943. specified. It is possible to annotate procs, templates, type and variable
  4944. definitions, statements, etc.
  4945. Macros module includes helpers which can be used to simplify custom pragma
  4946. access `hasCustomPragma`, `getCustomPragmaVal`. Please consult macros module
  4947. documentation for details. These macros are no magic, they don't do anything
  4948. you cannot do yourself by walking AST object representation.
  4949. More examples with custom pragmas:
  4950. - Better serialization/deserialization control:
  4951. .. code-block:: nim
  4952. type MyObj = object
  4953. a {.dontSerialize.}: int
  4954. b {.defaultDeserialize: 5.}: int
  4955. c {.serializationKey: "_c".}: string
  4956. - Adopting type for gui inspector in a game engine:
  4957. .. code-block:: nim
  4958. type MyComponent = object
  4959. position {.editable, animatable.}: Vector3
  4960. alpha {.editRange: [0.0..1.0], animatable.}: float32
  4961. Foreign function interface
  4962. ==========================
  4963. Nim's `FFI`:idx: (foreign function interface) is extensive and only the
  4964. parts that scale to other future backends (like the LLVM/JavaScript backends)
  4965. are documented here.
  4966. Importc pragma
  4967. --------------
  4968. The ``importc`` pragma provides a means to import a proc or a variable
  4969. from C. The optional argument is a string containing the C identifier. If
  4970. the argument is missing, the C name is the Nim identifier *exactly as
  4971. spelled*:
  4972. .. code-block::
  4973. proc printf(formatstr: cstring) {.header: "<stdio.h>", importc: "printf", varargs.}
  4974. Note that this pragma is somewhat of a misnomer: Other backends do provide
  4975. the same feature under the same name. Also, if one is interfacing with C++
  4976. the `ImportCpp pragma <manual.html#implementation-specific-pragmas-importcpp-pragma>`_ and
  4977. interfacing with Objective-C the `ImportObjC pragma
  4978. <manual.html#implementation-specific-pragmas-importobjc-pragma>`_ can be used.
  4979. The string literal passed to ``importc`` can be a format string:
  4980. .. code-block:: Nim
  4981. proc p(s: cstring) {.importc: "prefix$1".}
  4982. In the example the external name of ``p`` is set to ``prefixp``. Only ``$1``
  4983. is available and a literal dollar sign must be written as ``$$``.
  4984. Exportc pragma
  4985. --------------
  4986. The ``exportc`` pragma provides a means to export a type, a variable, or a
  4987. procedure to C. Enums and constants can't be exported. The optional argument
  4988. is a string containing the C identifier. If the argument is missing, the C
  4989. name is the Nim identifier *exactly as spelled*:
  4990. .. code-block:: Nim
  4991. proc callme(formatstr: cstring) {.exportc: "callMe", varargs.}
  4992. Note that this pragma is somewhat of a misnomer: Other backends do provide
  4993. the same feature under the same name.
  4994. The string literal passed to ``exportc`` can be a format string:
  4995. .. code-block:: Nim
  4996. proc p(s: string) {.exportc: "prefix$1".} =
  4997. echo s
  4998. In the example the external name of ``p`` is set to ``prefixp``. Only ``$1``
  4999. is available and a literal dollar sign must be written as ``$$``.
  5000. Extern pragma
  5001. -------------
  5002. Like ``exportc`` or ``importc``, the ``extern`` pragma affects name
  5003. mangling. The string literal passed to ``extern`` can be a format string:
  5004. .. code-block:: Nim
  5005. proc p(s: string) {.extern: "prefix$1".} =
  5006. echo s
  5007. In the example the external name of ``p`` is set to ``prefixp``. Only ``$1``
  5008. is available and a literal dollar sign must be written as ``$$``.
  5009. Bycopy pragma
  5010. -------------
  5011. The ``bycopy`` pragma can be applied to an object or tuple type and
  5012. instructs the compiler to pass the type by value to procs:
  5013. .. code-block:: nim
  5014. type
  5015. Vector {.bycopy.} = object
  5016. x, y, z: float
  5017. Byref pragma
  5018. ------------
  5019. The ``byref`` pragma can be applied to an object or tuple type and instructs
  5020. the compiler to pass the type by reference (hidden pointer) to procs.
  5021. Varargs pragma
  5022. --------------
  5023. The ``varargs`` pragma can be applied to procedures only (and procedure
  5024. types). It tells Nim that the proc can take a variable number of parameters
  5025. after the last specified parameter. Nim string values will be converted to C
  5026. strings automatically:
  5027. .. code-block:: Nim
  5028. proc printf(formatstr: cstring) {.nodecl, varargs.}
  5029. printf("hallo %s", "world") # "world" will be passed as C string
  5030. Union pragma
  5031. ------------
  5032. The ``union`` pragma can be applied to any ``object`` type. It means all
  5033. of the object's fields are overlaid in memory. This produces a ``union``
  5034. instead of a ``struct`` in the generated C/C++ code. The object declaration
  5035. then must not use inheritance or any GC'ed memory but this is currently not
  5036. checked.
  5037. **Future directions**: GC'ed memory should be allowed in unions and the GC
  5038. should scan unions conservatively.
  5039. Packed pragma
  5040. -------------
  5041. The ``packed`` pragma can be applied to any ``object`` type. It ensures
  5042. that the fields of an object are packed back-to-back in memory. It is useful
  5043. to store packets or messages from/to network or hardware drivers, and for
  5044. interoperability with C. Combining packed pragma with inheritance is not
  5045. defined, and it should not be used with GC'ed memory (ref's).
  5046. **Future directions**: Using GC'ed memory in packed pragma will result in
  5047. a static error. Usage with inheritance should be defined and documented.
  5048. Dynlib pragma for import
  5049. ------------------------
  5050. With the ``dynlib`` pragma a procedure or a variable can be imported from
  5051. a dynamic library (``.dll`` files for Windows, ``lib*.so`` files for UNIX).
  5052. The non-optional argument has to be the name of the dynamic library:
  5053. .. code-block:: Nim
  5054. proc gtk_image_new(): PGtkWidget
  5055. {.cdecl, dynlib: "libgtk-x11-2.0.so", importc.}
  5056. In general, importing a dynamic library does not require any special linker
  5057. options or linking with import libraries. This also implies that no *devel*
  5058. packages need to be installed.
  5059. The ``dynlib`` import mechanism supports a versioning scheme:
  5060. .. code-block:: nim
  5061. proc Tcl_Eval(interp: pTcl_Interp, script: cstring): int {.cdecl,
  5062. importc, dynlib: "libtcl(|8.5|8.4|8.3).so.(1|0)".}
  5063. At runtime the dynamic library is searched for (in this order)::
  5064. libtcl.so.1
  5065. libtcl.so.0
  5066. libtcl8.5.so.1
  5067. libtcl8.5.so.0
  5068. libtcl8.4.so.1
  5069. libtcl8.4.so.0
  5070. libtcl8.3.so.1
  5071. libtcl8.3.so.0
  5072. The ``dynlib`` pragma supports not only constant strings as argument but also
  5073. string expressions in general:
  5074. .. code-block:: nim
  5075. import os
  5076. proc getDllName: string =
  5077. result = "mylib.dll"
  5078. if existsFile(result): return
  5079. result = "mylib2.dll"
  5080. if existsFile(result): return
  5081. quit("could not load dynamic library")
  5082. proc myImport(s: cstring) {.cdecl, importc, dynlib: getDllName().}
  5083. **Note**: Patterns like ``libtcl(|8.5|8.4).so`` are only supported in constant
  5084. strings, because they are precompiled.
  5085. **Note**: Passing variables to the ``dynlib`` pragma will fail at runtime
  5086. because of order of initialization problems.
  5087. **Note**: A ``dynlib`` import can be overridden with
  5088. the ``--dynlibOverride:name`` command line option. The Compiler User Guide
  5089. contains further information.
  5090. Dynlib pragma for export
  5091. ------------------------
  5092. With the ``dynlib`` pragma a procedure can also be exported to
  5093. a dynamic library. The pragma then has no argument and has to be used in
  5094. conjunction with the ``exportc`` pragma:
  5095. .. code-block:: Nim
  5096. proc exportme(): int {.cdecl, exportc, dynlib.}
  5097. This is only useful if the program is compiled as a dynamic library via the
  5098. ``--app:lib`` command line option. This pragma only has an effect for the code
  5099. generation on the Windows target, so when this pragma is forgotten and the dynamic
  5100. library is only tested on Mac and/or Linux, there won't be an error. On Windows
  5101. this pragma adds ``__declspec(dllexport)`` to the function declaration.
  5102. Threads
  5103. =======
  5104. To enable thread support the ``--threads:on`` command line switch needs to
  5105. be used. The ``system`` module then contains several threading primitives.
  5106. See the `threads <threads.html>`_ and `channels <channels.html>`_ modules
  5107. for the low level thread API. There are also high level parallelism constructs
  5108. available. See `spawn <manual_experimental.html#parallel-amp-spawn>`_ for
  5109. further details.
  5110. Nim's memory model for threads is quite different than that of other common
  5111. programming languages (C, Pascal, Java): Each thread has its own (garbage
  5112. collected) heap and sharing of memory is restricted to global variables. This
  5113. helps to prevent race conditions. GC efficiency is improved quite a lot,
  5114. because the GC never has to stop other threads and see what they reference.
  5115. Thread pragma
  5116. -------------
  5117. A proc that is executed as a new thread of execution should be marked by the
  5118. ``thread`` pragma for reasons of readability. The compiler checks for
  5119. violations of the `no heap sharing restriction`:idx:\: This restriction implies
  5120. that it is invalid to construct a data structure that consists of memory
  5121. allocated from different (thread local) heaps.
  5122. A thread proc is passed to ``createThread`` or ``spawn`` and invoked
  5123. indirectly; so the ``thread`` pragma implies ``procvar``.
  5124. GC safety
  5125. ---------
  5126. We call a proc ``p`` `GC safe`:idx: when it doesn't access any global variable
  5127. that contains GC'ed memory (``string``, ``seq``, ``ref`` or a closure) either
  5128. directly or indirectly through a call to a GC unsafe proc.
  5129. The `gcsafe`:idx: annotation can be used to mark a proc to be gcsafe,
  5130. otherwise this property is inferred by the compiler. Note that ``noSideEffect``
  5131. implies ``gcsafe``. The only way to create a thread is via ``spawn`` or
  5132. ``createThread``. The invoked proc must not use ``var`` parameters nor must
  5133. any of its parameters contain a ``ref`` or ``closure`` type. This enforces
  5134. the *no heap sharing restriction*.
  5135. Routines that are imported from C are always assumed to be ``gcsafe``.
  5136. To disable the GC-safety checking the ``--threadAnalysis:off`` command line
  5137. switch can be used. This is a temporary workaround to ease the porting effort
  5138. from old code to the new threading model.
  5139. To override the compiler's gcsafety analysis a ``{.gcsafe.}`` pragma block can
  5140. be used:
  5141. .. code-block:: nim
  5142. var
  5143. someGlobal: string = "some string here"
  5144. perThread {.threadvar.}: string
  5145. proc setPerThread() =
  5146. {.gcsafe.}:
  5147. deepCopy(perThread, someGlobal)
  5148. Future directions:
  5149. - A shared GC'ed heap might be provided.
  5150. Threadvar pragma
  5151. ----------------
  5152. A variable can be marked with the ``threadvar`` pragma, which makes it a
  5153. `thread-local`:idx: variable; Additionally, this implies all the effects
  5154. of the ``global`` pragma.
  5155. .. code-block:: nim
  5156. var checkpoints* {.threadvar.}: seq[string]
  5157. Due to implementation restrictions thread local variables cannot be
  5158. initialized within the ``var`` section. (Every thread local variable needs to
  5159. be replicated at thread creation.)
  5160. Threads and exceptions
  5161. ----------------------
  5162. The interaction between threads and exceptions is simple: A *handled* exception
  5163. in one thread cannot affect any other thread. However, an *unhandled* exception
  5164. in one thread terminates the whole *process*!