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

Bools and Enums in generic parametrizations #4337

Open
ozra opened this issue Apr 25, 2017 · 0 comments
Open

Bools and Enums in generic parametrizations #4337

ozra opened this issue Apr 25, 2017 · 0 comments

Comments

@ozra
Copy link
Contributor

ozra commented Apr 25, 2017

I miss the possibility of using Bools and Enums (instead of resorting to empty types for signature decoration) in generic parametrizations, would be well considered in tandem with #934.

With "generic parametrizations" I refer to what Crystal calls "type vars" and "free vars".

I often use booleans in C++ templated classes for costly verification code: allowing noobs, and prototyping concepts, using classes in a clean simple fashion without risk of errors, while allowing pros/performance critical code to use them at a finer granular level without integrity checks. In some cases there's just a small part of the class that might have three or more variations depending on requirement, and then enum is nice (NoBeltsCommonCalc| BeltsOnCommonCalc|BeltsOnAdaptiveCalc).

In embedded real-time coding this is a definite must, though I know that such targets are far from an aim of Crystal (though a lovely thought). With that parenthesis aside: it's obviously good for high-performance code on more capable architectures too.

Pseudo Crystal:

class Foo(T, UseCheck = true)
  @x : T = T.new(0)
  
  def do_stuff(x : T) : Nil
    if UseCheck
      if x > 9
        com.log_fatal "Serious breach of confidence in Foo#do_stuff dude!"
        x = T.new(0)  # hopefully save nuclear power plant from blowing up
      end
    end
    @x = x
  end
end

a = Foo(QU8_56).new
a.do_stuff QU8_56.new(10)  # Lucky me the system was speced to have power enough for safety checks - just a couple of hours of city-wide black out.

b = Foo(QU8_56, false).new  # I'll use it responsibly - no room for checks on this EMF-safe lo-fi arch!
a.do_stuff QU8_56.new(10)   # Not! Well, no need to resign: the world just blew up!
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

2 participants