Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
stevelaw18
Visitor

Function defined within Function

Is it possible to define a function within another function?

What about returning a function from within another function?
0 Kudos
11 REPLIES 11
TheEndless
Channel Surfer

Re: Function defined within Function

BrightScript supports anonymous functions, so yes. Functions can also be referenced, so also yes. Your questions are a little bit ambiguous, though. If you could provide an example of exactly what you're wanting to do, it'd be easier to give you a more definitive answer as to whether it's possible or not.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
stevelaw18
Visitor

Re: Function defined within Function

The reason I'm asking is more to understand the capabilities of the language. More or less to find out what methods of organization exist, and ways to preventing the global scope from becoming polluted with many functions.

In JavaScript for instance, I can create a constructor function, that returns an object, and that object still has a reference to any functions defined within the constructor function, due to the closure that is formed.

Also, in JavaScript, I can create an object (which is essentially a hash like brightscript), and define values to be functions, essentially simulating methods on that object.

I wasn't sure if BrightScript had any requirements that functions need to be defined globally, and whether anonymous functions are allowed, but it sounds like they are allowed.
0 Kudos
belltown
Roku Guru

Re: Function defined within Function

For an example, look at the code for my JSON parser in https://github.com/belltown/Roku/blob/master/JSONDecoder/source/JSONDecoder.brs

I have over a dozen functions defined within the parser object, none of them in the global scope.
0 Kudos
TheEndless
Channel Surfer

Re: Function defined within Function

Out of curiosity, what is the concern with declaring them with a global scope, since they can still be referenced for use within a particular object's context?
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
belltown
Roku Guru

Re: Function defined within Function

"TheEndless" wrote:
Out of curiosity, what is the concern with declaring them with a global scope, since they can still be referenced for use within a particular object's context?

In my case, it's so I can create a library that could potentially be used by many other source code files, and not have to worry about naming conflicts. I'm not trying to hide the functions from the caller, just ensure that they don't have the same names as anything that might be used elsewhere. I've run into problems, for example, in some of the Roku example code (e.g. the MRSS code) where I've tried to use some of their utility functions only to find that their names conflict with my own, or in one version of the MRSS template, names that conflicted with function names in other parts of their own code.
0 Kudos
TheEndless
Channel Surfer

Re: Function defined within Function

"belltown" wrote:
"TheEndless" wrote:
Out of curiosity, what is the concern with declaring them with a global scope, since they can still be referenced for use within a particular object's context?

In my case, it's so I can create a library that could potentially be used by many other source code files, and not have to worry about naming conflicts. I'm not trying to hide the functions from the caller, just ensure that they don't have the same names as anything that might be used elsewhere. I've run into problems, for example, in some of the Roku example code (e.g. the MRSS code) where I've tried to use some of their utility functions only to find that their names conflict with my own, or in one version of the MRSS template, names that conflicted with function names in other parts of their own code.

Right. I figured something like that was your reasoning, but wasn't sure if you thought there was some sort of memory implication. I typically give my "class" functions a unique prefix to avoid conflicts, but have used anonymous functions on occasion.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
RokuKevin
Visitor

Re: Function defined within Function

Brightscript does not have namespace functionality or closures, but it does have anonymous functions and associative array objects that can be used to build classes.

There are a couple workarounds you could use to avoid name conflicts:
* Use a naming convention for your global functions with your own unique prefix: mySuperLib_function1(), mySuperLib_function2(),...
* Expose you library functionality via Brightscript classes rather than global functions
* Use the Brightscript singleton pattern to make methods available via a function (named with your unique library identifier) at global scope.

--Kevin
0 Kudos
stevelaw18
Visitor

Re: Function defined within Function

Expose you library functionality via Brightscript classes rather than global functions

What do you mean by Brightscript "classes"? Do you mean using an associative array to simulate an object, like in the JSON parsing example mentioned earlier in this post?

* Use the Brightscript singleton pattern to make methods available via a function (named with your unique library identifier) at global scope.

Do you have an example of this that I can see?
0 Kudos
MSGreg
Visitor

Re: Function defined within Function

An excellent library of functions uses this methodology: libRokuDev
0 Kudos