feature request: assert keyword

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

feature request: assert keyword

Michael Baczynski-2
First, I don't want to start a discussion about the usefulness of asserts - in my casesanity
checking with asserts helped me a lot to pin down bugs when working on complex algorithms, it's
SIMPLE and effective.

So how about some sugar for assert statements, like:
assert condition, "message";

Currently I'm doing this:

#if debug
somepackage.Assert.assert(condition, "message");
#end

..which is a pita ;)

Also, an additional compiler directive like --no-asserts would be great.

best,
michael


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Nicolas Cannasse
Le 22/12/2010 16:12, Michael Baczynski a écrit :

> First, I don't want to start a discussion about the usefulness of
> asserts - in my casesanity checking with asserts helped me a lot to pin
> down bugs when working on complex algorithms, it's SIMPLE and effective.
>
> So how about some sugar for assert statements, like:
> assert condition, "message";
>
> Currently I'm doing this:
>
> #if debug
> somepackage.Assert.assert(condition, "message");
> #end

What about :

class D {
        #if debug
        public static function assert( cond, ?pos : haxe.PosInfos ) {
                ..
        }
        #else
        public static inline function assert( code : Dynamic ) : Void {
        }
        #end
}

Then D.assert(cond) will get eliminated when compiled without -debug

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Michael Baczynski-2
On 22.12.2010 16:16, Nicolas Cannasse wrote:

> Le 22/12/2010 16:12, Michael Baczynski a écrit :
>> First, I don't want to start a discussion about the usefulness of
>> asserts - in my casesanity checking with asserts helped me a lot to pin
>> down bugs when working on complex algorithms, it's SIMPLE and effective.
>>
>> So how about some sugar for assert statements, like:
>> assert condition, "message";
>>
>> Currently I'm doing this:
>>
>> #if debug
>> somepackage.Assert.assert(condition, "message");
>> #end
>
> What about :
>
> class D {
>     #if debug
>     public static function assert( cond, ?pos : haxe.PosInfos ) {
>         ..
>     }
>     #else
>     public static inline function assert( code : Dynamic ) : Void {
>     }
>     #end
> }
>
> Then D.assert(cond) will get eliminated when compiled without -debug
>
Does the compiler exclude the empty assert function or does it generate a function definition?
Otherwise looks good :)

> Best,
> Nicolas
>


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Mario Carbajal
Does the compiler exclude the empty assert function or does it generate a function definition?
It will, because of the inline keyword.

On Wed, Dec 22, 2010 at 12:29 PM, Michael Baczynski <[hidden email]> wrote:
On 22.12.2010 16:16, Nicolas Cannasse wrote:
Le 22/12/2010 16:12, Michael Baczynski a écrit :
First, I don't want to start a discussion about the usefulness of
asserts - in my casesanity checking with asserts helped me a lot to pin
down bugs when working on complex algorithms, it's SIMPLE and effective.

So how about some sugar for assert statements, like:
assert condition, "message";

Currently I'm doing this:

#if debug
somepackage.Assert.assert(condition, "message");
#end

What about :

class D {
   #if debug
   public static function assert( cond, ?pos : haxe.PosInfos ) {
       ..
   }
   #else
   public static inline function assert( code : Dynamic ) : Void {
   }
   #end
}

Then D.assert(cond) will get eliminated when compiled without -debug

Does the compiler exclude the empty assert function or does it generate a function definition?
Otherwise looks good :)


Best,
Nicolas



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Vag Vagoff
In reply to this post by Michael Baczynski-2
On 22.12.2010 17:12, Michael Baczynski wrote:
> So how about some sugar for assert statements, like:
> assert condition, "message";

It will be very convenient.

> Currently I'm doing this:
>
> #if debug
> somepackage.Assert.assert(condition, "message");
> #end
>

I'm doing

private typedef D = debug.Debug.Debug;
class C {
        function m() {
                ...
                D.assert(...)
                ...
        }
}

where Debug class defined as

class Debug {
     #if debug
     static public var assertProc : Bool -> String -> PosInfos -> Bool = defaultAssertProc;
     static public function assert( cond : Bool, ?msg : String, ?pos : PosInfos ) : Bool
         return assertProc(cond, msg, pos)
     private static function defaultAssertProc( cond, msg, pos) {
      if (cond)
                return true;
        msg = msg == null? "assert failed" : "assert failed: " + msg;
         haxe.Log.trace(msg, pos);
        #if js
        if (confirm(msg + "\nPress OK to run debugger, Cancel to continue:")) {
                untyped __js__("debugger");
        }
        #end
        return false;
        }
    #else
     static public inline function assert( cond : Bool, ?msg : String, ?pos : PosInfos ) : Bool return true
    #end
}

Vag.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Nicolas Cannasse
In reply to this post by Michael Baczynski-2
Le 22/12/2010 16:29, Michael Baczynski a écrit :
> Does the compiler exclude the empty assert function or does it generate
> a function definition?
> Otherwise looks good :)

This would make it :

#if !debug extern #end class D { .... }

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Michael Baczynski-2
On 22.12.2010 18:03, Nicolas Cannasse wrote:

