rokucommunity / rokucommunity/brighterscript

Allow passing function references into `observeField`

Open
#522 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
208
Forks
68
Avg merge
8h 39m
Merged PRs (30d)
39

Description

BrighterScript should support passing function references into observeField and observerFieldScoped methods.

Named functions

sub init()
    m.top.observeField("someField", callback)
end sub
sub callback()
end sub

transpiles to:

sub init()
    m.top.observeField("someField", "callback")
end sub
sub callback()
end sub

Anonymous functions

sub init()
    m.top.observeField("someField", sub()
    end sub)
end sub

transpiles to:

sub init()
    m.top.observeField("someField", bslib_getFunctionName(sub()
    end sub))
end sub

'bslib.brs
sub bslib_getFunctionName(func as Function)
    return func.toStr().mid(10)
end sub

Considerations:

  • we won't be able to track all variable types without a proper type system. So something like this might be risky for a little while. We could get around that by warning users in all situations where we don't know what the variable type is?:
    someFunc  = function()
    end function
    m.top.observeField("someField", someFunc)
    

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing compiler handling of observeField and observerFieldScoped calls, then compare the named and anonymous function-reference examples in the issue. Determine how the compiler should distinguish safe references from unknown variable types, including when to emit bslib_getFunctionName, and verify that both examples transpile as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.