stevelaw18
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
08:17 AM
Function defined within Function
Is it possible to define a function within another function?
What about returning a function from within another function?
What about returning a function from within another function?
11 REPLIES 11

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
08:48 AM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
stevelaw18
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
10:30 AM
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.
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.
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
02:35 PM
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.
I have over a dozen functions defined within the parser object, none of them in the global scope.

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
03:03 PM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
03:29 PM
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.

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
05:07 PM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)

RokuKevin
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2012
07:27 PM
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
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
stevelaw18
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2012
07:11 AM
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?
MSGreg
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2012
08:39 AM
Re: Function defined within Function
An excellent library of functions uses this methodology: libRokuDev