Skip to content

Allow "invalid" as a type #1052

@luis-j-soares

Description

@luis-j-soares

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 ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions