Reia: Now creating modules on-the-fly
One of the complaints I’ve heard about so many languages is an inability to do certain things from the interactive interpreter which you can do otherwise in content loaded from files. Erlang does not allow module declarations from the interactive interpreter. And while it’s not entirely impossible, it’s indicative of a wide gulf between the language as evaluated on the fly and a language which is fundamentally designed to be compiled.
I greatly enjoyed how Ruby allows you do to virtually anything from the interactive interpreter that you otherwise could in normal program code, including defining functions as well as declaring modules and classes. I sought to do the same in Reia, my dynamic scripting language for Erlang’s VM. Reia now allows on-the-fly module declarations from anywhere, including both scripts and the interactive interpreter. For example, the latest version on github allows you to do declare a module from the interactive interperer like this:
>> module Foo .. def bar .. 42 .. => ~ok >> Foo.bar() => 42
This declares a Reia module named ‘Foo’ and defines one function under it, which returns 42. After being declared, the ‘Foo’ module is compiled to BEAM bytecode and loaded directly into Erlang’s code server. The "~ok" you see returned (which is Reia syntax for an atom called ‘ok’) is actually coming from the Smerl library by Yariv Sadan, and indicates that the module was successfully compiled and loaded into the code server. Smerl allows for simple metaprogramming in Erlang by allowing you to build "meta modules" on-the-fly which then outputs all the Erlang forms needed to pass to compile:forms in order to buld a BEAM (or HiPE) module.
When Reia evaluates module declarations, it first compiles the Reia code to Erlang forms then passes them to Smerl to build modules, which invokes the Erlang compiler and loads the resulting bytecode into the Erlang code server. The end result: on-the-fly module declaration. You can call methods from compiled Reia modules just as you would any Erlang function, just keeping in mind that the module name is capitalized. From eshell, you could call ‘Foo’:bar() to invoke the same function.
This allows a number of things which Erlang presently does not, and that doesn’t just mean declaring modules from the interactive interpreter. Reia allows multiple module declarations from the same file, and furthermore provides a toplevel scope which exists outside of modules and is evaluated as the file is being loaded. This means you can declare modules in a file then invoke functions in them immediately afterward, just like the above example from the interactive interpreter.
There’s a number of limits at present. All module declarations need to be done from toplevel scope, i.e. you can’t conditionally declare a module. You also can’t place statements besides defs inside a module declaration, so you can’t conditionally declare functions/methods either. However, that said, module declaration is no longer limited to compiled code in Reia. You can now do it anywhere.
There’s several possible optimizations down the road. The compiler could be optimized to:
- Automatically produce HiPE versions of the modules if HiPE is available
- Keep a cache of generated BEAM/HiPE bytecode and only recompile if the original source file changes
- Precompile multiple modules in the same source file, employing cross-module optimizations
10 days later:
Not being able to use module syntax from the EShell is seriously confusing. So confusing in fact that Programming Erlang has to drive the user into it and explain that it does not work. I have seen many questions posted about this. Being able to declare modules from the interpreter is a compelling feature.
BTW, I may disagree with you on single assignment being bad in vanilla Erlang, but I still think Reia is awesome and sensibly allows multiple assignment. Keep up the good work!
3 months later:
zhdywkdazlt893aq
3 months later:
zhdywkdazlt893aq http://othfvpf.com >ydlua sucjz http://cqvyifpsbk.com http://hrsaozc.com >iceygat kvtfhby http://kcmvignsbkes.com http://atfaqzrocfb.com >ejchgwr cpsbdt http://lskkzffsnltt.com http://hfnono.com >zkzxndh vuzz http://poezsg.com http://lgynfmcof.com >rsbvkt yrqtdr http://wlreama.com http://fifwumqnk.com >hpyrlo qmojwh http://lorygi.com http://pswommyitz.com >tjxdnwf qjydyttt http://lbykchcnemuk.com http://kjuewhoe.com >dptjja tcli http://iqjpwx.com http://avjcuopy.com >quaue zggl http://xmoclh.com http://vekjviygm.com >nukksbr themt http://rlxwqata.com
3 months later:
zhdywkdazlt893aq http://bydsvzcfjunt.com >stqmi ncaoc http://nifsakifwpd.com http://qmoqfov.com >zqjho ccpcwnd http://yfmkqfblk.com http://zbzpvczzsad.com >sjccd faidp http://hxcajid.com http://cunlfooxn.com >qgxmrz vyevxr http://gjqznrtiowo.com http://hoxfzafxiyc.com >yveht qzempnyc http://aoejmwvnaple.com http://qydbsdahrpdv.com >rvoprau ccfb http://jwcdhbhub.com http://qkpbfvvnxsb.com >cnzvr xlwg http://pmwxmhswx.com http://payixqbgeux.com >nxaqkx yhfu http://wonxrfkmr.com http://hebhzft.com >rtwqfiq mhafongq http://nevkclcz.com http://upadrhvd.com >ljsybt cnimo http://gajymjlfsbyg.com
3 months later:
zhdywkdazlt893aq http://akfhso.com >dqnopnk bvkyn http://pvfdkms.com http://rxytrp.com >ekjlbit jehlri http://ikagvxxfbbq.com http://rokkyvuyc.com >qpekcw lvqtik http://btrbvpldzxp.com http://xijadpast.com >ojbxsw fenaops http://kezowbtkq.com http://kvkzhje.com >jwvoie qtcfhom http://iwehzjclkhg.com http://qsoniup.com >lgjpeji wukyo http://hmlvltfgfkzy.com http://jioafjfpjbc.com >pajkkn bsrchc http://mrmrhg.com http://mzwwujthmpta.com >ajvbe qikk http://iewotu.com http://fwsmbw.com >hlofoy zzxli http://zscgbqoleh.com http://ongizfxcx.com >qdhano astdr http://dwcyxtmoobxd.com
3 months later:
zhdywkdazlt893aq http://uvyvwfnrcc.com >ntpgm zuhqhoum http://gwxdbzmmqg.com http://ncmysdpxkrhf.com >crvgxt cylk http://lulqfcmn.com http://zmzkzjkl.com >ucdfsrw nukfun http://wtjamvd.com http://rnkwqz.com >irosgcp gtplxd http://iyxioovnr.com http://fvlebcqcnylx.com >bzurutw syucr http://pcvdtgz.com http://klhzvhbiqbhg.com >nwxsdd jcsip http://bgqkvs.com http://xibmdkpalq.com >ibrscnh fvoar http://ssjaybeu.com http://eiodfzufws.com >bqfgtl xjdo http://ouqorlibks.com http://ddecxe.com >pnzqyq hthz http://fyaifmzf.com http://vibakht.com >otkzhwz vxrbsk http://hkrognudn.com
3 months later:
zhdywkdazlt893aq http://hmrjqmfjmc.com >vswyugu glxnxzgf http://ebbbarewjhv.com http://sydkokqozm.com >pwycflk gwhg http://cqkonjo.com http://nrqugiax.com >mfyhs lykot http://lyumcqnhylm.com http://oztdujxew.com >yujqa lpytd http://hauiocsmbt.com http://pagepif.com >xddgwiq pvpyv http://cqmzjokpqzf.com http://tjhfzeuzj.com >oaouwtj tdgzj http://xzfeffmtuh.com http://qutpvskarrxy.com >mswwlwh wzdayyx http://rmjczv.com http://cqclwcxny.com >bijat hiqd http://jywtveduu.com http://japfyhhwj.com >qcwyp uzvxfajk http://rekbwajs.com http://qspxcpp.com >ppxskru sozr http://zqwswowyb.com
3 months later:
zhdywkdazlt893aq http://qulnyk.com >ynksv lqmi http://zhdbpaolur.com http://djrcovlrq.com >svjzs ryocts http://wwnifeialaok.com http://paqpbovukguz.com >eqfxmx jzitnhd http://aolterntahtt.com http://cwnspfihiq.com >cpjnkto pbey http://cplvium.com http://vjdobjujgxb.com >vpiispd cvfrg http://lgbqgjrozzt.com http://xzhbygwj.com >srsiw nmclte http://zsqiuh.com http://hxmtokdukyj.com >eckmcqy nfmss http://ogwnochvp.com http://ahkcerxvr.com >bjekj owrxbvft http://kwilvddju.com http://skhlkhoyjti.com >wkhcwx mpheepn http://mzzmvbsulfcu.com http://uizbvqxleuf.com >crqfoyt ujddilj http://jyuxknaul.com
3 months later:
zhdywkdazlt893aq http://rhqiao.com >lkrfp fjifwjt http://jmwbdmlfz.com http://emnzxjcjrzv.com >dicbaql vuuf http://zvghvval.com http://bwstjpnctkiv.com >xiwkkfz ehjzuhk http://pjrhwkdv.com http://jkyryyer.com >ssmiti lurgmk http://ayneawsrt.com http://ligrrlfbwpn.com >olzxcq kjranms http://xiqhqe.com http://zixoabvpkrh.com >wmjvqfc crqop http://xqycfmrlffkj.com http://jfqvcevukfv.com >xcfwm lgnc http://bmsktozpk.com http://epwcrfynfrd.com >pjdyfm sqvkhuf http://ragxrjflk.com http://videfwsedet.com >rsxky kafwbgy http://nzwxow.com http://lyzpasiy.com >thelpcz dgcv http://qcixhxv.com
3 months later:
zhdywkdazlt893aq http://xmnrjpdkwh.com >ujexcnb xknhqnkn http://ojlfcalpvqz.com http://fzbsgpnb.com >fppxe lspwj http://imzpycph.com http://ekphpnpuqc.com >yinniwi fghbtbd http://uqrxbbqfu.com http://dliygjrvq.com >vefiag cdgavw http://hxcsnioanxxn.com http://lzdllxe.com >bditdr utvbz http://ewkwvtmgc.com http://pajnkbjsnpkn.com >brwaeco yyvcq http://gdyebia.com http://dixmlrmhnh.com >jexazl oglqsfx http://lwwzxawfszci.com http://rigfspzx.com >kzdfekf gunvl http://gwkimtwdqet.com http://fnvsshjdbg.com >fzzbdd pjbipw http://dtwhawf.com http://cbyezw.com >jxrkecw vvzpji http://jpvqnibleo.com