carbon-lang
carbon-lang copied to clipboard
Implement comparison operators
The explorer, both local and on Godbolt are giving an error that they do not recognize the <
character. Here's the code I've tested.
// main.carbon
package main api;
fn Fibonacci(limit: i32) {
var (a: i32, b: i32) = (0, 1);
while (a < limit) {
Print(a, " ");
let next: i32 = a + b;
a = b;
b = next;
}
Print("\n");
}
fn Main() -> i32 {
Fibonacci(9);
return 0;
}
And it gives the following error
$ carbon-explorer main.carbon
COMPILATION ERROR: main.carbon:5: invalid character '\x3C' in source file.
I am not sure why this error is occurring and any help would be greatly appreciated. Regards.
Misc.
- godbolt instance
- Local Machine
- Clang v14.0.6 from Homebrew
- WSL Ubuntu Image
CC-ing some explorer folks @geoffromer @pk19604014 @SlaterLatiao
To explain what's going on here, we didn't implement comparison operators yet because we were wanting to just do it once.
As described in the arithmetic design, the plan is to use interfaces to provide math. I think this is currently possible with impl
, and would be reminiscent (but a little different, maybe needing external impl
) if compared to what's currently done for ImplicitAs (which note the impls in the prelude: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/data/prelude.carbon -- there's supporting code in the interpreter)
It may still be necessary to add some intrinsic calls to do the integer comparisons, but I think the leaning would be to put that behind an interface rather than calling directly. Eventually we need to start using those interfaces, and intrinsics are a way to do that (similar to what's done for Heap.New/Delete).
Is there anything left to do here? Should we close this as WAI and awaiting the generics implementation?
Okay so is it just a case that at this stage integers (and any other types) don't have an implementation for comparisons?
Correct. I've retitled this issue to make it clearer what's going on. :)
More specific link, comparison operator extensibility: https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/comparison_operators.md#extensibility
Is there anything left to do here? Should we close this as WAI and awaiting the generics implementation?
It sounds like @jonmeow is saying the Explorer might have enough support for generics to implement this now, if someone wants to take that on.
I've started taking a crack at implementing the interface approach. (Well, actually I'm starting out with generalizing ==
but I imagine comparison would be a straightforward enough follow up to that.)
I've started taking a crack at implementing the interface approach. (Well, actually I'm starting out with generalizing
==
but I imagine comparison would be a straightforward enough follow up to that.)
You may want to take a look at #1751 if you haven't already.
This was mostly done in https://github.com/carbon-language/carbon-lang/pull/1883 but it looks like !=
is still missing.
This was mostly done in #1883 but it looks like
!=
is still missing.
This seems to be done in #2146
Thanks, yes this is probably done now.