1. Function initialization at the first call.

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 
new topic     » topic index » view message » categorize

2. Re: Function initialization at the first call.

SnakeCharmer said...

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: Function initialization at the first call.

mattlewis said...
SnakeCharmer said...

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 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Function initialization at the first call.

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?

new topic     » goto parent     » topic index » view message » categorize

5. Re: Function initialization at the first call.

SnakeCharmer said...

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.

SnakeCharmer said...

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: Function initialization at the first call.

FYI, I also changed object() in Phix because, as you said, one less variable, just better.

mattlewis said...

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.

SnakeCharmer said...

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: Function initialization at the first call.

mattlewis said...

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 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu