sharplet / State

An implementation of the State monad in Swift.
10 stars 2 forks source link

Should this framework define operators? #7

Open sharplet opened 9 years ago

sharplet commented 9 years ago

See #4 and #6 for specific use cases (but mainly #4).

For example, what happens if another framework defines infix operator |> {}, but with conflicting associativity or precedence, and then I import both in the same file? I've defined everything as methods for now, and I think custom operators should mostly just be sugar for those methods where it makes sense and clearly adds benefits to readability/understandability.

Here's some options I can think of:

  1. Do I define the operators, but put them in a nested module so you have to do something like import State.Operators? (Hopefully that's even a thing, right?) That would give users an option for conflict resolution, but if I'm going to have operators they should be there for good reason anyway, so is burying them in a different module/extension worth the loss of discoverability?
  2. Do I depend on an external module that defines the operators already, and just implement them for the State type?
  3. Just go ahead and define them by default and hope for the best? What will happen?? :worried:
  4. Just give up?
sharplet commented 9 years ago

Ok, so here's what happens when you have conflicting operator declarations:

/Users/adsharp/src/sharplet/State/CoreTests/CoreTests.swift:12:24: error: ambiguous operator declarations found for operator
        let three = 2 |> add(1)
                       ^
<unknown>:0: note: found this matching operator declaration
<unknown>:0: note: found this matching operator declaration

It's interesting that the compiler doesn't actually give a valid location the declarations it found.

So here's what I'd really like: by default, if you import State, you should get everything, including the operators. But if you end up with a conflict because of another library you're using, you should be able to import State.Core to get everything but the operators.