# How do you reference a specific implementation of an operator?

**URL:** <https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811>\
**Category:** Using Swift\
**Created:** [February 12, 2018, 6:46am UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811 "2018-02-12T06:46:19Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 6:46am UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/1 "2018-02-12T06:46:19Z")

</div>

Consider

```
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.)

---

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [February 12, 2018, 9:53am UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/2 "2018-02-12T09:53:57Z")

</div>

> [@GarthSnyder](#):
>
> let comparator: (Int, Int) -\> Bool = \>

This works:

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

```

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 8:06pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/3 "2018-02-12T20:06:11Z")

</div>

Ah, magic! Works perfectly, thanks!

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

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [February 12, 2018, 8:10pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/4 "2018-02-12T20:10:51Z")

</div>

> [@GarthSnyder](#):
>
> ```swift
> let comparators: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=)]
> 
> ```

That is some weird coloring.

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

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 8:21pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/5 "2018-02-12T20:21:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [February 12, 2018, 8:42pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/6 "2018-02-12T20:42:12Z")

</div>

> [@Nevin](#):
>
> Wait, why are the operators weird colors in your post but not mine?

Quoted code blocks act funny in Discourse

---

<div class="post-metadata">

**Author:** ![nuclearace](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nuclearace](https://forums.swift.org/u/nuclearace)\
**Post date:** [February 12, 2018, 8:42pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/7 "2018-02-12T20:42:59Z")

</div>

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

```swift  
```

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [February 12, 2018, 8:43pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/8 "2018-02-12T20:43:17Z")

</div>

~~I think it's parsing it as a function declaration? You're welcome to take a look at [highlight.js](https://github.com/isagalaev/highlight.js/blob/master/src/languages/swift.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.

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 8:48pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/9 "2018-02-12T20:48:41Z")

</div>

> [@nuclearace](#):
>
> Discourse uses highlight.js. To get Swift highlighting you have to use Markdown to tell it what language it should be using to highlight.

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:

```swift
    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.

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [February 12, 2018, 9:07pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/10 "2018-02-12T21:07:18Z")

</div>

> [@GarthSnyder](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 9:26pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/11 "2018-02-12T21:26:13Z")

</div>

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":

```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.

---

<div class="post-metadata">

**Author:** ![Nicole\_Jacque](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicole_jacque/32/245_2.png) [@Nicole\_Jacque](https://forums.swift.org/u/Nicole_Jacque)\
**Post date:** [February 12, 2018, 9:27pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/12 "2018-02-12T21:27:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [February 12, 2018, 9:27pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/13 "2018-02-12T21:27:44Z")

</div>

From my experience they follow [GitHub Flavored Markdown](https://help.github.com/articles/basic-writing-and-formatting-syntax/). There might be some differences, but until now I haven't been let down yet.

---

<div class="post-metadata">

**Author:** ![Nicole\_Jacque](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicole_jacque/32/245_2.png) [@Nicole\_Jacque](https://forums.swift.org/u/Nicole_Jacque)\
**Post date:** [February 12, 2018, 9:29pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/14 "2018-02-12T21:29:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sam.saffron](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sam.saffron/32/1161_2.png) [@sam.saffron](https://forums.swift.org/u/sam.saffron)\
**Post date:** [February 12, 2018, 9:38pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/15 "2018-02-12T21:38:10Z")

</div>

> [@GarthSnyder](#):
>
> But evidently, Discourse highlights for a different language in this context,

```swift
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 [meat.discourse.org](http://meat.discourse.org) so it does not get lost? (also where are my emojis 🐈‍:bust\_in\_silhouette:)

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [February 12, 2018, 10:00pm UTC](https://forums.swift.org/t/how-do-you-reference-a-specific-implementation-of-an-operator/9811/16 "2018-02-12T22:00:52Z")

</div>

[Reported](https://meta.discourse.org/t/default-code-formatting-language-varies-unexpectedly/80418) at [meta.discourse.org](http://meta.discourse.org).
