[Closed] Can functions be declared in Structs?
You need to call the fn from the struct:
fn myFunction01 = (
myStruct.myFunction02()
)
Light
You need to call the fn from the struct:
fn myFunction01 = (
myStruct.myFunction02()
)
Light,
How would I do that if I was running the struct as an instance? Would use the struct name or the variable name that holds the instance?
Struct name, but note that with this:
fn myFunction01 = (
myStruct.myFunction02()
)
I meant to show the fn definition in the original struct.
Light
struct testSt
(
fn myFn=(),
fn run=
(
myFn()
),
fn myFn=
(
print "This works"
)
)
testInst=testSt()
testInst.run()
This is the way that I go about it. Just declare the fn uptop like you might declare a variable but as a function.
Bobo, this is one of those cases that I find I get errors if I don’t create an instance of it. I often have fn’s calling fn’s in the struct but I do it directly.
If I do this:
struct testSt
(
fn myFn=(),
fn run=
(
myFn()
),
fn myFn=
(
print "This works"
)
)
testSt.run()
I get an error telling me that it needs an instance. Or I can do this.
struct testSt
(
fn run=
(
testSt.myFn()
),
fn myFn=
(
print "This works"
)
)
testSt.run()
This gets around having to create the instance of the struct as well I don’t have to declare the function for scope.
What is the better method? I have used both without problems but I have never really done any major testing on them. I should point out that I have gone with the instance of the struct because I understood that access a struct was slower then directly access the functions. Is this the case?
Ok, so now I think you guys are hitting my problem on the head! Thanks so much.
So Paul, it looks like you’re using a “dummy” function definition to act as a declaration, and then you just re-define the function whenever you like. That’s exactly what I was looking for.
Light, you’ve introducted my to and idea I hadn’t thought of before; calling functions of a struct from the outside in. That may come in handy in the future, thanks.
Bobo, thanks for addressing the issue of using global structs to tidy up groups of related functions. That is also opening my mind to new possibilities…
struct testSt
(
fn run=
(
stTime=timeStamp()
for x = 1 to 10000 do
(
testSt.myFn()
)
edTime=timeStamp()
format "Processing took % seconds
" ((edTime - stTime) / 1000.0)
),
fn myFn=
(
print "This works"
)
)
testSt.run()
Processing took 12.766 seconds
Processing took 9.766 seconds
Processing took 9.985 seconds
Processing took 10.25 seconds
Processing took 9.734 seconds
struct testSt
(
fn myFn=(),
fn run=
(
stTime=timeStamp()
for x = 1 to 10000 do
(
myFn()
)
edTime=timeStamp()
format "Processing took % seconds
" ((edTime - stTime) / 1000.0)
),
fn myFn=
(
print "This works"
)
)
testInst=testSt()
testInst.run()
Processing took 10.297 seconds
Processing took 9.703 seconds
Processing took 9.875 seconds
Processing took 9.703 seconds
Processing took 10.0 seconds
Processed both 5 times and those are the results. Doesn’t look like there is a big difference in speed. So it looks like you only really need to use the instance if you want to get access to variables in the struct which I do use from time to time as I will store data collected in a struct that will be used by other structs.
And the last test without the struct:
global myFn
fn run=
(
stTime=timeStamp()
for x = 1 to 10000 do
(
myFn()
)
edTime=timeStamp()
format "Processing took % seconds
" ((edTime - stTime) / 1000.0)
)
fn myFn=
(
print "This works"
)
run()
Processing took 10.359 seconds
Processing took 10.719 seconds
Processing took 10.516 seconds
Processing took 10.407 seconds
Processing took 10.515 seconds
Paul,
What if myFn has variables. Eg.
Fn MyFn a b = (
print a
print b
)
eek
Your function has PARAMETERS. Just pass them as parameters, they will be local to the function and garbage collected when the function exits.
Not sure what the problem would be…