Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with operators with "=" in them #1267

Closed
pao opened this issue Sep 8, 2012 · 7 comments
Closed

Problems with operators with "=" in them #1267

pao opened this issue Sep 8, 2012 · 7 comments

Comments

@pao
Copy link
Member

pao commented Sep 8, 2012

While trying to emulate Haskell syntax for the monad module I'm suddenly working on, I tried to define >>= as an alias for bind(f::Function, m::Monad). Julia lets me make the definition, but doesn't let me use it:

bind(f::Function, m::Monad) = join(fmap(f, m))
(>>=)(m::Monad, f::Function) = bind(f, m)
julia> ss = bind(get) do st 
           (value, newSt) = ("incr!", st+1)
           mthen(mreturn(State, value), put(newSt))
       end
State(#<function>)

julia> ss = get >>= st -> begin
           (value, newSt) = ("incr!", st+1)
           mthen(mreturn(State, value), put(newSt))
       end
no method >>(State,Function)
 in method_missing at base.jl:70

(entire WIP monad.jl: https://gist.github.com/3677645)

I get different errors if I try to simplify things:

julia> (>>=)(a, b) = a + b

julia> 1 >>= 2
syntax error: invalid assignment lvalue 1

julia> a = 1 >>= 2
syntax error: invalid assignment lvalue 1

Clearly the = in >>= is the source of confusion. Do we want to fix things so >>= can be legal infix, or ban = from infix operators?

@JeffBezanson
Copy link
Sponsor Member

It's just that a>>=b is converted to a=a>>b. You would have to call it using (>>=)(a,b).

@pao
Copy link
Member Author

pao commented Sep 8, 2012

Which defeats the purpose in this case--the point would be to make it usable infix. So this is an extension on +=, -=, etc.? Didn't know that operators did that automatically. I'm inclined to think you shouldn't be allowed to define an operator ending in =, then, since the behavior won't be intuitive? Not that people are too likely to run into this...

@pao
Copy link
Member Author

pao commented Sep 8, 2012

I can't define the fish operator >=> at all (syntax error). I'm coming to the opinion that = in operators is bad.

@JeffBezanson
Copy link
Sponsor Member

Very possible. The trouble is that >>= etc. exist as symbols in the parse tree, so you have to be able to write things like :(>>=). So I'm not sure at what point to raise the error.

@pao
Copy link
Member Author

pao commented Sep 10, 2012

Can it be picked up when the method body is bound to the name (I assume this happens at some point), or is inspecting the string representation of the symbol not possible at that point?

@JeffBezanson
Copy link
Sponsor Member

That can be done, but then it maybe also has to be disallowed on the left of an assignment?

@pao
Copy link
Member Author

pao commented Oct 6, 2012

Thanks! While we won't be able to implement a subset of Haskell with Julia macros, at least we'll be consistent about it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants