1. Rob: Q: Constant Scope
- Posted by Vincent <darkvincentdude at yahoo.com>
Apr 19, 2005
-
Last edited Apr 20, 2005
Rob,
The ref manual says:
"Constants may not be declared inside a subroutine."
"Constant declarations must be outside of any subroutine. Constants can
be global or local, but not private."
Can you tell me why you made the language support only constant declera-
tions at top-level, and not support it at routine level (private scope)?
I'm just curious on why made it this way. What are the disadvantages?
I'm trying to figure out whether or not I want to add this functionability into
my custom interpreter, and I wanted to know what you thought about
it.
----
Another question... Is there anyway I can use global variables in
Eu2C translated/compiled DLLs, being able to use define_c_var()?
If not, could you consider, adding support for it in the next
release? I tried making an example but it didnt work, maybe I did
somthing wrong?
Thanks,
Vincent
Without walls and fences, there is no need for Windows and Gates.
2. Re: Rob: Q: Constant Scope
- Posted by Robert Craig <rds at RapidEuphoria.com>
Apr 19, 2005
-
Last edited Apr 20, 2005
Vincent wrote:
> The ref manual says:
> "Constants may not be declared inside a subroutine."
> "Constant declarations must be outside of any subroutine. Constants can
> be global or local, but not private."
>
> Can you tell me why you made the language support only constant declera-
> tions at top-level, and not support it at routine level (private scope)?
> I'm just curious on why made it this way. What are the disadvantages?
>
> I'm trying to figure out whether or not I want to add this functionability
> into my
> custom interpreter, and I wanted to know what you thought about
> it.
Here's something I wrote a few years ago, that discusses this issue
and a few other contentious issues:
http://www.listfilter.com/cgi-bin/esearch.exu?fromMonth=2&fromYear=7&toMonth=2&toYear=7&postedBy=rds&keywords=constant+scope+routine
In language design there is rarely a "right answer".
Things are very subjective.
> Another question... Is there anyway I can use global variables in
> Eu2C translated/compiled DLLs, being able to use define_c_var()?
> If not, could you consider, adding support for it in the next
> release? I tried making an example but it didnt work, maybe I did
> somthing wrong?
I have an example of define_c_var() in a Linux demo.
It lets the Euphoria program access a C variable in a shared library.
I don't know how well that works in the Windows world.
I think in Windows, it might only be routines that can be
exported from a .dll, not variables. Maybe someone else here has tried it.
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
3. Re: Rob: Q: Constant Scope
- Posted by Brian Broker <brian_broker at yahoo.com>
Apr 19, 2005
-
Last edited Apr 20, 2005
Robert Craig wrote:
>
> I don't know how well that works in the Windows world.
> I think in Windows, it might only be routines that can be
> exported from a .dll, not variables. Maybe someone else here has tried it.
>
> Regards,
> Rob Craig
> Rapid Deployment Software
> <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a>
define_c_var() works just fine with DLLs (assuming the lib actually exports
variables).
-- Brian
4. Re: Rob: Q: Constant Scope
Robert Craig wrote:
>
> Vincent wrote:
> > The ref manual says:
> > "Constants may not be declared inside a subroutine."
> > "Constant declarations must be outside of any subroutine. Constants can
> > be global or local, but not private."
> >
> > Can you tell me why you made the language support only constant declera-
> > tions at top-level, and not support it at routine level (private scope)?
> > I'm just curious on why made it this way. What are the disadvantages?
> >
> > I'm trying to figure out whether or not I want to add this functionability
> > into my
> > custom interpreter, and I wanted to know what you thought about
> > it.
>
> Here's something I wrote a few years ago, that discusses this issue
> and a few other contentious issues:
>
> <a
> href="http://www.listfilter.com/cgi-bin/esearch.exu?fromMonth=2&fromYear=7&toMonth=2&toYear=7&postedBy=rds&keywords=constant+scope+routine">http://www.listfilter.com/cgi-bin/esearch.exu?fromMonth=2&fromYear=7&toMonth=2&toYear=7&postedBy=rds&keywords=constant+scope+routine</a>
>
> In language design there is rarely a "right answer".
> Things are very subjective.
>
I see your point on how this can cause some confusion...
Your example: (slightly modified)
function aaa(integer z)
object d, e, f
constant a = 99 * foo()
integer b = 99 + bar() * 7, h, i, g = 777
atom c
g += 1
h = g
z = h
return z + b / a
end function
In your example, "integer b & g" uses assignment at declare and right
above it is "constant a", which must use that method--plus the mixed
types have the constant in the same group of code without seperation;
that could cause confusion to the untrained eye.
I was thinking about assignment at declare too as a syntax improvement,
but now I think if I were to add support for private constants within routines,
it would be better to leave it out, while still forcing it
upon constants. That alone greatly helps to destinguish constants from
variables.
New example:
function aaa(integer z)
constant a = 99 * foo()
object d, e, f
integer b, h, i, g
atom c
b = 99 + bar() * 7
g = 777
g += 1
h = g
z = h
return (z + (b / a))
end function
All the code in the example above has organized grouping: seperating
constant from types, assigment from decleration, and "return" from
the rest; along with the standard four space block indentation. That
organizes the code and eliminates any confusion.
> > Another question... Is there anyway I can use global variables in
> > Eu2C translated/compiled DLLs, being able to use define_c_var()?
> > If not, could you consider, adding support for it in the next
> > release? I tried making an example but it didnt work, maybe I did
> > somthing wrong?
>
> I have an example of define_c_var() in a Linux demo.
> It lets the Euphoria program access a C variable in a shared library.
> I don't know how well that works in the Windows world.
> I think in Windows, it might only be routines that can be
> exported from a .dll, not variables. Maybe someone else here has tried it.
>
I was talking more about global variables being exported to translated +
compiled DLLs via Euphoria to C translator. I made a extremely simple
example, which didnt seem to work using define_c_var() with the DLL and variable
name (string). I think it would be worthwhile to add support
for this, even if it would'nt be a common use. Or is there something I'm
not understanding about all this?
Thanks,
Vincent
Without walls and fences, there is no need for Windows and Gates.
5. Re: Rob: Q: Constant Scope
Robert Craig wrote:
>
>
>posted by: Robert Craig <rds at RapidEuphoria.com>
>
>Vincent wrote:
>
>
>>The ref manual says:
>>"Constants may not be declared inside a subroutine."
>>"Constant declarations must be outside of any subroutine. Constants can
>>be global or local, but not private."
>>
>>Can you tell me why you made the language support only constant declera-
>>tions at top-level, and not support it at routine level (private scope)?
>>I'm just curious on why made it this way. What are the disadvantages?
>>
>>I'm trying to figure out whether or not I want to add this functionability
>>into my
>>custom interpreter, and I wanted to know what you thought about
>>it.
>>
>>
>Here's something I wrote a few years ago, that discusses this issue
>and a few other contentious issues:
>
>>http://www.listfilter.com/cgi-bin/esearch.exu?fromMonth=2&fromYear=7&toMonth=2&toYear=7&postedBy=rds&keywords=constant+scope+routine
>
>In language design there is rarely a "right answer".
>Things are very subjective.
>
>
>>Another question... Is there anyway I can use global variables in
>>Eu2C translated/compiled DLLs, being able to use define_c_var()?
>>If not, could you consider, adding support for it in the next
>>release? I tried making an example but it didnt work, maybe I did
>>somthing wrong?
>>
>>
>I have an example of define_c_var() in a Linux demo.
>It lets the Euphoria program access a C variable in a shared library.
>I don't know how well that works in the Windows world.
>I think in Windows, it might only be routines that can be
>exported from a .dll, not variables. Maybe someone else here has tried it.
>
>Regards,
> Rob Craig
> Rapid Deployment Software
> http://www.RapidEuphoria.com
>
>
>
>
Copied from the post:
There's an optimization which is near the top of my list, that would
eliminate the need for copy-on-write in many cases. Consider:
function sort(sequence s)
s[i] = ...
end function
sequence x
...
x = {5,6,7,8}
...
x = sort(x)
It would be possible to detect that the value of x, as passed to
sort(), will never be needed again. The implementation could then
arrange that the value passed to sort() will have only *one*
reference count (from s), rather than 2 (from x and s). This would
eliminate the need for a copy.
I think that this is a very reasonable optimization that eliminates most
of the arguments for modifying "pass by reference" values. Have you
ever implemented it?
--
==============================
Too many freaks, not enough circuses.
j.
6. Re: Rob: Q: Constant Scope
Jason Gade wrote:
> copied from Rob Craig's post (a few years ago):
> There's an optimization which is near the top of my list, that would
> eliminate the need for copy-on-write in many cases. Consider:
>
> function sort(sequence s)
>
> s[i] = ...
>
> end function
>
> sequence x
> ...
> x = {5,6,7,8}
> ...
> x = sort(x)
>
> It would be possible to detect that the value of x, as passed to
> sort(), will never be needed again. The implementation could then
> arrange that the value passed to sort() will have only *one*
> reference count (from s), rather than 2 (from x and s). This would
> eliminate the need for a copy.
>
> I think that this is a very reasonable optimization that eliminates most
> of the arguments for modifying "pass by reference" values. Have you
> ever implemented it?
No I haven't, but it's still near the top of my list.
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
7. Re: Rob: Q: Constant Scope
Vincent wrote:
> I was talking more about global variables being exported to translated +
> compiled DLLs
> via Euphoria to C translator. I made a extremely simple
> example, which didnt seem to work using define_c_var() with the DLL and
> variable name
> (string). I think it would be worthwhile to add support
> for this, even if it would'nt be a common use. Or is there something I'm
> not understanding about all this?
I guess what you are suggesting is having global Euphoria
variables declared in the main program, as well as in a .dll,
and having them accessible from either place. I've never given that
much thought. I don't think there's much demand for it since you can
exchange any Euphoria data via parameters and return values. Also,
the scope rules would have to be worked out and documented - what if
you have two or more global variables with the same name in the .dll,
or in the main program? What if the types don't match? Things that are
now resolved at "compile" time, would have to be resolved at
dll-open time, i.e. run-time. I just don't know if I want
to "go there".
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
8. Re: Rob: Q: Constant Scope
Vincent wrote:
>
> ----
> Another question... Is there anyway I can use global variables in
> Eu2C translated/compiled DLLs, being able to use define_c_var()?
> If not, could you consider, adding support for it in the next
> release? I tried making an example but it didnt work, maybe I did
> somthing wrong?
You could do this, but this probably only makes sense for integers, and
you'd have to be careful to keep them at 31 bits, because Euphoria would
interpret bigger things as either atoms or sequences. You can take a look
at my contribution for passing 32-bit integers to a compiled euphoria
routine for some additional insight into what sort of things would be
involved.
Matt Lewis
9. Re: Rob: Q: Constant Scope
Matt Lewis wrote:
>
> Vincent wrote:
> >
> > ----
> > Another question... Is there anyway I can use global variables in
> > Eu2C translated/compiled DLLs, being able to use define_c_var()?
> > If not, could you consider, adding support for it in the next
> > release? I tried making an example but it didnt work, maybe I did
> > somthing wrong?
>
> You could do this, but this probably only makes sense for integers, and
> you'd have to be careful to keep them at 31 bits, because Euphoria would
> interpret bigger things as either atoms or sequences. You can take a look
> at my contribution for passing 32-bit integers to a compiled euphoria
> routine for some additional insight into what sort of things would be
> involved.
>
> Matt Lewis
>
Thanks, Ill take a look at it :)
Regards,
Vincent
Without walls and fences, there is no need for Windows and Gates.
10. Re: Rob: Q: Constant Scope
Brian Broker wrote:
> Robert Craig wrote:
>>
>> I don't know how well that works in the Windows world.
>> I think in Windows, it might only be routines that can be
>> exported from a .dll, not variables. Maybe someone else here has tried it.
>
> define_c_var() works just fine with DLLs (assuming the lib actually
> exports variables).
I translated the most simple sample DLL that I could think of, using
the official Eu 2.5 translator (compiled with Borland and Watcom) on
Windows 98:
------------[ file mydll.ew ]------------
global integer foo
foo = 123
-----------------------------------------
... and tested with the following code
include dll.e
constant
lpLib = open_dll("mydll.dll"),
lpFoo = define_c_var(lpLib, "foo")
? lpLib
? lpFoo
Output is
6291456
-1
What goes wrong here?
Regards,
Juergen
11. Re: Rob: Q: Constant Scope
Juergen Luethje wrote:
>
>
> I translated the most simple sample DLL that I could think of, using
> the official Eu 2.5 translator (compiled with Borland and Watcom) on
> Windows 98:
>
> ------------[ file mydll.ew ]------------
> }}}
<eucode>
> global integer foo
> foo = 123
> </eucode>
{{{
> -----------------------------------------
>
>
> ... and tested with the following code
> }}}
<eucode>
> include dll.e
>
> constant
> lpLib = open_dll("mydll.dll"),
> lpFoo = define_c_var(lpLib, "foo")
>
> ? lpLib
> ? lpFoo
> </eucode>
{{{
>
>
> Output is
> 6291456
> -1
>
> What goes wrong here?
You need to change the declaration for foo. In main_.h, you should see
extern int _1foo;
Change this to:
extern _declspec(dllexport) _1foo;
Now you should be able to:
lpFoo = define_c_var(lpLib, "__1foo")
Note that bcc adds an extra underscore. I don't think bcc allows you to
put variables into the def file, so you seem to be stuck with the ugly
name. I haven't tried with any other compilers.
Matt Lewis
12. Re: Rob: Q: Constant Scope
Matt Lewis wrote:
> Juergen Luethje wrote:
>>
>> I translated the most simple sample DLL that I could think of, using
>> the official Eu 2.5 translator (compiled with Borland and Watcom) on
>> Windows 98:
>>
>> ------------[ file mydll.ew ]------------
>> }}}
<eucode>
>> global integer foo
>> foo = 123
>> </eucode>
{{{
>> -----------------------------------------
<snip>
> You need to change the declaration for foo. In main_.h, you should see
>
> extern int _1foo;
>
> Change this to:
>
> extern _declspec(dllexport) _1foo;
>
> Now you should be able to:
> }}}
<eucode>
> lpFoo = define_c_var(lpLib, "__1foo")
> </eucode>
{{{
> Note that bcc adds an extra underscore. I don't think bcc allows you to
> put variables into the def file, so you seem to be stuck with the ugly
> name. I haven't tried with any other compilers.
That works fine, thank you!
Well, the ugly name isn't a big issue for me, since it's only used once
in the define_c_var() function.
Now I know the reason for my previous confusion. I had assumed that the
translator automatically exports global variables from a DLL. Now I've read
the docs and saw, that this obviously isn't the case.
Thanks,
Juergen
13. Re: Rob: Q: Constant Scope
Juergen Luethje wrote:
>
> Matt Lewis wrote:
>
> > Juergen Luethje wrote:
> >>
> >> I translated the most simple sample DLL that I could think of, using
> >> the official Eu 2.5 translator (compiled with Borland and Watcom) on
> >> Windows 98:
> >>
> >> ------------[ file mydll.ew ]------------
> >> }}}
<eucode>
> >> global integer foo
> >> foo = 123
> >> </eucode>
{{{
> >> -----------------------------------------
>
> <snip>
>
> > You need to change the declaration for foo. In main_.h, you should see
> >
> > extern int _1foo;
> >
> > Change this to:
> >
> > extern _declspec(dllexport) _1foo;
> >
> > Now you should be able to:
> > }}}
<eucode>
> > lpFoo = define_c_var(lpLib, "__1foo")
> > </eucode>
{{{
> > Note that bcc adds an extra underscore. I don't think bcc allows you to
> > put variables into the def file, so you seem to be stuck with the ugly
> > name. I haven't tried with any other compilers.
>
> That works fine, thank you!
> Well, the ugly name isn't a big issue for me, since it's only used once
> in the define_c_var() function.
>
> Now I know the reason for my previous confusion. I had assumed that the
> translator automatically exports global variables from a DLL. Now I've read
> the docs and saw, that this obviously isn't the case.
>
> Thanks,
> Juergen
>
>
Awesome dude thanks :D
Regards,
Vincent
--
Without walls and fences, there is no need for Windows and Gates.