> Le 22/12/2010 16:29, Michael Baczynski a écrit :
>> Does the compiler exclude the empty assert function or does it generate
>> a function definition?
>> Otherwise looks good :)
>
> This would make it :
>
> #if !debug extern #end class D { .... }
>
> Nicolas
>
thanks! nice little trick :)

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Hudson Ansley
In reply to this post by Nicolas Cannasse
very cool, would it make sense for this to be in the std library so
that everyone uses it the same way? Since it is eliminated when debug
is false, seems like a no-brainer :-)
Regards,
Hudson

On Wed, Dec 22, 2010 at 12:03 PM, Nicolas Cannasse
<[hidden email]> wrote:

> Le 22/12/2010 16:29, Michael Baczynski a écrit :
>>
>> Does the compiler exclude the empty assert function or does it generate
>> a function definition?
>> Otherwise looks good :)
>
> This would make it :
>
> #if !debug extern #end class D { .... }
>
> Nicolas
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

MarcWeber
In reply to this post by Nicolas Cannasse
Excerpts from Nicolas Cannasse's message of Wed Dec 22 16:16:38 +0100 2010:
> Then D.assert(cond) will get eliminated when compiled without -debug

Probably also macros could be used (using EBlock []) to eliminate code?
I didn't test it.
Inline already gets the job done.
This was for completeness.

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Michael Baczynski-2
In reply to this post by Nicolas Cannasse
On 22.12.2010 18:03, Nicolas Cannasse wrote:

> Le 22/12/2010 16:29, Michael Baczynski a écrit :
>> Does the compiler exclude the empty assert function or does it generate
>> a function definition?
>> Otherwise looks good :)
>
> This would make it :
>
> #if !debug extern #end class D { .... }
>
> Nicolas
>
hi,

seems that the generated code has some leftovers when using asserts defined as extern.
So it's not exactly the same when using conditional compilation flags, which might hurt performance
for small, inlined methods containing assert statements.
Flash:
class TestAssert {
     public static function main() {
         var a = 0;
         var b = 1;
         D.assert(a != b, "error");
     }
}
becomes:

pushbyte 0
convert_i
setlocal1
pushbyte 1
convert_i
setlocal2
pushnull
coerce_a
pop
returnvoid

In JavaScript all assert statements are replaced with null:
class Main {
     public static function main() {
         D.assert(false, "error");
     }
}
becomes:
Main.main = function() {
     null;
}

In cpp, asserts become NULL which is 0 so I guess no problem here.

best,
michael

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Nicolas Cannasse
Le 01/04/2011 18:37, Michael Baczynski a écrit :

> On 22.12.2010 18:03, Nicolas Cannasse wrote:
>> Le 22/12/2010 16:29, Michael Baczynski a écrit :
>>> Does the compiler exclude the empty assert function or does it generate
>>> a function definition?
>>> Otherwise looks good :)
>>
>> This would make it :
>>
>> #if !debug extern #end class D { .... }
>>
>> Nicolas
>>
> hi,
>
> seems that the generated code has some leftovers when using asserts
> defined as extern.
> So it's not exactly the same when using conditional compilation flags,
> which might hurt performance for small, inlined methods containing
> assert statements.
> Flash:
> class TestAssert {
> public static function main() {
> var a = 0;
> var b = 1;
> D.assert(a != b, "error");
> }
> }
> becomes:
>
> pushbyte 0
> convert_i
> setlocal1
> pushbyte 1
> convert_i
> setlocal2
> pushnull
> coerce_a
> pop
> returnvoid
>
> In JavaScript all assert statements are replaced with null:
> class Main {
> public static function main() {
> D.assert(false, "error");
> }
> }
> becomes:
> Main.main = function() {
> null;
> }

This is now partially fixed on SVN : it will only generate "null" if
assert if the last statement in the {} block.

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: feature request: assert keyword

Michael Baczynski-2
On 01.04.2011 18:50, Nicolas Cannasse wrote:

> Le 01/04/2011 18:37, Michael Baczynski a écrit :
>> On 22.12.2010 18:03, Nicolas Cannasse wrote:
>>> Le 22/12/2010 16:29, Michael Baczynski a écrit :
>>>> Does the compiler exclude the empty assert function or does it generate
>>>> a function definition?
>>>> Otherwise looks good :)
>>>
>>> This would make it :
>>>
>>> #if !debug extern #end class D { .... }
>>>
>>> Nicolas
>>>
>> hi,
>>
>> seems that the generated code has some leftovers when using asserts
>> defined as extern.
>> So it's not exactly the same when using conditional compilation flags,
>> which might hurt performance for small, inlined methods containing
>> assert statements.
>> Flash:
>> class TestAssert {
>> public static function main() {
>> var a = 0;
>> var b = 1;
>> D.assert(a != b, "error");
>> }
>> }
>> becomes:
>>
>> pushbyte 0
>> convert_i
>> setlocal1
>> pushbyte 1
>> convert_i
>> setlocal2
>> pushnull
>> coerce_a
>> pop
>> returnvoid
>>
>> In JavaScript all assert statements are replaced with null:
>> class Main {
>> public static function main() {
>> D.assert(false, "error");
>> }
>> }
>> becomes:
>> Main.main = function() {
>> null;
>> }
>
> This is now partially fixed on SVN : it will only generate "null" if assert if the last statement
> in the {} block.
>
> Nicolas
>
thanks! besides, I was wrong with the cpp target - doesn't work at all but I added a bug report:
http://code.google.com/p/hxcpp/issues/detail?id=114

--
haXe - an open source web programming language
http://haxe.org