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

Adding a native 'bool' type #56

Open
Lehona opened this issue Jul 9, 2019 · 0 comments
Open

Adding a native 'bool' type #56

Lehona opened this issue Jul 9, 2019 · 0 comments

Comments

@Lehona
Copy link

Lehona commented Jul 9, 2019

As discussed on Discord we'd like to add support for a built-in type 'bool' which can only be inhabited by true/false.

Improvements:

  • We would like programmers to increase their compile-time expressiveness by asserting that a certain variable can only be inhabited by true or false.
  • Expressions using boolean operators such as ==, && or ! will produce a boolean value.

Problems:

  • The values 'true' and 'false' are already defined as constants in Daedalus (with the value 1 and 0 respectively). We can probably just ignore their definition.
  • Just like in C, statements of the form if (intVar) are used quite frequently. We need to accept passing integers into places where a boolean expression is expected, otherwise we break too much code.
  • Conversely, booleans will need to be implicitly converted to integers as well, to support code that is currently using integers as a replacement for booleans, e.g. if (intVar == true), where converting intVar to boolean would be incorrect.
  • If we want to enforce some stricter typechecking, we need an explicit way to convert a boolean into an integer. For now we can provide a built-in function to do so, which becomes a NOP during bytecode creation. For compatibility reasons a daedalus equivalent can be introduced when working with other compilers.

Open Questions:

  • Do we allow implicit conversion from int to bool when providing parameters for a function? I'd like not to, but this will make refactoring a whole lot more annoying, having to change foo(intVar) to foo(intVar != 0) everywhere. On the other hand, this will make refactoring a lot easier because the compiler will yell at places you have forgot to change/refactor.

Here are some examples, including my thoughts on what should happen internally/at the type level.
Examples:

  • if (intVar == True) True is promoted to an integer, using True = 1 and False = 0.
  • if (intVar) Because if-statements expect a boolean value, the integer has to be conceptually converted to a boolean with C-like semantics, i.e. 0 => False, everything else => True. We don't have to change anything, though, because that is what Gothic already does anyway.
  • intVar = True; This should maybe throw a warning or error only in strict mode and convert implicitly in non-strict/compatibility mode.
  • boolVar = 0; this should always throw an error, because the bool datatype is already opt-in, i.e. we have no legacy code to consider.

Please post any additional thoughts you have and I will try to keep the OP updated.

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

No branches or pull requests

1 participant