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.

Noticethatincase'pair',L.Pair.Car<strong>and</strong>L.Pair.Cdrarethemselves 3.finit(L),case(L,L.Pair),init(L.Pair),init(L.Pair.Car), 2.finit(L),case(L,L.Nil)g case(L.Pair.Car,L.Pair.Car.PrintName),init(L.Pair.Car.PrintName), init(L.Pair.Cdr),case(L.Pair.Cdr,L.Pair.Cdr.Nil)g 4.Additional<strong>Hermes</strong>Constructs63<br />

theexample,theyarebothinitialized<strong>and</strong>revealed.Thisislegal,since fullyinitializedistheminimumtypestate,<strong>and</strong>initialized<strong>and</strong>revealedis revealed,bothL.Pair.Car<strong>and</strong>L.Pair.Cdrmustbefullyinitialized.In variantsoftypeLispObject.ThedenitionrequiresthatwhenL.Pairis ahighertypestate.Itwouldbeillegaltoperformanoperationtomake<br />

thecomponents,becauseonlyoneofthemreallyexists<strong>and</strong>youdon'tknow hideavariantexplicitlywiththehidestatement.Butusuallyvariantsbecomehiddenbecauseofacoercion.Forexample,ifyouwroteaprogram<br />

Whenthevariantisinitializedbuthidden,youmaynotaccessanyof Ifthecaseattributeisnotpresent,thenthevariantishidden.Youmay L.Pair.CaruninitializedwhileL.Pairisrevealed.<br />

whichoneitis.Forexample,supposethetypestateisfinit(L)g<strong>and</strong>you mentsmentionedabove,thenatthemergerofthepathsthetypestate whichtookthreedierentpaths,eachexecutingoneoftheunitestate-<br />

wouldbefinit(L)g.<br />

thevariantisinthewrongcase,youwillgetaCaseErrorexception.If youwanttoknowthecaseofavariantsoyouknowwhichcomponentitis thecomponentyouhopetoaccess.Ifthecomponentdoesnotexistbecause nentwithouttheappropriatecaseattribute. isimpossiblebecauseyoumaynothaveanyattributeofavariantcomposultinimpossibletypestate".Thetypestatefinit(L),init(L.PrintName)g<br />

trytodotheassignmentL.Printname:="Foo".Thiswillgenerateatypestateerrormessagesaying\addingattributeinit(L.PrintName)wouldre-<br />

CdrofaLispObjecttoarbitrarydepth: correcttoreveal,issuethecaseofoperation.Thisreturnstheenumeration valueofthecase.Here'safragmentofaprocedurewhichswapsCar<strong>and</strong> receiveParmsfrominit; Torevealavariant,usetherevealstatement.Theoper<strong>and</strong>ofrevealis<br />

blockbegin<br />

attributeswhichthedenitionsaiditwassupposedtohave.Youmayraise Afterthereveal,thecomponentoftherevealedvarianthasthetypestate endblock; returnParms; on(CaseError)--it'snotapair SwapCM.L.Pair.Cdr:=Temp; SwapCM.L.Pair.Car:=Swap(SwapCM.L.Pair.Cdr); revealSwapCM.L.Pair; Temp:=Swap(SwapCM.L.Pair.Car);--it'sapair

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

Saved successfully!

Ooh no, something went wrong!