28.01.2015 Views

Hermes: A Tutorial and Reference Manual - Researcher - IBM

Hermes: A Tutorial and Reference Manual - Researcher - IBM

Hermes: A Tutorial and Reference Manual - Researcher - IBM

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

compile-timeistooconstraining.<strong>Hermes</strong>supportsvariants<strong>and</strong>polymorphs Sometimestherequirementthatthetypeofeveryvaluebeknownat 4.3Variants toallowthisrequirementtobeweakened.Howeverwedonotgiveupthe fundamentalprinciplesthatmachine-levelimplementationdetailmustbe 4.Additional<strong>Hermes</strong>Constructs61<br />

forpurposesofthisexample,we'llassumetobejustacharacterstring ustalkaboutLISPobjects: hidden,<strong>and</strong>thatthelanguagemustbesecure. whohasprogrammedinLISP.ALISPobjectiseitheranatom(which printname),apair,ornothing(nil).Herearethe<strong>Hermes</strong>typeswhichlet LispPair:record(Car:LispObject,Cdr:LispObject); LispKind:enumeration('atom','pair','nil'); Weillustratevariantswithanexamplewhichwillbefamiliartoanyone<br />

LispObject:variantofLispKind(<br />

nents:L.PrintNameoftypeCharstring,L.PairoftypeLispPair,<strong>and</strong> varianttype. whichisusedconventionallytorepresentanullvalue.We'llexplainthe languages.Emptyisapredenedtype|anenumerationwithnovalues| SupposeLisavariableoftypeLispObject.ThenLhasthreecompo-<br />

Theenumeration<strong>and</strong>recordtypesarejustlikesimilartypesinother 'pair'->Pair:LispPairffullg, 'nil'->Nil:Emptyfg); 'atom'->PrintName:Charstringffullg,<br />

L.NiloftypeEmpty.Inthisrespect,avariantisjustlikearecord.But whereasaninitializedrecordmayhaveseveralcomponents,onlyonecomponentofavariantcanexistatanyonetime.Whichcomponentitisdependsuponthecaseofthevariant.Thecaseisanenumerationvalue|in<br />

thisexample,either'atom','pair',or'nil'.Eachcomponentisassociatedwithoneofthecasesofthevariant.Sothevalueofaninitialized<br />

whereintheprogramthevariantishidden,<strong>and</strong>whereitisrevealed.When itisrevealed,thecompilertracksitscase.SoifLisaLispObject,the variantisacasevalueplusexactlyoneofthevariantcomponents. isatypestateproperty.Thatmeansthatthecompilerwillkeeptrackof possibletypestatesofLare: thatwedon'tknowthecaseatcompiletime.Revealedmeanswedo.This Aninitializedvariantcanbeeitherhiddenorrevealed.Hiddenmeans<br />

initialized<strong>and</strong>revealedincase'pair' uninitialized initialized<strong>and</strong>hidden initialized<strong>and</strong>revealedincase'atom'

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!