Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert macro for non-release #4263

Open
will opened this issue Apr 9, 2017 · 5 comments
Open

assert macro for non-release #4263

will opened this issue Apr 9, 2017 · 5 comments

Comments

@will
Copy link
Contributor

will commented Apr 9, 2017

I've been working on some numeric code that benefits from a lot of invariant checks while developing and testing. However having them all run in release mode is not necessary.

I made a macro that has been helpful. Is this something that you all would want in the language itself?

~ ➤ cat assert.cr
macro assert(exp, file = __FILE__, line = __LINE__)
  {% if !flag?(:release) %}
    unless {{exp}}
      raise "Assertion Failed #{{{file}}}:#{{{line}}}"
    end
  {% end %}
end

def test
  puts "evaulated"
  false
end

assert test
~ ➤ crystal run assert.cr
evaulated
Assertion Failed /Users/will/assert.cr:14 (Exception)
0x10750b685: *CallStack::unwind:Array(Pointer(Void)) at ??
0x10750b621: *CallStack#initialize:Array(Pointer(Void)) at ??
0x10750b5f8: *CallStack::new:CallStack at ??
0x107505881: *raise<Exception>:NoReturn at ??
0x107505861: *raise<String>:NoReturn at ??
0x1075053b8: __crystal_main at ??
0x10750a858: main at ??
~ ➤ crystal run --release assert.cr
~
@Sija
Copy link
Contributor

Sija commented Sep 30, 2017

@asterite @oprypin how about adding assert macro to stdlib?

@oprypin
Copy link
Member

oprypin commented Sep 30, 2017

Yep, I'm definitely all for it

@oprypin
Copy link
Member

oprypin commented Sep 30, 2017

The proposal is not very convincing though.
This is not for unittests.
The assert can be sprinkled throughout the code for sanity checks. "This definitely should be true, if it's not, something went totally wrong". Checks that you don't necessarily have to write and that are good for leaving out in release mode.

@Sija Sija mentioned this issue Sep 30, 2017
@al6x
Copy link

al6x commented Dec 27, 2019

+1, would be nice to have in stdlib, and during release too. Maybe leave ability to disable it in special circumstances

{% unless flag?(:ludicrous_speed) %}

@konovod
Copy link
Contributor

konovod commented Dec 28, 2019

There is a little need in macro if it used in release - you can just do raise "My custom assertion message" if exp.
And using it only in debug is a bad practice imho - release is a place where extra checks won't hurt (except small portion of time-critical code).

Another thing is that it can modify control flow (type of exp was T? without check and T with check). It is unlikely that it can cause bugs though, most likely code would just throw compile-time error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants