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

Add support for custom IntType, FloatType #111

Open
jvliwanag opened this issue Jul 30, 2022 · 4 comments
Open

Add support for custom IntType, FloatType #111

jvliwanag opened this issue Jul 30, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@jvliwanag
Copy link

Looking for support for custom IntType, FloatType within an evaluation. In particular, was hoping to us evalexpr with decimal.

It would be better to be able to provide these types as type parameters instead of having rust feature flags enforce the types at compile time. This way, one evaluation can use say, f64 float type, whereas another portion can use Decimal float type.

@ISibboI
Copy link
Owner

ISibboI commented Jul 30, 2022 via email

@NatanFreeman
Copy link
Contributor

NatanFreeman commented Sep 15, 2022

As discussed here default types for function traits are not yet possible. Unless there is another means of accomplishing this, adding traits to the existing eval functions would break compatibility. We would either have to create yet another class of eval functions that allow specifying types or just give up on backwards compatibility.

@ISibboI
Copy link
Owner

ISibboI commented May 27, 2023

As described in #122 (comment), a way to work around many issues with custom numeric types is to get rid of the current distinction between integers and floats. Instead, add just one generic number type, which can then distinguish between integers and floats internally.

The idea for implementation would roughly be:

  • Replace Value::Int and Value::Float with Value::Number and make Value generic over the number type.
  • Specify the number type via an associated type to the Context trait.
  • Add a NumberType trait, maybe make it compatible with num-traits somehow.
    • NumberType should require the relevant std::ops.
    • NumberType is responsible for parsing itself from a string.
  • Token would also be generic over the number type.

Open questions are:

  • What happens to builtin functions? If they are specified by the NumberType trait, then adding new builtin functions would become a breaking change each time. Possibly it is best to move them into a BuiltinFunctionsContext and then add composable contexts (Make contexts composable #81) for using builtin functions. Then the number type would only specify "basic" mathematical functions, and only if a new builtin function requires a not-yet-specified operation it would become a breaking change.
  • HashMapContext has a Value in one of its fields. Can this be made generic via the implementation of Context without giving HashMapContext a type parameter? Would a type parameter here be bad?
  • Making Value generic makes a lot of other types generic. Would it be better to instead have Value::Number contain a Box<dyn NumberType>?
  • Review existing code and see what else comes up.

@victorolinasc
Copy link

Not sure this is a proper proposal here to the problem but one way we could be "faster" to implement things here is to use some "syntax" to differentiate between types. One example of such a way is to use sigils. For example, in Elixir we can write something like: ~D[2024-01-01] and this is a proper Date struct.

The syntax is simply:

  • starts with ~
  • uses a discriminator (which can be a single letter or a stream of letters)
  • uses some separator which can be any of [], //, etc...
  • has the value between the separators
  • can have modifiers after the end of the separator (can be flags for the parser)

We could make this small syntax uplift and it would be easy to have both 0.1 as a float and something like ~f/0.2/ be a Decimal. We could then extend to ~$/R$ 4,32/ be a money amount and so on.

Would this be welcome as a contribution?

https://hexdocs.pm/elixir/sigils.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants