1. Function initialization at the first call.
- Posted by SnakeCharmer Jan 17, 2013
- 1229 views
For example, I need function for exctraction of values from some sequence. This sequence must be builded before any extractions (for example, filled with random data). How to do such initialization? I thought up only one way yet.
sequence String = "" function InitString() String = "I'm initialized now!" end function export function SymbolFromString(integer Position) if length(String) = 0 then InitString() end if return String[Position] end function
2. Re: Function initialization at the first call.
- Posted by mattlewis (admin) Jan 17, 2013
- 1213 views
For example, I need function for exctraction of values from some sequence. This sequence must be builded before any extractions (for example, filled with random data). How to do such initialization? I thought up only one way yet.
More or less...in 4.0 you can also use object() to determine if a variable has been initialized:
sequence String function InitString() String = "I'm initialized now!" end function export function SymbolFromString(integer Position) if not object(String) = 0 then InitString() end if return String[Position] end function
Matt
3. Re: Function initialization at the first call.
- Posted by jimcbrown (admin) Jan 17, 2013
- 1215 views
For example, I need function for exctraction of values from some sequence. This sequence must be builded before any extractions (for example, filled with random data). How to do such initialization? I thought up only one way yet.
More or less...in 4.0 you can also use object() to determine if a variable has been initialized:
sequence String function InitString() String = "I'm initialized now!" end function export function SymbolFromString(integer Position) if not object(String) = 0 then InitString() end if return String[Position] end function
Matt
Err ... shouldn't that be
if object(String) = 0 then
or
if not object(String) then
4. Re: Function initialization at the first call.
- Posted by SnakeCharmer Jan 18, 2013
- 1160 views
Thanks. I am glad that the principle was good. For some reason I was afraid that it is inadmissible ideologically.
UPD: By the way, it would be much more convenient to make String equal to "" initially. What is the best way to check emptiness of string?
if length(String) = 0 then ... if String = {} then ... if equal(String, {}) then ...
or something else?
5. Re: Function initialization at the first call.
- Posted by mattlewis (admin) Jan 18, 2013
- 1160 views
Thanks. I am glad that the principle was good. For some reason I was afraid that it is inadmissible ideologically.
The only other way would be to have a separate variable as a flag to identify whether you have initialized it. The 4.0 update to object()'s behavior was intended to simplify this.
UPD: By the way, it would be much more convenient to make String equal to "" initially. What is the best way to check emptiness of string?
Possibly, but this could also lead to other program errors. Basically, it would be more difficult to catch places where you forgot to initialize something correctly, since we couldn't produce an error. In any case, there is no "default" variable value for any sort of euphoria objects, and I don't see that happening.
Matt
6. Re: Function initialization at the first call.
- Posted by petelomax Jan 19, 2013
- 1114 views
FYI, I also changed object() in Phix because, as you said, one less variable, just better.
The only other way would be to have a separate variable as a flag to identify whether you have initialized it. The 4.0 update to object()'s behavior was intended to simplify this.
By the way, it would be much more convenient to make String equal to "" initially.
Possibly, but this could also lead to other program errors. In any case, there is no "default" variable value for any sort of euphoria objects, and I don't see that happening.
Again fyi only, prior to the change to object(), I used separate flags, and initialised the (static integer) flag at compile-time, which was not possible with non-integers (due to reference counting and other logistical issues). So instead of:
a() ... string s = "string" -- (or "", same problem) procedure a() ?s -- oops, s not assigned end procedure
I would code
a() integer init=0 -- (can be initialised at compile time) string s procedure a() if not init then s = "string" init = 1 end if ?s end procedure
In short, forward calls and initialisation of non-integers gave me headaches in Phix too, prior to the change to object().
Pete
7. Re: Function initialization at the first call.
- Posted by SnakeCharmer Jan 19, 2013
- 1097 views
Possibly, but this could also lead to other program errors. Basically, it would be more difficult to catch places where you forgot to initialize something correctly, since we couldn't produce an error. In any case, there is no "default" variable value for any sort of euphoria objects, and I don't see that happening.
Matt
I mean that default value "" is convenient for building string with "&" and "append" operators. It will be really better to make here thus?
sequence String -- instead of: sequence String = "" function InitString() String = "" -- Sometime we should make it. for i = 1 to 1000 do String &= (remainder(i, 26) + 'A') end for end function export function getSymbol(integer Position) if not object(String) then InitString() end if return String[Position] end function