How do you reference a specific implementation of an operator?


let sortedInts = intArray.sorted(by: >)

Here, the compiler recognizes > as a function name and picks out the appropriate implementation using contextual type information.

However, it seems that in the case of operator names, this magic occurs solely in the context of function calls. General expressions cannot reference operator names, even when the relevant types are fully specified. For example, the following does not work:

let comparator: (Int, Int) -> Bool = >

A non-operator function name would work fine here; only operators seem to cause confusion.

I know it's possible to work around this limitation by wrapping operators in closures or by running them through a function that accepts them as a parameter. But these both seem like hacks. Is there a more direct way to refer to specific operator implementations?

It's not really relevant, but the specific use case is that I'd like to test an implementation with multiple comparator functions - ideally, something simple like

for comparator in [ <, <=, >, >= ] {

(Of course, the driver table might need to specify different expected results for those different comparison operators. And this specific example doesn't provide the type information necessary to disambiguate the operator functions.)

This works:

let comparator: (Int, Int) -> Bool = (>)

Ah, magic! Works perfectly, thanks!

    let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]

That is some weird coloring.

Edit: Wait, why are the operators weird colors in your post but not mine?

Unknown, captain. But I'd imagine Discourse has its own syntax highlighting engine and doesn't use the Clang-based parser. Bare operators as function names are kind of an oddball case.

Quoted code blocks act funny in Discourse

Discourse uses highlight.js. To get Swift highlighting you have to use Markdown to tell it what language it should be using to highlight.


I think it's parsing it as a function declaration? You're welcome to take a look at highlight.js, which is what Discourse uses.

EDIT: No, I took a closer look and it looks like Discourse is applying JavaScript's syntax highlighting instead of Swift's.

Egad, you're right. I didn't fix above since that would make the past discussion not make sense, but here is that code again with the language annotation:

    let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]

Is Swift not the default code highlighting for these forums? I believe there is a per-forum setting for that somewhere in the bowels of Discourse.

This would be really great. Can we do this @Nicole_Jacque?

Hmm... Another data point:

Actually, the thing that seems to fix my original formatting is not the explicit "swift" designation but the use of triple backticks (```) to set off the quoted code. Here it is again with the backticks but without the explicit "swift":

let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]

In the previous post, I was relying on the fact that when you set indented lines to "Preformatted text" mode, it seems to auto-format them as code:

   let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]

But evidently, Discourse highlights for a different language in this context, even though the main default might already be set to Swift.

Really, the underlying issue here is that there's no guidance about how to format code shown in the Compose window. No link to a full markdown reference, no format-as-code icon, no links to formatting help of any kind. In fact, that information doesn't seem to exist anywhere on the site.

1 Like

Hm, I thought I had. Maybe it got lost in the transition... Let me look.

From my experience they follow GitHub Flavored Markdown. There might be some differences, but until now I haven't been let down yet.

Yeah, it's already set. @sam.saffron, any idea?

let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]
let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]

Yes ... this is a bug, I will get it fixed, can you report it on so it does not get lost? (also where are my emojis :cat:‍:bust_in_silhouette:)

1 Like

Reported at