Re: Euphoria vs. C and OOP
- Posted by ChrisBurch2 <crylex at freeuk.co.uk> Jan 05, 2007
- 686 views
CChris wrote: > > Bill Reed wrote: > > > > > > People have been saying that Euphoria should be more "C" like, or > > more "OOP" like. Euphoria has unique features that set it apart from > > C or OOP, and make it flexible, but in different ways. > > > > Instead of trying to make Euphoria fit into a C mold or OOP mold, > > take advantage of the unique features of Euphoria. > > > > sequences are structures, but not like C. They are just as flexible, > > perhaps even more so, since it is easier to add to or change what > > a sequence is. > > > > A sequence can be just a string, "ABC", or a structure: > > }}} <eucode> > > a = { > > "ABC", -- string > > 123, -- integer or atom > > {"XYZ", 456} -- another sequence > > } > > </eucode> {{{ > > Referencing these members can be done with constants, like > > > > }}} <eucode> > > constant NAME = 1, AMT = 2, DESC = 3, > > DNAME = 1, DAMT = 2 > > > > a[NAME] ("ABC") > > > > or > > a[DESC] ( {"XYZ", 456} ) > > a[DESC][DNAME] ("XYZ") > > a[DESC][DAMT] (456) > > </eucode> {{{ > > > > C has Public and Private functions. Euphoria can have them too, > > but in different ways. > > > > See the following include and program to see what I mean. I hope > > this can stimulate some discussion as to using Euphoria in a more > > structured way. > > > > > > ==================================================================== > > == Member banking.e == > > ==================================================================== > > }}} <eucode> > > > > --*----------------------------------------------------------------* > > --* banking.e bank account program * > > --* * > > --* Public * > > --* function BankProgram * > > --* * > > --* Private (or member functions) * > > --* function CreateAcct * > > --* function Withdrawal * > > --* function Deposit * > > --* procedure Balance * > > --* * > > --* example of account: * > > --* * > > --* a = {015505, "John Smith", 12000.15} * > > --* a[1] = acctno * > > --* a[2] = acctname * > > --* a[3] = balance * > > --*----------------------------------------------------------------* > > > > global constant > > acctno = 1, > > acctname = 2, > > balance = 3, > > blank_acct = {0, "", 0} > > > > function CreateAcct() -- Private function returns account > > sequence User_account > > > > User_account = blank_acct > > User_account[acctno] = 111000 -- assign next account # > > User_account[acctname] = prompt_string("Enter name : ") > > > > return User_account > > > > end function > > > > function Withdrawal(sequence account, atom amount) -- Private function > > account[balance] -= amount > > > > return account > > > > end function > > > > function Deposit(sequence account, atom amount) -- Private function > > account[balance] += amount > > > > return account > > > > end function > > > > procedure Balance(sequence account) -- Private procedure > > printf(1, "account : %d %s Balance : %.2f\n", {account[acctno], > > account[acctname], > > account[balance]}) > > > > end procedure > > > > global function BankProgram(sequence parms) > > --*----------------------------------------------------------------* > > --* Public because it is global * > > --*----------------------------------------------------------------* > > > > sequence command, User_account, ret_account > > atom amount > > > > --*----------------------------------------------------------------* > > --* parms[1] = command * > > --* parms[2] = account (optional when creating new account) * > > --* parms[3] = optional amount * > > --* parms[4]... optional and expandable * > > --*----------------------------------------------------------------* > > > > command = parms[1] > > > > if equal(command, "new") then > > ret_account = CreateAcct() > > elsif equal(command, "Balance") then > > User_account = parms[2] > > Balance(User_account) > > ret_account = {} > > elsif equal(command, "Deposit") then > > User_account = parms[2] > > amount = parms[3] > > ret_account = Deposit(User_account, amount) > > elsif equal(command, "Withdrawal") then > > User_account = parms[2] > > amount = parms[3] > > ret_account = Withdrawal(User_account, amount) > > else > > puts(1, "Unknown command\n") > > return {} > > end if > > > > return ret_account > > > > end function > > </eucode> {{{ > > > > ==================================================================== > > == end of Member banking.e == > > ==================================================================== > > > > > > ==================================================================== > > == main program: == > > ==================================================================== > > }}} <eucode> > > > > include get.e > > include graphics.e > > include banking.e > > > > sequence myAccount, null_status > > > > text_color(WHITE) bk_color(BLUE) > > clear_screen() > > > > myAccount = BankProgram({"new"}) > > myAccount = BankProgram({"Deposit", myAccount, 12000.00}) > > myAccount = BankProgram({"Withdrawal", myAccount, 150.00}) > > null_status = BankProgram({"Balance", myAccount}) > > > > </eucode> {{{ > > > > Other functions and features can be added from this basic > > framework. Features such as checking for overdraft status, adding > > extra fees, crediting or debiting accounts, etc. > > This is the sort of hack you are unfortunately saddled with when using the > language. Removing this sort of global namespace pollution is a priority > issue IMHO. > If only because, once you have defined a constant "balance" to number the > relevant field in your sequence, you can no longer have another "structure" > > use the same name, as the field index name must be global. This makes no > sense. > > CChris Hi Try BlitzBasic - it makes perfect sense there! I would argue that this was not so much a hack, as a feature of the language - it does not detract from using the language, it just requires naming global variables a little more carefully. The argument to that would be 'why should we', and I would answer 'Whats the problem?' Euphoria is a simple, yet versatile, language unique from C, yet changing the 'structure' indexing system to make it more 'C' like would surely lead to a convergence with C. Perhaps a new language is needed - 'C' with sequences - could call it Cquence - and and let Euphoria wallow in its own unique simplicity. Chris