Custom Operator only works sometimes

I'm trying out a custom operator I found in a blog:

infix operator •
func • <I, O>(lhs: (I) -> O, rhs: I) -> O { return lhs(rhs) }

func foo(_:String) { }
let param = "idk"
foo • param

What I don't get is sometimes this compiles and sometimes I get the error:

operator is not a known binary operator 
foo • param

I'll literally build the exact same code in an Xcode playground and sometimes it compiles and sometimes it doesn't. I don't understand why? I don't know if it's because my computer's old, or it's a glitch or what?

It works fine on both iOS Playground (3.4) and Xcode playground (12.3). Did you do something between the time it succeeds and the time it fails?

It's also possible that the error was lingering from the time before you declare the operation, but it should still run just fine.

I prefer using a Command Line Tool project for experimentation like this, since I've been frustrated by Playground every time I've made another attempt to use it.

Did you do something between the time it succeeds and the time it fails ?

I suppose I must have, but I have no idea what I did.

It's also possible that the error was lingering from the time before you declare the operation, but it should still run just fine.

Yes, it took me a while to figure out that sometimes errors just linger, even after I fix them. The error will sometimes stay in the log, even if the error doesn't even make sense anymore (e.g. I completely deleted the section that the error is complaining about).

But I'm certain that it's not only a lingering error, because I'll execute the program line by line to see what happens, and it will break on that same line and throw up the operator is not a known binary operator error. (But other times I'll execute the exact same code, no typing at all, and it won't throw an error).

Do playground pages look at other playground pages? I'm experimenting with a lot of different things and so I have a bunch of playground pages in the same workspace with different versions of the same structs that I'm working on. I'm wondering if any of the redundant declarations from other pages are confusing the compiler. I have noticed that Playgrounds starts to slow down as I add more pages and code.

Perhaps I should look into that. Would you mind sending a tutorial on how to do that?

That'd be a very short tutorial :slightly_smiling_face::

  • In Xcode: File -> New -> Project
  • Select "Command Line Tool" in the macOs tab
  • Next etc.

Write code in the main.swift and press CMD+B to compile, CMD+R to (compile and) run.

1 Like

I'd also recommend that you try iOS Playground (via catalyst).

Each page (in the same playground) is considered to be its own module, so you'd need public access to cross between pages.

You may want to file a bug report (via feedback assistant), though I couldn't replicate the result, so it's hard to say.

1 Like

Did a little more experimenting and now I can finally repeat it on demand. It has something to do with the way that the step through execution within Xcode Playgrounds (that blue triangle circle button on the left on the line numbers).

I like to write a few lines then hit Shift + Enter which builds the code and runs it up to that line, then stops execution at that line.

How to replicate the error:
Execute up to line with the definition func • <I, O>(lhs: (I) -> O, rhs: I) -> O { return lhs(rhs) }
//This compiles fine
and THEN execute up to foo • bar
//This breaks

But here's how to replicate the success:
Execute both the operator overloading definition and the operator call in the same pass.
//This compiles and runs smoothly. Success.
But if you ever call the operator on another line and execute that then that will break.

It's as if Xcode playground forgets the definition of the operator overloading after each run.

@Lantua (I'm not sure if iOS Playgrounds will behave the same way. I haven't even figured out how to execute line by line on iOS Playgrounds.)

Yea, you may want to file a report (should be over https://feedbackassistant.apple.com).

You can click the speedometer next to the run button. AFAICT, they use different engines.

1 Like

This is a known bug:

2 Likes
Terms of Service

Privacy Policy

Cookie Policy