Names with trailing question mark

bearophile bearophileHUGS at lycos.com
Tue Oct 4 01:28:08 PDT 2011


Jacob Carlborg:

> > if (foo.even()?)
> 
> Why would you put the question mark after the parentheses. At least in 
> Ruby the question mark is part of the method name. This looks better:
> 
> if (foo.even?())

It's a typo of mine, sorry :-) The question mark is part of the name, so it is of course before the ().


> > I think this too gets accepted:
> > string customerName = user.loggedIn??user.name:"Who are you?";
> 
> This is not accepted in Ruby. You need a space after the second question 
> mark. If this would be implemented I would vote for requiring a space 
> after the first question mark as well.

In D the space after the second question mark is not required, and I think you can't change this rule unless you want to change lot of already written D code. So I think this is not a good idea.

Regarding the idea of requiring a space after the first question mark, it is doable, even if a bit unusual. I have mixed feelings about it. Maybe other people will express what they think about it.


> What about allowing method names like Scala does, containing arbitrary 
> symbols.
> 
> void ::: (int a) {}
> 
> I assume that will complicate the language quite a lot.

This is a change far bigger than the one I have suggested (that in most cases is limited to allowing one specific extra trailing symbol in names of functions/methods). Yours is an interesting idea, but it introduces complexities and opportunities that are larger and very different from the topic of this thread. So if you want to discuss your idea, its pro and cons, I suggest you to do it in a different new thread.


I have also thought about the idea of accepting the "?" as leading symbol only for function/method names that return a single boolean value. This sounds cute (because it makes this design more focused), but D has templates so there is also code like:

T foo?(T)(T x) { return x; }

I guess it's not a good idea to statically enforce T to be a boolean...

Bye and thank you,
bearophile


More information about the Digitalmars-d mailing list