September 16, 2012
I get this error from a minimal windows example:

import core.runtime;
import std.c.windows.windows;
import std.string;

pragma(lib, "gdi32.lib");

extern (Windows)
{
int WinMain( ... ) { ... }
HRESULT appMain( ... ) {
    ...
    WNDCLASS wc;
    ...
    wc.lpfnWndProc = &wndProc;
    ...
}
HRESULT wndProc( ... ) { ... }
}

Error: cannot implicitly convert expression (& wndProc) of type
extern (Windows) int function(void* hwnd, uint message, uint wParam, int lParam)
to
extern (Windows) int function(void*, uint, uint, int) nothrow
shell returned 1

What does the nothrow stems from? Is this something new?
September 16, 2012
On 16-09-2012 19:39, deed wrote:
> I get this error from a minimal windows example:
>
> import core.runtime;
> import std.c.windows.windows;
> import std.string;
>
> pragma(lib, "gdi32.lib");
>
> extern (Windows)
> {
> int WinMain( ... ) { ... }
> HRESULT appMain( ... ) {
>      ...
>      WNDCLASS wc;
>      ...
>      wc.lpfnWndProc = &wndProc;
>      ...
> }
> HRESULT wndProc( ... ) { ... }
> }
>
> Error: cannot implicitly convert expression (& wndProc) of type
> extern (Windows) int function(void* hwnd, uint message, uint wParam, int
> lParam)
> to
> extern (Windows) int function(void*, uint, uint, int) nothrow
> shell returned 1
>
> What does the nothrow stems from? Is this something new?

nothrow is a function attribute meaning "this function does not throw exceptions". Just mark your wndProc function as nothrow and it should go away.

http://dlang.org/function.html#nothrow-functions

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
September 16, 2012
I did, but then I am not able to use writeln for debugging.
Is this restriction something new?
September 16, 2012
deed:

> I did, but then I am not able to use writeln for debugging.

writeln is a "throwing" function, so you can't use it inside a nothrow function.


> Is this restriction something new?

It's not new. I think writeln was always "throwing", since the creation of "nothrow".

Bye,
bearophile
September 16, 2012
But why is the wndProc function nothrow?
Why do I have to mark it with nothrow?

September 16, 2012
On Sunday, 16 September 2012 at 21:12:42 UTC, deed wrote:
> I did, but then I am not able to use writeln for debugging.
> Is this restriction something new?

nothrow just means the function itself should not *exit* with an exception. It is still legally allowed to call a throwing function, provided promises to handles (catche) any thrown exception. How it deals with the exception (silence/error) is up to it. For example:

--------
void foo() nothrow
{
    try
    {
        writeln("hello world!");
    }
    catch(Exception) { } //silence

    //doStuff
}
--------

Of course, the "try catch do nothing" writting can get old, so you can use std.exception's "collectException" too*;
--------
void foo() nothrow
{
    collectException(writeln("hello world!"));

    //doStuff
}
--------
*Though for me, the compiler sometimes still complains.

Or better yet, you *could* write a "debugWriteln()", which is marked as @trusted nothrow:
--------
@trusted nothrow
void debugWriteln(Args...)(Args args) nothrow
{
    try{writeln(args);}catch(Exception){};
}

void foo() nothrow
{
    debugWriteln("hello world!");

    //doStuff
}
--------
The @trusted is so that it can also be used inside safe functions (which will exhibit the same issue). With this, you can log in any function.
September 16, 2012
On Sunday, 16 September 2012 at 17:38:35 UTC, deed wrote:
> What does the nothrow stems from? Is this something new?

Isn't the real question why the wndProc function is expected to be nothrow?

The change is from this commit 4 months ago: 2886846a92c45d92308756cf4c077ae13f0f8460




September 16, 2012
Exactly. I couldn't remember seeing this error before.


September 16, 2012
On Sunday, 16 September 2012 at 22:08:53 UTC, deed wrote:
> Exactly. I couldn't remember seeing this error before.

I've only used the dsource Win32 bindings, because there is often stuff missing from the phobos ones:

http://www.dsource.org/projects/bindings/wiki/WindowsApi

But I don't understand the reason for the change to the phobos bindings. Like someone above said, the easiest solution is to wrap your wndProc body in a try/catch.
September 17, 2012
On 9/17/12, cal <callumenator@gmail.com> wrote:
> On Sunday, 16 September 2012 at 17:38:35 UTC, deed wrote:
>> What does the nothrow stems from? Is this something new?
> The change is from this commit 4 months ago: 2886846a92c45d92308756cf4c077ae13f0f8460

https://github.com/D-Programming-Language/druntime/pull/225

I don't understand this whole deal, what is or isn't allowed to be catched..
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home