-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Infix syntax specification for functions/operators #2703
Comments
+1 to have I like the idea of and and or but I'm not very sure. || and && are not difficult to read... And way is and && and not & ? |
Please no We already have |
Yes, I prefer to steal as few names as possible, and on top of that having synonyms in the language is a style nightmare. Our operators are already just function names with special syntax, so in principle this could be extended to more names. But special syntax for the |
@stevengj, I understand your point on multiple syntaxes, though I think it's more of a problem when it involves operators/functions that are very similar but slightly different (think '=' and '->' in R). Where I originally proposed With julia> has([1,2,3,4,],3)
ERROR: no method has(Array{Int32,1},Int32)
julia> contains([1,2,3,4],3)
true
julia> contains(["one"=>1,"two"=>2,"three"=>3,"four"=>4],("three",3))
true With the latter two being the primary case. I think that solves the Dict problem because we would support dicts how contains support dicts, as a regular iterable that would only work on a tuple |
I think I see what @JeffBezanson was saying now though about potential confusion. julia> contains(["one"=>1,"two"=>2,"three"=>3,"four"=>4],3)
false
julia> contains(["one"=>1,"two"=>2,"three"=>3,"four"=>4],"three")
false Which means if someone tried to check |
See also |
I don't buy the ambiguity argument – we just need to decide what |
If we address #2347 , it makes sense to write |
Yes, that would be very nice. |
I like that. |
Essentially implemented in b6da170. Now there is the new question of whether it makes sense to have a shorthand for asking if something is not in a collection. To me, |
|
This is what you get for adding syntax :-[. Sorry, I think we're sticking with |
I'll get used to |
@JeffBezanson, shouldn't |
@stevengj |
Why not in for strings? |
See #4153 and #4184. I guess the idea was that
That said, I think |
After 143395e : julia> "ab" in "abc"
ERROR: use contains(x,y) for string containment
in in at string.jl:479 |
Fully agree that |
What is the rationale for making it an error rather than simply returning |
Because subsequence testing and set containment are not the same operation. Using the element-of operator for finding substrings is an abuse of notation I don't believe I've ever seen. |
Python does it. |
If every math and theory textbook is one day updated to use python's definition of set containment, I will concede the point. |
No argument, just pointing out where the idea came from. |
We aren't talking about the set-containment operator ∈, we are talking about the English word "in". |
My question is, why can't there be a function that simply checks whether a value is one of those generated by something iterable? This seems like a totally obvious primitive function to me. What should this function be called? |
We're not talking about the English word "in" we're talking about the Julia operator |
In #4184 , Stefan gave a nice reponse when I espoused a different point of view about allowing multiple semantic meanings for the same function. Of course, you'll never get away entirely from having multiple meanings for the same function, and you need to balance this against needless function proliferation, but I can respect the view that in languages with multiple dispatch like Julia, it makes life easier down the road if semantic overloading is minimized. In this case, if Anyway, I'm happy enough with |
I agree it can be ok to take an "informal" approach to the semantics of some functions; it's just that this one is so primitive. It's such an extreme case that you almost don't want it to be a generic function, and just iterate and check |
"anywhere you can say for x in y, x in y now also works and refers to the same meanings of everything" To me this is the best argument for the present syntax (and to not have string containment included in |
Yes, it's a very compelling argument. |
This is a follow up to a post on julia-dev about providing the ability to check containment similar to Python, Ruby, SQL, etc.
'4 in [1,2,3,4,5]' -> true
This is extremely intuitive syntax and nonconflicting with our current use of 'in' while still being conceptually similar.
And while I'm on Santa's lap and on the topic of syntax readability, can I request we use 'and' and 'or' as aliases for short circuit operators && and ||, a la python? I think they are keywordy enough that people avoid using them for variable names while doing much for clear, readable syntax.
Thoughts?
Update: Changed title to reflect the desire for generalized infix syntax
The text was updated successfully, but these errors were encountered: