[Closed] struct initialization problem
Edit: It works if I add () to the end
[left]
[/left]
struct foo
(
a, b, c,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)
[left]If I do this:[/left]
[left]f1 = foo[/left]
f1.a = 1
[left]I get a runtime error. I guess, if values for the properties are not defined during initialization, they are no longer members of that initialization…[/left]
[left]There’s no problem doing it like this…[/left]
[left]f1 = foo 1 2 3[/left]
f1.a = 2
[left]…except that if I have 20 members, my initialization is going to be really long, and I wouldn’t[/left]
be able to leave any of them null to be determined later on. Since they must be initialized at creation, I’d like to set default values for them in the struct definition, so that I don’t have to specifically declare them all:
[left]
[/left]
struct foo
(
a=0, b=0, c=0,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)
[left]However, this doesn’t seem to work at all…because when I do this:[/left]
[left]f1 = foo[/left]
f1.a
[left]…the result is not 0 as expected, but rather a runtime error.[/left]
try
f1=foo()
if you use:
f1=foo
this means you want f1 to be a reference of foo.
Wich is handy if you have foo_1 and foo_2 and foo_3 and want to switch between them before executing on of their functions, like:
if a==b then executor=foo_1 else executor=foo_2
executor()
if you use
f1=foo()
you want to f1 to be the result of foo.
In your case returning foo with undefined a, b and c.
see also the OOP thread:
http://forums.cgsociety.org/showthread.php?t=272425&highlight=object+oriented
Georg