Skip to content

Latest commit

 

History

History
83 lines (55 loc) · 1.59 KB

File metadata and controls

83 lines (55 loc) · 1.59 KB

Sentinal Values

In python the value of None is the same as no value.

def func():
    return None

Is equivalent to:

def func():
    return

However this can cause problem when chaining functions together. For example:

def func_a():
    return # Shadow returns None

def func_b():
    # zero args expected
    return "egg" 

## Run chain example
value = func_a()
if value:
    func_b(value)
else:
    func_b()
# TypeError: func_b() takes 0 positional arguments but 1 was given

This occurs because value is populated with None, even though we don't explicitly return a value from func_a.

Using Sentinal Values

To correct this, we apply a sentinal value. A distinct object that is represents the absence of a value.

_SENTINAL = object()    

def func_a():
    return _SENTINAL


def func_b():
    # no value given to this 
    return "egg" 

## Run chain example
value = func_a()
if value is not _SENTINAL:
    func_b(value)
else:
    func_b(func_a())

With this we can detect when a function has explicitly returned no value and handle it accordingly.

Sentinals in Hyperway

Because Hyperway chains together multiple functions, sentinal allows functions to explicitly return no value

When building a node, apply your preferred sentinal value:

UNDEFINED = object() # considered a singleton.
def foo():
    return UNDEFINED 


unit = as_unit(foo, sentinal=UNDEFINED)
connection = make_edge(foo, unit)

When the node is executed, if foo returns UNDEFINED, Hyperway will treat this as no value and will not pass it to downstream nodes.