1. pass by ref
- Posted by xecronix Sep 02, 2015
- 2495 views
Is there a way in Euphoria to pass by ref?
#!/usr/bin/env eui enum FNAME sequence dude = {FNAME} public function fname(sequence self, sequence x) self[FNAME] = x return self[FNAME] end function sequence pbv_test = fname(dude, "Barney") printf(1, "pbv_test = %s\n", {pbv_test}) printf(1, "dude[FNAME] = %s\n", {dude[FNAME]}) -- dude must have been passed by value to fname()
2. Re: pass by ref
- Posted by SDPringle Sep 02, 2015
- 2464 views
Is there a way in Euphoria to pass by ref?
Sort of. See the section in the Manual on 'Pseudo Memory'. In fact, it is a good idea you read the whole manual. If you only skim the API sections it is not so long.
Shawn
3. Re: pass by ref
- Posted by ghaberek (admin) Sep 02, 2015
- 2472 views
Is there a way in Euphoria to pass by ref?
As Shawn mentioned you can use Pseudo Memory or, if you're feeling adventurous, you can use the experimental Strucutres feature in 4.1. Either one allows you to place an object into "memory" and then pass around its reference to other functions. If you wrap the entire process in some oop-style code, then you can make the experience quite simple and it doesn't really matter what you're using under the hood.
Pseudo Memory | Structures | |
---|---|---|
Pros | - Already provided in standard library - Code is more Euphoria-like |
- Uses 75% less memory when storing strings - Code to "peek" and "poke" is clean and terse |
Cons | - Uses more memory when storing strings - Code to "peek" and "poke" is verbose |
- Still in beta/experimental - Requires more attention to memory allocation |
Here are a couple examples I threw together:
-Greg
4. Re: pass by ref
- Posted by xecronix Sep 02, 2015
- 2419 views
... if you're feeling adventurous, you can use the experimental Strucutres feature in 4.1.
For sure. I'll download/build it tomorrow and start trying it.
Thanks a lot. This is of course exactly what I was trying to do. Except I was attempting to merge the getters/setters into one function. But the more I think about that idea the more I'm convinced it's not a good idea.
I did change a small bit of your dude Class like code. Check it out! :)
5. Re: pass by ref
- Posted by ghaberek (admin) Sep 03, 2015
- 2373 views
I did change a small bit of your dude Class like code. Check it out! :)
Looks good. I would make a few more changes:
- make the Dude type public so you can use it elsewhere
- allow the Dude type to accept NULL for empty objects
- update your main() routine to use the Dude type
constant NULL = 0 -- Awesome Euphoria Feature! Let's define what a dude looks like. public type Dude (atom ptr) return equal(ptr, NULL) or equal(eumem:ram_space[ptr][__TYPE__], DUDE_ID) end type procedure main() Dude my_dude = dude:new( "John", "Smith", 32 ) display( "[] [] is []", { get_fname( my_dude ), get_lname( my_dude ), get_age( my_dude ) } ) maybe_any_key() end procedure
-Greg
6. Re: pass by ref
- Posted by xecronix Sep 03, 2015
- 2363 views
Looks good. I would make a few more changes:
- make the Dude type public so you can use it elsewhere
- allow the Dude type to accept NULL for empty objects
- update your main() routine to use the Dude type
Awesome, I think we just finished flushing out the tech for the next Additional Documentation topic.
7. Re: pass by ref
- Posted by newphil82 Feb 11, 2016
- 2090 views
I've only had trivial needs - I used a "beginner's" method. Perhaps others do similar things? I make a "big data" sequence. I define "pointer constants" for everything relevant. As a clumsy example, let's imagine ... :
constant boss = 1, someone_else = 2,...telno = 1, address = 2, next_meeting = 3,
my_function(integer name, integer section, ... etc.)
which can read OR modify mybigdata[boss][next_meeting] as required. OK for simple applications?
8. Re: pass by ref
- Posted by _tom (admin) Feb 11, 2016
- 2190 views
I've only had trivial needs - I used a "beginner's" method. Perhaps others do similar things? I make a "big data" sequence. I define "pointer constants" for everything relevant. As a clumsy example, let's imagine ... :
constant boss = 1, someone_else = 2,...telno = 1, address = 2, next_meeting = 3,
my_function(integer name, integer section, ... etc.)
which can read OR modify mybigdata[boss][next_meeting] as required. OK for simple applications?
OpenEuphoria frees you from some ideas you get by reading about programming in "other language." I wrote a tutorial to debunk ideas a beginner may have about PBR and "bigdata." A "clumsy example" now becomes a useful personal program.
_tom
OpenEuphoria vs PBR
Here are some thoughts about PBR (Pass-By-Reference) . If you program in OpenEuphoria you do not need PBR.
Pass-By-Reference
Using "other-language" the idea of PBR seems to offer:
- speed
- laziness
- OOP
speed
Euphoria is fast without speed tricks like PBR. However, if your are programming in "other language" then tricks to speed things up are worthwile. Speed is the bonus you get by programming in Euphoria.
laziness
Using PBR to avoid writing an assignment statement is good for laziness and bad for safety. Unfortunately, PBR hides the exact location where the value of an object is changed in your code. Euphoria programs are easier to read and debug without PBR.
OOP
OOP style programming, while clever, can turn a simple program into a complicated program. By avoiding OOP and PBR you can keep programs simple. OpenEuphoria without PBR is friendlier.
But I Want PBR
OpenEuphoria is flexible if you insist. There is the option of using pseudo memory or structures to write powerful code. OpenEuphoria lets you write programs in your own style.
Conclusion
You don't need PBR to write efficient OpenEuphoria programs.
OpenEuphoria vs Big Data
The OpenEuphoria sequence is a marvel of language design. Go ahead and write reallybig_copy = reallybig without a speed or memory penalty because no actual copy is made. It "looks" like a copy and "behaves" like a copy but Euphoria actually uses internal pointers and optimizations to make your program fast and efficient. Go ahead and write simple--beginner--programs and let OpenEuphoria cope with "big data" effortlessly.
BigData Example
Create an enum to make indexing easier
enum Boss, Someone_else, Telno, Address, Next_meeting
You get a set of constants numbered 1 2 3 4 5 without extra work.
You could create a huge sequence by writing:
atom hugenumber = 1_000_000 sequence bigdata = repeat( {0,0,0,0,0}, hugenumber )
That gives you a database with a million items ready to be updated.
But, why bother when you can add an item just when you need it:
sequence bigdata = {} bigdata = append(bigdata, {"president", "", 41234, "head office", "Mon", "2:30pm" } )
You now have:
{ {"president", "", 41234, "head office", "Mon 2:30pm" }, -- room for lots more $ }
You are free to add, change, and remove items at any time.
Finally, use the enum constants to read and write values:
-- change values bigdata[1][Boss] = "Mr Big" bigdata[1][Next_meeting] = "Tue 9:00am" -- look at the values include std/console.e display( bigdata[1] )
You could create a procdure to output data:
procedure show( sequence data_item ) printf(1, ` Boss : %s Someone_else : %s Telno : %s Address : %s Next_meeting : %s `, data_item ) end procedure
You now get output like:
show( bigdata[1] )
Boss : Mr Big Someone_else : Telno : 41234 Address : head office Next_meeting : Tue 9:00am
A utilty to enter|change data looks like:
function write( sequence boss="", sequence someone_else="", sequence telno="", sequence address="", sequence nextmeeting="" ) return { boss, someone_else, telno, address, nextmeeting } end function
For example you could add a new item:
bigdata = append(bigdata, write("CEO","","","Branch Office", "Fri 3pm" ) ) display( bigdata )
The demo program now looks like:
enum Boss, Someone_else, Telno, Address, Next_meeting sequence bigdata = {} bigdata = append(bigdata, {"president", "", "41234", "head office", "Mon", "2:30pm" } ) -- change values bigdata[1][Boss] = "Mr Big" bigdata[1][Next_meeting] = "Tue 9:00am" -- look at the values include std/console.e display( bigdata[1] ) /* { "Mr Big", "", 41234, "head office", "Tue 9:00am", "2:30pm" } */ procedure show( sequence data_item ) printf(1, ` Boss : %s Someone_else : %s Telno : %s Address : %s Next_meeting : %s `, data_item ) end procedure show( bigdata[1] ) /* Boss : Mr Big Someone_else : Telno : 41234 Address : head office Next_meeting : Tue 9:00am */ --A utilty to enter|change data looks like: function write( sequence boss="", sequence someone_else="", sequence telno="", sequence address="", sequence nextmeeting="" ) return { boss, someone_else, telno, address, nextmeeting } end function --For example you could add a new item: bigdata = append(bigdata, write("CEO","","","Branch Office", "Fri 3pm" ) ) display( bigdata ) /* { "Mr Big", "", "41234", "head office", "Tue 9:00am", "2:30pm" }, { "CEO", "", "", "Branch Office", "Fri 3pm" } } */
Conclusion
You can create a small database program for yourself, with unlimited capacity and flexibility, with very little code.
9. Re: pass by ref
- Posted by dcuny Feb 12, 2016
- 2068 views
Euphoria is fast without speed tricks like PBR. However, if your are programming in "other language" then tricks to speed things up are worthwile. Speed is the bonus you get by programming in Euphoria.
PBR isn't a "trick", it's a technique. That you can do without it: good for you.
Using PBR to avoid writing an assignment statement is good for laziness and bad for safety. Unfortunately, PBR hides the exact location where the value of an object is changed in your code. Euphoria programs are easier to read and debug without PBR.
OE gives type safety on passing parameter into routines, but not out. It also doesn't have good semantics for multiple assignments back out. So I wouldn't say that it's more or less safe.
OOP style programming, while clever, can turn a simple program into a complicated program. By avoiding OOP and PBR you can keep programs simple. OpenEuphoria without PBR is friendlier.
OOP isn't used to be "clever", it's used because it's useful. Encapsulation is a useful technique.
OpenEuphoria is flexible if you insist. There is the option of using pseudo memory or structures to write powerful code.
Pseudo-memory is useful, fast... but in the end, just a simulation PBR. Being able to simulate something isn't the same as having it supported in the language, with the type safety that comes with it.
OpenEuphoria lets you write programs in your own style.
Actually, you've argued just the opposite: If your style is to use PBR of OOP, OE doesn't let you code in your own style.
You don't need PBR to write efficient OpenEuphoria programs.
Efficient isn't the only measure of value in a programming language.
And name calling - such as "speed tricks", "laziness", and "clever" - doesn't make your argument any stronger.
10. Re: pass by ref
- Posted by LarryMiller Feb 12, 2016
- 2020 views
OpenEuphoria has facilities that are usually better than pass by reference. But usually is not always. There are situations where pass by reference would be the easiest and most straightforward way of getting the job done. Using techniques like pseudo memory makes simulation of pass by reference less awkward but not as good as the real thing.
Almost all of the time using goto is a bad idea. But there are some situations where goto is a lesser evil than whatever else you might come up with to avoid it. The same with pass by reference.
11. Re: pass by ref
- Posted by _tom (admin) Feb 12, 2016
- 2012 views
Thanks David for looking over my post.
A major strength of Euphoria is that someone like me (essentially a non-programmer) can get advice from an expert (someone who has written his own programming language).
Ok, so I'm struggling to create a realistic "upbeat" presentation of OpenEuphoria.
- Friendly Flexible Fast (any better catch phrase?)
- how to promote the simplicity of Euphoria (when others just want more features)
- what actually makes Euphoria better?
- why is Euphoria easier to read|write than "other-language"?
- why do I avoid using "other-language"?
Ultimately, I like Euphoria and do not like "other-language."
_tom
12. Re: pass by ref
- Posted by _tom (admin) Feb 12, 2016
- 2030 views
OpenEuphoria has facilities that are usually better than pass by reference. But usually is not always. There are situations where pass by reference would be the easiest and most straightforward way of getting the job done. Using techniques like pseudo memory makes simulation of pass by reference less awkward but not as good as the real thing.
Almost all of the time using goto is a bad idea. But there are some situations where goto is a lesser evil than whatever else you might come up with to avoid it. The same with pass by reference.
I guess a language should be looked on a compromise. You can trim down a language to make it simpler--like Euphoria, or you can bloat it with features so that only one person can understand what you wrote--like Perl.
_tom
13. Re: pass by ref
- Posted by petelomax Feb 12, 2016
- 1995 views
OE gives type safety on passing parameter into routines, but not out. It also doesn't have good semantics for multiple assignments back out.
While Eu does not have any mechanism for heterogeneous assignment on declaration, I don't see anything wrong with
integer a sequence b mytype c myothertype d {a,b,c,d} = somefunc()
Am I missing something?
OOP isn't used to be "clever", it's used because it's useful. Encapsulation is a useful technique.
In my worldview, OOP != Encapsulation. Sure, if you've got OOP, you've got encapsulation, but you can have encapsulation without OOP.
If I see global sequence table, it's like "uh-oh, that thing could be modified anywhere."
If I see sequence table, it's like "ok, that thing can only be modified in this source file."
Is that not encapsulation, at least at the file level? Again, am I missing the point?
Pete
14. Re: pass by ref
- Posted by jimcbrown (admin) Feb 12, 2016
- 2012 views
OE gives type safety on passing parameter into routines, but not out. It also doesn't have good semantics for multiple assignments back out.
While Eu does not have any mechanism for heterogeneous assignment on declaration, I don't see anything wrong with
integer a sequence b mytype c myothertype d {a,b,c,d} = somefunc()
Am I missing something?
Actually, dcuny wrote about this a year ago:
15. Re: pass by ref
- Posted by petelomax Feb 12, 2016
- 2015 views
Actually, dcuny wrote about this a year ago:
I thought that was more about the number of parameters/return values rather than the types.
I confuse myself on these things as well, in Phix puts("str",1) is a compile-time error, whereas (cmiiw) it is a run-time error in OpenEuphoria.
16. Re: pass by ref
- Posted by jimcbrown (admin) Feb 12, 2016
- 2000 views
Actually, dcuny wrote about this a year ago:
I thought that was more about the number of parameters/return values rather than the types.
Well, both aspects were mentioned. http://openeuphoria.org/forum/m/126202.wc and your response http://openeuphoria.org/forum/m/126215.wc
I also think I mentioned that what dcuny wanted was entirely doable with a preprocessor (compile (or rather preprocessor) time errors and syntax sugar). So, if someone else wrote one, and we had an actual proof of concept to work with, then it would be a lot easier to get this sort of change in. (Not that the acceptance is guaranteed - it's kind of confusing to have both multiple assignment and desequencing imvho, and desequencing is much more versatile in that stuff like value() doesn't have to be rewritten to make use of the new functionality.)
In any case, as was said before, we could implement all these checks in the parser, but with respect to desequencing, instead of adding another feature. I think the sticking point is that we don't have a way to specify strict type checking (or checking on # of ret vals) on return values today.