←back to thread

13 points javatuts | 1 comments | | HN request time: 0.204s | source
Show context
mg ◴[] No.45844298[source]
Hmm... one moment. The first example function they provide:

    function handleResponse(response) {
        return match (response) {
        when ({ status: 200, data }) -> data
        when ({ status: 401 }) -> throw new Error(’Unauthorized’)
        when ({ status: 404 }) -> throw new Error(’Not Found’)
        when ({ status: s if s >= 500 }) -> throw new Error(’Server Error’)
        default -> throw new Error(’Unknown Error’)
      };
    }
Is less readable to me than the way I would write it without the match/when construct:

    function handleResponse(response) {
        status = response.status;
        data   = response.data;
        if (status === 200 && data) return data;
        if (status === 401) throw new Error(’Unauthorized’);
        if (status === 404) throw new Error(’Not Found’);
        if (status  >= 500) throw new Error(’Server Error’);
        throw new Error(’Unknown Error’);
    }
replies(1): >>45844344 #
1. mg ◴[] No.45844344[source]
The match/when approach also needs more code. 393 instead of 360 chars.