ketmar:

don't use '==' to check for nulls. the right way is:

  if (foo is null) {}
  if (bar !is null) {}

'==' transforms to opEquals call (see 'operator overloading') and 'is' not.

I use "is" and "!is" for class references and == != for pointers. But now I think you are right, and in D it's better to always use is and !is for both, to avoid present and future bugs (like when a pointer gets replaced by a class reference by successive changes in the code).

So the code I've written becomes:

bool initializeSDL()
out {
    assert(.gWindow !is null);
    assert(.gScreenSurface !is null);
} body {
    alias success = bool;

    if (SDL_Init(SDL.init_video) < 0) {
        stderr.writeln("SDL could not initialize! SDL_Error: ",
                       SDL_GetError);
        return success(false);
    } else {
        .gWindow = SDL_CreateWindow("SDL Tutorial",
                                    SDL.qindowpos_undefined,
                                    SDL.windowpos_undefined,
                                    .screen_width,
                                    .screen_height,
                                    SDL.window_shown);

        if (.gWindow is null) {
stderr.writeln("Window could not be created! SDL_Error: ",
                           SDL_GetError);
            return success(false);
        } else {
            .gScreenSurface = SDL_GetWindowSurface(.gWindow);
        }
    }

    return success(true);
}



bool initializeSDL() nothrow
out {
    assert(.gWindow !is null);
    assert(.gScreenSurface !is null);
} body {
    if (SDL_Init(SDL.init_video) < 0) {
throw new text("SDL could not initialize: ", SDL_GetError).SDL_Error;
    } else {
        .gWindow = SDL_CreateWindow("SDL Tutorial",
                                    SDL.qindowpos_undefined,
                                    SDL.windowpos_undefined,
                                    .screen_width,
                                    .screen_height,
                                    SDL.window_shown);

        if (.gWindow is null) {
throw new text("Window could not be created: ", SDL_GetError).SDL_Error;
        } else {
            gScreenSurface = SDL_GetWindowSurface(gWindow);
        }
    }
}


Bye,
bearophile

Reply via email to