evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.04k stars 120 forks source link

Assignment functions with compound statement #832

Closed MatthiasJ1 closed 5 months ago

MatthiasJ1 commented 5 months ago

Assignment functions whose last statement is a compound statement should return the value of the final expression of the active code path.

def f(x)=
    if x:
        # some code
        x+1
    else: 0

def f(x)=
    match x:
        case 0: -1
        case _: x

def f(x)=
    with open(x, "r"):
        x.read()

Compiles to (3.10):

def f(x):
    if x:
        # some code
        return x+1
    else:
        return 0

def f(x):
    match x:
        case 0: return -1
        case _: return x

def f(x):
    with open(x, "r"):
        return x.read()
evhub commented 5 months ago

Closing as duplicate of #190.