Using Decimal in matches is tricky because two equal values can have different precision so that 42.0d != 42.00d. You can even have cases where 420d != 420d, because one has three digits of precision and the other only two digits.
On Wed, Feb 8, 2017 at 10:20 AM, José Valim <jose.va...@plataformatec.com.br > wrote: > Yes, you need to parse the decimal at compile time, something like this: > > defmacro sigil_X(decimal, _) do > Macro.escape Decimal.new decimal > end > > > > *José Valim* > www.plataformatec.com.br > Skype: jv.ptec > Founder and Director of R&D > > On Wed, Feb 8, 2017 at 8:39 AM, Wojtek Mach <woj...@wojtekmach.pl> wrote: > >> Yes that worked, thanks. >> >> For the record, at first I tried: >> >> defmacro sigil_X(str, _) do >> quote do >> Decimal.new(unquote(str)) >> end >> end >> >> and it failed but with a similar error: cannot invoke remote function >> Decimal.new/1 inside match >> >> However, when macro expands to struct (here, just hardcoding it to prove >> the point) >> >> defmacro sigil_X(_str, _) do >> quote do >> %Decimal{coef: 1, exp: 0, sign: 1} >> end >> end >> >> it works well. >> >> W dniu środa, 8 lutego 2017 03:11:31 UTC+1 użytkownik José Valim napisał: >>> >>> You should be able to use inside matches if you make it a macro. >>> >>> On Wed, Feb 8, 2017 at 02:28 Wojtek Mach <woj...@wojtekmach.pl> wrote: >>> >>>> (sorry for digging up old topic) >>>> >>>> I would be open though to adding support for number sigils like 13.0d, >>>>> 13.0f although we should also consider how numbers like rationals would >>>>> play into that. >>>>> >>>> >>>> Would the idea by that Elixir would allow doing a 13.0d sigil, and >>>> Decimal (whether in core or separate library) implements that sigil? >>>> >>>> I think another advantage of including Decimal in core is exactly the >>>> sigil. Then the inspect implementation could simply return "13.0d" instead >>>> of "#Decimal<13.0>" which would make it copy-pasteable in editor/iex (since >>>> it'd be a valid Elixir expression) which makes it extremely convenient for >>>> actually working with it, debugging code that uses it etc. Another >>>> advantage is using it is in match. >>>> >>>> Currently this isn't allowed: >>>> >>>> defmodule DecimalSigil do >>>> def sigil_X(str, _) do >>>> Decimal.new(str) >>>> end >>>> end >>>> >>>> import DecimalSigil >>>> ~X[3] = Decimal.add(~X[1], ~X[2]) >>>> >>>> cannot invoke remote function DecimalSigil.sigil_X/2 inside match >>>> >>>> # see: https://github.com/ericmj/decimal/issues/38 >>>> >>>> Looks like custom sigils can't be used in matches, but built-in can. >>>> >>>> -- >>>> 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-co...@googlegroups.com. >>>> To view this discussion on the web visit https://groups.google.com/d/ms >>>> gid/elixir-lang-core/e5d1fc3e-14d9-489e-8051-2f7a7d2e913e%40 >>>> googlegroups.com >>>> <https://groups.google.com/d/msgid/elixir-lang-core/e5d1fc3e-14d9-489e-8051-2f7a7d2e913e%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >>> >>> >>> *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/ms >> gid/elixir-lang-core/194126ed-1f85-4fd0-aef2-dfae7e17a082% >> 40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-core/194126ed-1f85-4fd0-aef2-dfae7e17a082%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > 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%2Bk7sJNPX%3DYH8jF0JdzfwEVbAeGuyqi2kximbt > 1KWrfOQ%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bk7sJNPX%3DYH8jF0JdzfwEVbAeGuyqi2kximbt1KWrfOQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- Eric Meadows-Jönsson -- 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/CAM_eapi7B3R52Vu94MzWQmFs1MqiRk2Rmkr2eRjQsk2nB9WX7Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.