meaning of ? in a function?

172 views
Skip to first unread message

António Ramos

unread,
Aug 21, 2016, 1:37:12 PM8/21/16
to elixir-l...@googlegroups.com
Hello simple question for sure 

what is the meaning of the ? in 

def  aaa?()

regards
António

Peter Hamilton

unread,
Aug 21, 2016, 2:08:26 PM8/21/16
to elixir-l...@googlegroups.com

It has no meaning syntactically. It's just part of the function name. It is however a convention (especially common in Ruby) meaning "this function returns a boolean value".


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAEM0BxPu0w0pCpuNXzktOUUwpymxH7cyTTu3EgUePtKOX86vJA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Louis Pilfold

unread,
Aug 21, 2016, 3:09:45 PM8/21/16
to elixir-lang-talk
Hi

The convention is to end a function name with a ? if it returns a
boolean value. Otherwise it has no specific meaning.

Cheers,
Louis

António Ramos

unread,
Aug 21, 2016, 4:07:04 PM8/21/16
to elixir-l...@googlegroups.com
Got it!
thank you

2016-08-21 20:08 GMT+01:00 Louis Pilfold <lo...@lpil.uk>:
Hi

The convention is to end a function name with a ? if it returns a
boolean value. Otherwise it has no specific meaning.

Cheers,
Louis

On 21 August 2016 at 18:36, António Ramos <ramst...@gmail.com> wrote:
> Hello simple question for sure
>
> what is the meaning of the ? in
>
> def  aaa?()
>
> regards
> António
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAM-pwt7Cwi_LAvVWuk879ULfFLAs4USjdUEJxTMADEzW9Df%3DSA%40mail.gmail.com.

Elliot Crosby-McCullough

unread,
Aug 24, 2016, 9:24:53 AM8/24/16
to elixir-l...@googlegroups.com
Follow-up question; there's debate in Ruby as to whether methods ending in `?` are supposed to return actual booleans or just "truthy" and "falsy" values.

Is there a similar debate in Elixir or has it been more strictly defined?

On 21 August 2016 at 19:08, Peter Hamilton <petergh...@gmail.com> wrote:

It has no meaning syntactically. It's just part of the function name. It is however a convention (especially common in Ruby) meaning "this function returns a boolean value".

On Sun, Aug 21, 2016, 10:37 AM António Ramos <ramst...@gmail.com> wrote:
Hello simple question for sure 

what is the meaning of the ? in 

def  aaa?()

regards
António

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAOMhEnyyRKa-mi7roBci-MvcNiFGEwL4mzPOdTo96%3D0%3DK-h0eQ%40mail.gmail.com.

Michał Muskała

unread,
Aug 24, 2016, 9:28:58 AM8/24/16
to elixir-l...@googlegroups.com

> On 24 Aug 2016, at 15:24, Elliot Crosby-McCullough <elli...@gmail.com> wrote:
>
> Follow-up question; there's debate in Ruby as to whether methods ending in `?` are supposed to return actual booleans or just "truthy" and "falsy" values.
>
> Is there a similar debate in Elixir or has it been more strictly defined?

I've heard José state, that if it were possible, it would be an error for a ? function to return a non-boolean. I am equally of conviction that only strictly boolean values make sense in this context.

Michał.
signature.asc

Martin Svalin

unread,
Aug 24, 2016, 10:05:53 AM8/24/16
to elixir-l...@googlegroups.com
I think there's a stronger argument to be made for literal true false, not just truthy / falsy in Elixir than Ruby.

In Ruby, the return value might be used in an if-condition. Semantically, it shouldn't matter if the value is literal true or simply truthy. Something like `if method? == true` would be considered bad code.

In Elixir, you might also use the value in pattern matching. Matching on literal `true` would be considered completely reasonable. Example:

    def entrypoint(data), do: handle(data, valid?(data))

    def handle(data, true), do: something(data)
    def handle(data, false), do: something_else(data)

Returning truthy / falsy values would hobble this sort of pattern matching. It would be even worse if I only did a literal match on false, and ignored the in the true case, which would then be run for the falsy value nil.

Another consideration is that a truthy value could potentially be large, and could then be sent off in a message to another process in the expectation that it's small.

So please, return literal true / false from your predicate functions.

- Martin

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/DE691B14-076D-4FE1-B9B6-9E76FC7890A0%40muskala.eu.

Elliot Crosby-McCullough

unread,
Aug 30, 2016, 6:57:03 AM8/30/16
to elixir-l...@googlegroups.com
I'm in the literal camp even in Ruby, but it's good to see some less debateable reasoning for it in Elixir, thanks.

On 24 August 2016 at 15:05, Martin Svalin <martin...@gmail.com> wrote:
I think there's a stronger argument to be made for literal true false, not just truthy / falsy in Elixir than Ruby.

In Ruby, the return value might be used in an if-condition. Semantically, it shouldn't matter if the value is literal true or simply truthy. Something like `if method? == true` would be considered bad code.

In Elixir, you might also use the value in pattern matching. Matching on literal `true` would be considered completely reasonable. Example:

    def entrypoint(data), do: handle(data, valid?(data))

    def handle(data, true), do: something(data)
    def handle(data, false), do: something_else(data)

Returning truthy / falsy values would hobble this sort of pattern matching. It would be even worse if I only did a literal match on false, and ignored the in the true case, which would then be run for the falsy value nil.

Another consideration is that a truthy value could potentially be large, and could then be sent off in a message to another process in the expectation that it's small.

So please, return literal true / false from your predicate functions.

- Martin
ons 24 aug. 2016 kl 15:28 skrev Michał Muskała <mic...@muskala.eu>:

> On 24 Aug 2016, at 15:24, Elliot Crosby-McCullough <elli...@gmail.com> wrote:
>
> Follow-up question; there's debate in Ruby as to whether methods ending in `?` are supposed to return actual booleans or just "truthy" and "falsy" values.
>
> Is there a similar debate in Elixir or has it been more strictly defined?

I've heard José state, that if it were possible, it would be an error for a ? function to return a non-boolean. I am equally of conviction that only strictly boolean values make sense in this context.

Michał.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAAHw6C%2B2mL7zHOyVsFJ1UzSg9wVFrjcbc7sWGeEY%2B%3D9987nDpw%40mail.gmail.com.

webengi...@gmail.com

unread,
Aug 31, 2016, 4:39:21 PM8/31/16
to elixir-lang-talk
Reply all
Reply to author
Forward
0 new messages