Hacker News new | past | comments | ask | show | jobs | submit login

If it were a keyword rather than a pseudo-function, I would support its addition. Psuedo-functions should not be allowed into the language, as they tend to have side effects that you wouldn't expect from a function call (such as returning from caller to grandparent).

Some keyword that implies "return if error" so that you could then do:

    try v1, v2, v3 := someFunction()
You could even make it a little bit smart, taking advantage of the return types of the function to automatically fill in named return values:

    func MyFunc(param int) (result int, err error) {
        result = 0
        if param > 0 {
            result = param + 10
        } else {
            try p1, p2 := GetInternalValues(param)
            result = p1 * p2
        }
        return result, err
    }
where "try p1, p2 := GetInternalValues(param)" is syntactic sugar for (in this case):

    var p1, p2 int
    p1, p2, err = GetInternalValues(param)
    if err != nil {
        return result, err
    }



Given that putting 'try' before the variable definitions would probably interfere with the 'var' keyword, it makes a bit more sense to me that it should be put after the assignment operator (something like var try v1 = Thing() is probably lexable consistently, but it would be confusing to remember if it goes var try v1 or try var v1, unless it supported both which seems ish)

    v1 := try someFunction() 
I like that, primarily because it has echos of how the go keyword already operates. But the important thing is that it should be disallowed anywhere that isn't directly next to an assignment, which might be difficult. Like

    // compiler error
    fmt.Printf(try someFunction())
Not sure how easily that can be enforced without affecting the lexability of the grammar or compilation speed, but in a way, it should be thought of as a tokenized part of the assignment, not the RH-expression, such that the assignment tokens become

    =
    :=
    = try
    := try




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: