This will work: defguard to_boolean(term) when term not in [false, nil]
On Wed, 23 Dec 2020 15:02:43 -0500 Austin Ziegler <halosta...@gmail.com> wrote: > It would be easy enough to provide this as a predicate in your own > code if that’s your style preference > > ```elixir > def boolean(falsy) when falsy in [nil, false], do: false > def boolean(_), do; true > > # OR > def boolean(test), do: if(test, do: true, else: false) > > # OR > def boolean(test), do: !!test > ``` > > It can read a little more intentional to say `%{x | assigned?: > boolean(assigned_to_id)}` than `%{x | assigned?: !!assigned_to_id}` > or `%{x | assigned?: !is_nil(assigned_to_id)}`, but I don’t really > think this adds much value to the core. > > -a > > > > On Wed, Dec 23, 2020 at 2:01 PM José Valim <jose.va...@dashbit.co> > wrote: > > > I see. You can match on both or use a with in this case: > > > > if membership_ends_on || trial_ends_on || dues_exempt? do > > changeset > > else > > add_error(changeset, :membership_ends_on, "is required") > > end > > > > On Wed, Dec 23, 2020 at 7:50 PM Moxley Stratton > > <moxley.strat...@gmail.com> wrote: > > > >> José: > >> > >> I agree on your first example. > >> > >> Your second example wouldn't work: > >> > >> case membership_ends_on || trial_ends_on || dues_exempt do > >> nil -> add_error(changeset, :membership_ends_on, "is > >> required") _ -> changeset > >> end > >> > >> It wouldn't work because `dues_exempt` is a boolean. If > >> `dues_excempt` is `false`, it wouldn't match the `nil` clause. > >> > >> On Wed, Dec 23, 2020 at 10:39 AM José Valim <jose.va...@dashbit.co> > >> wrote: > >> > >>> Thank you for the examples. > >>> > >>> However, I believe those examples are not really checking for a > >>> boolean, but rather for nil. If you don't want to use is_nil, you > >>> can always compare with nil, which is also pipeable: > >>> > >>> def has_access? do > >>> at_least_logged_in? and get_csrf_cookie(session) != nil > >>> end > >>> > >>> %{event_form | submitted: event_form.submitted_at != nil} > >>> > >>> case membership_ends_on || trial_ends_on || dues_exempt do > >>> nil -> add_error(changeset, :membership_ends_on, "is > >>> required") _ -> changeset > >>> end > >>> > >>> > >>> On Wed, Dec 23, 2020 at 7:04 PM Bruce Tate <br...@grox.io> wrote: > >>> > >>>> I support this feature. The arguments for > >>>> > >>>> > >>>> - a solution that composes well with pipes and > >>>> - the linters rejecting the most common solution of !! > >>>> > >>>> > >>>> are all particularly good ones. From my perspective, I teach > >>>> composition with pipes in the core, `with` in the boundary, and > >>>> I try to have students return a boolean rather than a truthy > >>>> result as part of good API design. > >>>> > >>>> The inability to convert from truthy to boolean has always felt > >>>> like a missing feature to me. > >>>> > >>>> -bt > >>>> > >>>> On Wed, Dec 23, 2020 at 10:42 AM Andrea Leopardi > >>>> <an.leopa...@gmail.com> wrote: > >>>> > >>>>> What's the particular advantage of "to_boolean(term)" over "not > >>>>> is_nil(term)"? That it supports also booleans, which however > >>>>> could be used directly in predicates? > >>>>> > >>>>> Andrea > >>>>> > >>>>> On Wed, Dec 23, 2020 at 4:32 PM Moxley Stratton < > >>>>> moxley.strat...@gmail.com> wrote: > >>>>> > >>>>>> Here are examples: > >>>>>> > >>>>>> def has_access? do > >>>>>> at_least_logged_in? and !!get_csrf_cookie(session) > >>>>>> end > >>>>>> > >>>>>> %{event_form | submitted: !!event_form.submitted_at} > >>>>>> > >>>>>> case !!(membership_ends_on || trial_ends_on || > >>>>>> dues_exempt) do true -> changeset > >>>>>> false -> add_error(changeset, :membership_ends_on, "is > >>>>>> required") > >>>>>> end > >>>>>> > >>>>>> case !!previous_value and !!new_value do > >>>>>> true -> put_change(changeset, :unconfirmed_email, > >>>>>> new_value) false -> changeset > >>>>>> end > >>>>>> > >>>>>> submitted? = !!Ecto.Changeset.get_change(changeset, > >>>>>> :submitted_at) > >>>>>> > >>>>>> %{rsvpd: !!participation.rsvpd} > >>>>>> > >>>>>> On Tue, Dec 22, 2020 at 11:48 PM José Valim > >>>>>> <jose.va...@dashbit.co> wrote: > >>>>>> > >>>>>>> Can we revisit in which scenarios an explicit boolean > >>>>>>> conversion is necessary? > >>>>>>> > >>>>>>> On Wed, Dec 23, 2020 at 1:21 AM Moxley Stratton < > >>>>>>> moxley.strat...@gmail.com> wrote: > >>>>>>> > >>>>>>>> I would like revisit this issue. > >>>>>>>> > >>>>>>>> The issue was last discussed 5 years ago. Much has changed > >>>>>>>> in that time. The given solution was to use a > >>>>>>>> double-negative (!!) to convert a given value to a pure > >>>>>>>> boolean. > >>>>>>>> > >>>>>>>> Here are the problems with the suggested solution: > >>>>>>>> 1. I believe this solution is a hack that is not intuitive to > >>>>>>>> beginners. It's rarely seen by mid and senior developers, > >>>>>>>> meaning they have to pause to interpret what !! means. > >>>>>>>> 2. A double negative can't be part of an Elixir pipeline. > >>>>>>>> 3. The Credo package warns about double negatives: "Double > >>>>>>>> boolean negation found". Linters in other languages have a > >>>>>>>> similar warning. Other languages have a built-in function > >>>>>>>> (JavaScript has Boolean()) to convert to boolean. > >>>>>>>> 4. The alternative to double-negative is verbose: "if expr, > >>>>>>>> do: true, else: false", or the less readable "expr && true > >>>>>>>> || false" > >>>>>>>> > >>>>>>>> I would be happy to submit a PR to add Kernel.to_boolean/1. > >>>>>>>> > >>>>>>>> On Sunday, November 1, 2015 at 5:48:28 AM UTC-8 José Valim > >>>>>>>> wrote: > >>>>>>>> > >>>>>>>>> Not necessarily , but the ones in Kernel are. :) > >>>>>>>>> > >>>>>>>>> -- > >>>>>>>>> > >>>>>>>>> > >>>>>>>>> *José Valim* > >>>>>>>>> www.plataformatec.com.br > >>>>>>>>> Skype: jv.ptec > >>>>>>>>> Founder and Director of R&D > >>>>>>>>> > >>>>>>>> -- > >>>>>>>> You received this message because you are subscribed to the > >>>>>>>> Google Groups "elixir-lang-core" group. > >>>>>>>> To unsubscribe from this group and stop receiving emails > >>>>>>>> from it, send an email to > >>>>>>>> elixir-lang-core+unsubscr...@googlegroups.com. To view this > >>>>>>>> discussion on the web visit > >>>>>>>> https://groups.google.com/d/msgid/elixir-lang-core/45dc9a7b-1657-4d02-933b-fe8fe182c2bcn%40googlegroups.com > >>>>>>>> <https://groups.google.com/d/msgid/elixir-lang-core/45dc9a7b-1657-4d02-933b-fe8fe182c2bcn%40googlegroups.com?utm_medium=email&utm_source=footer> > >>>>>>>> . > >>>>>>>> > >>>>>>> -- > >>>>>>> You received this message because you are subscribed to a > >>>>>>> topic in the Google Groups "elixir-lang-core" group. > >>>>>>> To unsubscribe from this topic, visit > >>>>>>> https://groups.google.com/d/topic/elixir-lang-core/KKqjMbXoIJA/unsubscribe > >>>>>>> . > >>>>>>> To unsubscribe from this group and all its topics, send an > >>>>>>> email to elixir-lang-core+unsubscr...@googlegroups.com. > >>>>>>> To view this discussion on the web visit > >>>>>>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B7iTRj%3Do62adGM%3DspKxnscAT1QoVC-0uqLDS_YPcDGAA%40mail.gmail.com > >>>>>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B7iTRj%3Do62adGM%3DspKxnscAT1QoVC-0uqLDS_YPcDGAA%40mail.gmail.com?utm_medium=email&utm_source=footer> > >>>>>>> . > >>>>>>> > >>>>>> -- > >>>>>> You received this message because you are subscribed to the > >>>>>> Google Groups "elixir-lang-core" group. > >>>>>> To unsubscribe from this group and stop receiving emails from > >>>>>> it, send an email to > >>>>>> elixir-lang-core+unsubscr...@googlegroups.com. To view this > >>>>>> discussion on the web visit > >>>>>> https://groups.google.com/d/msgid/elixir-lang-core/CAFJDP5fJWCVCU41R0CQWx-q6RD1ENUtw92bcpSunf6SBNfQtsg%40mail.gmail.com > >>>>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAFJDP5fJWCVCU41R0CQWx-q6RD1ENUtw92bcpSunf6SBNfQtsg%40mail.gmail.com?utm_medium=email&utm_source=footer> > >>>>>> . > >>>>>> > >>>>> -- > >>>>> You received this message because you are subscribed to the > >>>>> Google Groups "elixir-lang-core" group. > >>>>> To unsubscribe from this group and stop receiving emails from > >>>>> it, send an email to > >>>>> elixir-lang-core+unsubscr...@googlegroups.com. To view this > >>>>> discussion on the web visit > >>>>> https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BLMgPAwYN8FyEs0t%3DVvmAQzP9gx-VjykT6Q11Dvd%3DsVyA%40mail.gmail.com > >>>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BLMgPAwYN8FyEs0t%3DVvmAQzP9gx-VjykT6Q11Dvd%3DsVyA%40mail.gmail.com?utm_medium=email&utm_source=footer> > >>>>> . > >>>>> > >>>> > >>>> > >>>> -- > >>>> > >>>> Regards, > >>>> Bruce Tate > >>>> CEO > >>>> > >>>> > >>>> <https://bowtie.mailbutler.io/tracking/hit/f8218219-d2a8-4de4-9fef-1cdde6e723f6/c7c97460-016e-45fb-a4ab-0a70318c7b97> > >>>> > >>>> Groxio, LLC. > >>>> 512.799.9366 > >>>> br...@grox.io > >>>> grox.io > >>>> > >>>> -- > >>>> You received this message because you are subscribed to the > >>>> Google Groups "elixir-lang-core" group. > >>>> To unsubscribe from this group and stop receiving emails from > >>>> it, send an email to > >>>> elixir-lang-core+unsubscr...@googlegroups.com. To view this > >>>> discussion on the web visit > >>>> https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-47q8nXAKw%2B8ZH_NHh2booBmEYtDf-dB70GHXPDXnZmXw%40mail.gmail.com > >>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-47q8nXAKw%2B8ZH_NHh2booBmEYtDf-dB70GHXPDXnZmXw%40mail.gmail.com?utm_medium=email&utm_source=footer> > >>>> . > >>>> > >>> -- > >>> You received this message because you are subscribed to a topic > >>> in the Google Groups "elixir-lang-core" group. > >>> To unsubscribe from this topic, visit > >>> https://groups.google.com/d/topic/elixir-lang-core/KKqjMbXoIJA/unsubscribe > >>> . > >>> To unsubscribe from this group and all its topics, send an email > >>> to elixir-lang-core+unsubscr...@googlegroups.com. > >>> To view this discussion on the web visit > >>> https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4J5P%2BdmuOB94xq1L9poz7t8_sjQMub3nYGwrzEYYHgqAQ%40mail.gmail.com > >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4J5P%2BdmuOB94xq1L9poz7t8_sjQMub3nYGwrzEYYHgqAQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > >>> . > >>> > >> -- > >> You received this message because you are subscribed to the Google > >> Groups "elixir-lang-core" group. > >> To unsubscribe from this group and stop receiving emails from it, > >> send an email to elixir-lang-core+unsubscr...@googlegroups.com. > >> To view this discussion on the web visit > >> https://groups.google.com/d/msgid/elixir-lang-core/CAFJDP5fA2A6Au1vWXVWB8WamCU8prEzUDwR22VXW7%2B%3D6xp7xqg%40mail.gmail.com > >> <https://groups.google.com/d/msgid/elixir-lang-core/CAFJDP5fA2A6Au1vWXVWB8WamCU8prEzUDwR22VXW7%2B%3D6xp7xqg%40mail.gmail.com?utm_medium=email&utm_source=footer> > >> . > >> > > -- > > You received this message because you are subscribed to the Google > > Groups "elixir-lang-core" group. > > To unsubscribe from this group and stop receiving emails from it, > > send an email to elixir-lang-core+unsubscr...@googlegroups.com. > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B0rS0Qb%3D1UG6Qa-MC1J8CndBXWkwbbs0kgftq8j_YeaQ%40mail.gmail.com > > <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B0rS0Qb%3D1UG6Qa-MC1J8CndBXWkwbbs0kgftq8j_YeaQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > > . > > > > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5fe6c0b1.1c69fb81.62fe1.f3beSMTPIN_ADDED_MISSING%40gmr-mx.google.com.