August 23, 2014
On Saturday, 23 August 2014 at 11:07:23 UTC, ketmar via Digitalmars-d-learn wrote:
> and "foo is null" is nice to read. ;-)

function bool init
begin
    rem Initialization flag
    bool success assign true;

    rem Initialize SDL
    if execute SDL_Init SDL_INIT_VIDEO lt 0
    begin
        execute printf "SDL could not initialize! SDL_Error: %s\n",
execute SDL_GetError;
        success assign false;
    end
    else
    begin
        rem Create window
        gWindow assign execute SDL_CreateWindow "SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN;
        if gWindow is NULL
        begin
            execute printf "Window could not be created! SDL_Error:
%s\n", execute SDL_GetError;
            success assign false;
        end
        else
        begin
            rem Get window surface
            gScreenSurface assign execute SDL_GetWindowSurface gWindow;
        end;
    end;

    return success;
end;

Couldn't resist... :)
August 23, 2014
On Sat, 23 Aug 2014 15:10:22 +0000
Kagamin via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> Meh, looks like a guest from pascal or basic.
so let's replace writeln() with %&^#$#%^& then. this looks like a
function name for Real Hackers.


August 25, 2014
On 8/23/2014 7:19 PM, nikki wrote:

>
> How would you write it?
```
// Put this somewhere you can import it into any module calling SDL
class SDLError : Error {
    public this( string msg, string file = __FILE__, size_t line = __LINE__ ) {
        import std.string;
        import std.conv;
        import derelict.sdl2.sdl;

        auto fmsg = format( "%s: %s", msg, to!string( SDL_GetError() ));
        super( fmsg, file, line, null );
    }
}

void init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        throw new SDL_Error( "Failed to init SDL" );
    }


    //Create window
    gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    if( !gWindow )
    {
        throw new SDLError( "Failed to create window" );
    }

    //Get window surface
    gScreenSurface = SDL_GetWindowSurface( gWindow );
}

```

Regarding 'is' vs == for equality, I prefer to use the latter with pointers returned from C so that I can keep in mind that it actually is something given to me from C and is not managed by the GC. I can tell at a glance.

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

August 25, 2014
On Monday, 25 August 2014 at 02:17:47 UTC, Mike Parker wrote:
> // Put this somewhere you can import it into any module calling

Small modification for even terser error handling:

T sdlEnforce(T)(T result, string message = null)
{
  if (!result)
    throw new SdlException("SDL error: "
      ~ (message ? message ~ ": " : "")
      ~ to!string(SDL_GetError()));
  return result;
}

Usage:

gWindow = SDL_CreateWindow(...).sdlEnforce("SDL_CreateWindow");

> class SDLError : Error {

This is misuse of the Error class. Don't do this, subclass Exception instead. D errors should be used to indicate logic errors (conditions that can only arise from a bug in the program), not failures in 3rd-party libraries.
August 25, 2014
On 8/25/2014 11:35 AM, Vladimir Panteleev wrote:
> On Monday, 25 August 2014 at 02:17:47 UTC, Mike Parker wrote:
>> // Put this somewhere you can import it into any module calling
>
> Small modification for even terser error handling:
>
> T sdlEnforce(T)(T result, string message = null)
> {
>    if (!result)
>      throw new SdlException("SDL error: "
>        ~ (message ? message ~ ": " : "")
>        ~ to!string(SDL_GetError()));
>    return result;
> }
>
> Usage:
>
> gWindow = SDL_CreateWindow(...).sdlEnforce("SDL_CreateWindow");
>
>> class SDLError : Error {
>
> This is misuse of the Error class. Don't do this, subclass Exception
> instead. D errors should be used to indicate logic errors (conditions
> that can only arise from a bug in the program), not failures in
> 3rd-party libraries.

I use Exception for recoverable errors and Error for those that aren't.

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

August 27, 2014
On Monday, 25 August 2014 at 03:19:09 UTC, Mike Parker wrote:
> I use Exception for recoverable errors and Error for those that aren't.

Sorry, you're right, that description of Exception/Error is correct. But I don't think that SDL initialization is a non-recoverable error. The program might want to retry SDL initialization with different parameters, and if that code would make it into a library, said library might try using a different graphics library or use some other form of user interaction.
August 27, 2014
On Wednesday, 27 August 2014 at 05:39:59 UTC, Vladimir Panteleev wrote:
> On Monday, 25 August 2014 at 03:19:09 UTC, Mike Parker wrote:

>
> Sorry, you're right, that description of Exception/Error is correct. But I don't think that SDL initialization is a non-recoverable error. The program might want to retry SDL initialization with different parameters

While this may be true in this case, I think that, in general, you cannot draw such a clear line between what's recoverable and what's not. If you really want to push things to the extreme, the sole unrecoverable error shall be assertion failure and the SIGKILL.

Everything else could be otherwise handled, even if for that you'd need to program a new operating system inside your program, just because the one that intend to use is missing ;)

No, that's extreme. But, matter is, what is recoverable and what not, is a matter of what the architect (and the specification) decide to be.
August 27, 2014
On Wednesday, 27 August 2014 at 05:45:34 UTC, eles wrote:
> On Wednesday, 27 August 2014 at 05:39:59 UTC, Vladimir Panteleev wrote:
>> On Monday, 25 August 2014 at 03:19:09 UTC, Mike Parker wrote:
>

> failure and the SIGKILL.

(and SIGKILL just because you cannot catch it, otherwise you could yell at the user...)
August 27, 2014
On Wednesday, 27 August 2014 at 05:45:34 UTC, eles wrote:
> While this may be true in this case, I think that, in general, you cannot draw such a clear line between what's recoverable and what's not. If you really want to push things to the extreme, the sole unrecoverable error shall be assertion failure and the SIGKILL.
That's exactly the line I would draw.
_Nothing_ else is an error, so _everything_ is an exception.
Best forget about that something else even exist.
August 27, 2014
On 8/27/2014 2:39 PM, Vladimir Panteleev wrote:
> On Monday, 25 August 2014 at 03:19:09 UTC, Mike Parker wrote:
>> I use Exception for recoverable errors and Error for those that aren't.
>
> Sorry, you're right, that description of Exception/Error is correct. But
> I don't think that SDL initialization is a non-recoverable error. The
> program might want to retry SDL initialization with different
> parameters, and if that code would make it into a library, said library
> might try using a different graphics library or use some other form of
> user interaction.

It is entirely reasonable for someone to decide that failure to init SDL is an unrecoverable error, while failure to create a window is not, or that both are or neither is. I don't see that any one of the these approaches is set in stone, or that it even need be the same from project to project.

The way I structure my code in the little rinky-dink projects scattered about my hard drive, any failure in initializing required systems (like graphics) is an Error, whereas init failure for noncritical systems (like audio) is an Exception. The former I let filter to a top-level handler where I log them and exit, the latter I catch and respond where appropriate. I would very likely take a different approach were I developing a library for public consumption so that the user can decide.

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

1 2
Next ›   Last »