rokucommunity / rokucommunity/brighterscript

Allow "invalid" as a type

Open
#1,052 1 comment 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

It would be extremely useful to allow invalid as a type, for cases like the one below:

interface ISomething
    id as String
    name as String
    value as Integer
end interface

'...

myMapOfThings = {} ' as Dictionary<string, ISomething>

function getSomething(id as String) as ISomething or invalid
    ' The "id" might not exist in the map, leading to this function returning `invalid`.
    '   If I can't specify that `invalid` can be returned, then the user might assume that
    '   it always returns something, and then have the Roku crash when attempting to use it.
    return m.myMapOfThings[id]
end function

item = getSomething("nonExistingId")
print item ' prints "invalid"
print item.name ' CRASH!

function ensureSomething(id as String) as ISomething
    ' This function always returns something, so there's no need to specify "or invalid" in
    '   the return type. And the user knows they don't need to check for invalid values
    '   either.
    item = m.myMapOfThings[id]
    if item = invalid
        item as Something = {
            id: id
            name: ""
            value: -1
        }
        m.myMapOfThings[id] = item
    end if
    return item
end function

item = ensureSomething("nonExistingId")
print item ' prints "<roAssociativeArray>"
print item.name ' prints ""

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

The issue body is the available starting point; trace how return types and the existing invalid value are represented and checked. Determine the scope needed for ISomething or invalid, then verify that functions can declare and return this type without weakening existing type checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.