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

proposal: Go 2: Allow question marks in function names #26111

Closed
sblackstone opened this issue Jun 28, 2018 · 18 comments
Closed

proposal: Go 2: Allow question marks in function names #26111

sblackstone opened this issue Jun 28, 2018 · 18 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@sblackstone
Copy link

This comes from Ruby and its a real readability helper for functions that simply return booleans, e.g.

type OptionContract struct {
  Right string
}

func (oc *OptionContract) put?() bool {
  return oc.Right == 'P'
}

var (
  oc OptionContract
)

oc.right = 'P'

oc.put?() // True
@gopherbot gopherbot added this to the Proposal milestone Jun 28, 2018
@ALTree ALTree added the v2 A language change or incompatible library change label Jun 28, 2018
@ALTree
Copy link
Member

ALTree commented Jun 28, 2018

It's not much better than the "Is-" idiom - e.g. IsPrime - (in fact, personally I prefer the Is idiom over the ? idiom) and at the same time this would require complicating the rules for valid variable names (that right now are quite simple). Not sure it's worth it.

@ianlancetaylor
Copy link
Contributor

If we adopt this, then why not ¿Poner? ? As @ALTree says, the current rules are simple.

@as
Copy link
Contributor

as commented Jun 29, 2018

The put? is very confusing, but isPut doesn't help either.

@sblackstone
Copy link
Author

sblackstone commented Jul 2, 2018

Regardless of whether you like the idiom or hate the idiom, whats so special about the question mark?

If...

func 世 int() {
	return 1
}

compiles because that character is part of UTF8, then why not the poor lowly ?

It's also a readability thing - when your eyes are scanning a line you get to find out whats being looked at before learning its a boolean.

e.g. (ruby)

if option.call? && option.long? // Do something particular.

@davecheney
Copy link
Contributor

davecheney commented Jul 2, 2018 via email

@sblackstone
Copy link
Author

sblackstone commented Jul 2, 2018

func helloɁ() {
	fmt.Println("Hello!")
}

Compiles... :-)

@davecheney
Copy link
Contributor

davecheney commented Jul 2, 2018 via email

@sblackstone
Copy link
Author

Why is "letterhood" or "punctuationhood" relevant?

The language doesn't include ternary expressions so its unlikely to be used in other contexts outside regex..

@Azareal
Copy link

Azareal commented Jul 2, 2018

If I recall, some of the ideas for generics or similar use question marks.
There were also some ideas for error handling which tossed question marks in.

Those might not make it into the language, but it's always good to keep the door open.
Plus, if question marks are valid, then I don't see why every other pieces of punctuation shouldn't be (not that it's practical to do so), otherwise you have to keep track of which ones are legal and which are not.

@davecheney
Copy link
Contributor

Plus, if question marks are valid, then I don't see why every other pieces of punctuation shouldn't be (not that it's practical to do so), otherwise you have to keep track of which ones are legal and which are not.

x, y := package.a,b,c

what is x? It is the function package.a,b or is it the function package.a and y is the local variable b,c.

@ianlancetaylor
Copy link
Contributor

@sblackstone Whether a character is a letter or punctuation is relevant because that is what the spec says today. From https://golang.org/ref/spec#Identifiers: "An identifier is a sequence of one or more letters and digits. The first character in an identifier must be a letter." Letter is defined at https://golang.org/ref/spec#Letters_and_digits as anything that Unicode considers to be a letter, plus the underscore character.

@sblackstone
Copy link
Author

@ianlancetaylor I understand thats what the current spec says but thats why this is a proposal and not a bug report.

@ianlancetaylor
Copy link
Contributor

Ah, sorry for misunderstanding.

In that case I would say that whether a character is a letter or punctuation is relevant because we want to keep the spec simple. Why should we add an exception for ? ? As I tried to get at with my Spanish example above, there are many similar punctuation characters. Once we start treating some punctuation characters as valid identifier characters, where do we stop? What principle do we follow? I think this is also what @Azareal was getting at above.

@DeedleFake
Copy link

DeedleFake commented Jul 2, 2018

Well, you could do what WDTE does and allow absolutely anything that isn't ambiguous with keywords and symbols.

I'm just kidding. Don't do that. Please.

Quite frankly, I think that allowing non-ASCII characters was a bit of a mistake in some way, though a minor one, as any use of, for example, Kanji in a function name in a library is going to be an absolute pain for anyone trying to use it that isn't used to typing those characters. The English alphabet is pretty well standardized in terms of ability to type it, though, so the reverse is not true. Thankfully, I've yet to see anyone do that, but the fact that #5763 exists means that someone at least had an interest, if, perhaps, only for internal use.

That being said, I'm actually not against making exceptions for ? and !, exceptions that would be much simpler to specify with an ASCII base for allowed characters. I used Ruby for a while and, once I got used to it, I do have to agree that the ? and ! conventions for 'query' and mutating methods, respectively, were quite nice. Go technically already has a weakly enforced form of the latter, at least in terms of methods on struct types, via pointer of non-pointer receivers, but I wouldn't be against a more official and cleaner way of specifying the classes of behavior a function is designed for like that.

I don't know what I'm talking about here. Just kind of brainstorming out loud.

@bcmills
Copy link
Contributor

bcmills commented Aug 2, 2018

I've always liked that Go uses punctuation for operators and letters for identifiers. (The one exception is the underscore character, which is in the “Punctuation, connector” category.)

I could envision allowing more things in identifiers — such as combining marks (with suitable normalization), letterlike symbols, and perhaps emoji — but I feel pretty strongly that we should reserve the non-connector Punctuation and mathematical Symbols for (possible future) operators.

Otherwise, where do we draw the line⸮

@g13013
Copy link

g13013 commented Sep 17, 2018

Even thought the ? character can be replaced by Is prefix, the special ! one has a very interesting meaning, especially that Go does not allow immutability yet

// be careful pointerToVal will be changed
func SomFunc!(pointerToVal *Type) {

}

@bcmills
Copy link
Contributor

bcmills commented Sep 17, 2018

@g13013

the special ! one has a very interesting meaning

It has an interesting meaning in certain programming languages (particularly those derived from Lisp), but it does not imply mutation in Go today, nor in English prose. That makes it much less obvious — especially to newcomers — than possible alternatives (such as a Set prefix).

In contrast, ? at least implies a question in prose.

@ianlancetaylor
Copy link
Contributor

We aren't going to do this. We aren't going to make an exception for one specific punctuation character. The current identifier rule is simple and there is no need to make it more complex.

@golang golang locked and limited conversation to collaborators Oct 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

10 participants