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

? behaves strangely as an identifier #6286

Closed
joshkarges opened this issue Mar 27, 2014 · 15 comments
Closed

? behaves strangely as an identifier #6286

joshkarges opened this issue Mar 27, 2014 · 15 comments
Labels
parser Language parsing and surface syntax

Comments

@joshkarges
Copy link

I have a composite type which has a single Dict property that I thought would be interesting to access with an overloaded ? operator

type J9Type
    propDict::Dict
end
function ?(obj::J9Type,field::Union(Symbol,String))
    return get(obj.propDict,field,Nothing())
end
A = J9Type([:p1=>1,:p2=>2,:p3=>7])

I found that I could only use my overloaded ? operator if I mention it before hand, even with a comment. Otherwise it says that ? is not a generic function.

#?
?(A,:p1)
@JeffBezanson
Copy link
Member

It's not supposed to be possible to define methods for ?.

@JeffBezanson
Copy link
Member

Also note that ?x is special syntax in the REPL for getting help about x.

@JeffBezanson
Copy link
Member

I'm not able to reproduce any comment-related behavior in either master or 0.2. Could you provide some more details about what commit hash you're using (just paste the banner), and exactly how you're running the code --- what is in a file, what is typed at the REPL, etc. ?

@StefanKarpinski
Copy link
Member

? isn't an operator in Julia. There is a a ? b : c control flow construct and REPL inputs starting with ? are specially handled as help queries.

@joshkarges
Copy link
Author

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.0-prerelease+2154 (2014-03-24 13:35 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 96fb7e1* (3 days old master)
|__/                   |  x86_64-apple-darwin13.1.0

I ran it in both iJulia, and in the terminal julia. It fails in either case if there is no mention of ? before I call it. But it succeeds (though it's not supposed to?) at defining the function and calling it after i mention it with

julia> typeof(?); ?(A,:p1)
juila> methods(?); ?(A,:p1)
etc...

and it succeeds in iJulia if there's even a comment #? in the same cell as the call.

@joshkarges joshkarges changed the title Comments affects the dispatch of overloaded ? operator Comments affects the dispatch of overloaded ? function Mar 27, 2014
@StefanKarpinski
Copy link
Member

Those give me ? not defined which is probably not the right error message here.

@joshkarges
Copy link
Author

This is in iJulia
screen shot 2014-03-27 at 1 51 51 pm

@JeffBezanson
Copy link
Member

Well, uses of ? as an identifier really should be errors. Unfortunately I can't fix this due to the @unix? etc. macros, which abuse this feature. #whywecanthavenicethings

Maybe time to fix #4233?
cc @vtjnash

@StefanKarpinski
Copy link
Member

There was my suggested non-breaking fix of changing the parsing so that @unix? A : B parses as @unix ? A : B and simultaneously adding a zero-argument @unix macro (and others) that is true or false appropriately at compile time. Is there any issue with that approach?

@JeffBezanson
Copy link
Member

I think that will work.

@vtjnash
Copy link
Member

vtjnash commented Mar 27, 2014

No. We use it sometimes in places (like ccall) which don't allow conditionals

@JeffBezanson JeffBezanson changed the title Comments affects the dispatch of overloaded ? function ? behaves strangely as an identifier Mar 27, 2014
@StefanKarpinski
Copy link
Member

No. We use it sometimes in places (like ccall) which don't allow conditionals

That can be changed. The current @unix? A : B macros are terrible.

@StefanKarpinski
Copy link
Member

There are only two places in Base where that kind of thing is used in a macro:

base/stream.jl
881-
882:dup(x::RawFD) = RawFD(ccall((@windows? :_dup : :dup),Int32,(Int32,),x.fd))
883-dup(src::RawFD,target::RawFD) = systemerror("dup",-1==
884:    ccall((@windows? :_dup2 : :dup2),Int32,
885-    (Int32,Int32),src.fd,target.fd))

Not exactly earth-shattering to work around.

@jiahao
Copy link
Member

jiahao commented Jul 23, 2015

Ref: #1910

@quinnj
Copy link
Member

quinnj commented Jun 22, 2017

Closing as dupe of #22025 (even though this came first, the other issue seems to have more recent/productive discussion)

@quinnj quinnj closed this as completed Jun 22, 2017
carlobaldassi added a commit that referenced this issue Jul 8, 2017
carlobaldassi added a commit that referenced this issue Jul 10, 2017
carlobaldassi added a commit that referenced this issue Jul 16, 2017
carlobaldassi added a commit that referenced this issue Jul 16, 2017
JeffBezanson pushed a commit that referenced this issue Jul 17, 2017
* deprecate no space after the ? (no space before was already deprecated)

* deprecate no space around the :

* fix instances where no spaces were used in Base

Deprecate the use of `?` as an identifier

Ref #6286
jeffwong pushed a commit to jeffwong/julia that referenced this issue Jul 24, 2017
* deprecate no space after the ? (no space before was already deprecated)

* deprecate no space around the :

* fix instances where no spaces were used in Base

Deprecate the use of `?` as an identifier

Ref JuliaLang#6286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

6 participants