Skip to content

A plea for Optional #2043

Open
Open
@rebcabin

Description

@rebcabin

In many data-intensive applications, we need sentinel values that signal "nothing interesting here." That's normally handled in ordinary Python via None and the Optional type. Here is an example of what I would like:

from lpython import (i32, dataclass)

@dataclass
class FullFormValue:
    list_i32 : Optional[list[i32]]
    string   : Optional[str]

foo : dict[str, FullFormValue]

ttype    : FullFormValue = FullFormValue(None, 'dimensions')
contents : FullFormValue = FullFormValue([1, 2], None)

foo = {'ttype'   : ttype,
       'contents': contents}

Instead, I have to do something like the following:

from lpython import (i32, dataclass)

@dataclass
class FullFormValue:
    list_i32 : list[i32]
    string   : str

foo : dict[str, FullFormValue]

DEAD_LIST : list[i32] = [-1]  # Issue 2039 forces non-empty sentinel
DEAD_STRING : str = ''  # would prefer None but we don't have Optional

ttype    : FullFormValue = FullFormValue(DEAD_LIST, 'dimensions')
contents : FullFormValue = FullFormValue([1, 2], DEAD_STRING)

foo = {'ttype'   : ttype,
       'contents': contents}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions