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

Feature Request: Add dot notation operator support (attribute getter) #149

Open
sweihub opened this issue Aug 21, 2023 · 2 comments
Open

Comments

@sweihub
Copy link

sweihub commented Aug 21, 2023

Hi Mr. @ISibboI

I poke around the threads and find related issue: #117, this is similar to what I am looking for, I will make a pull request if you aggree with my proposal.

We would like to expand the dot notation to attribute getter operator, object.field will be redirected to a user defined function dot(object, "field", ()), the user is responsbible for implementing it, now we are able to mimic object in a simple way.

Expected usage

eval_with_context_mut("
    f = future("IC2312");
    bid = f.bid * 1.001;
    // call the method
    f.buy(30.5, 100);
    // return the prices
    (bid, f.ask, f.mid, f.last)
",
&mut context);

User defined dot functions will handle this:

  • future("IC2312")
  • dot(f, "buy", (30.5, 100))
  • dot(f, "ask", ())
  • dot(f, "bid", ()), ...

Kindly review and discuss, thanks!

@sweihub
Copy link
Author

sweihub commented Aug 27, 2023

@ISibboI Do you have one minute to review this?

@sweihub
Copy link
Author

sweihub commented Aug 27, 2023

I've implemented this, please review the PR: #153, thanks!

#[test]
fn test_dot_attribute() {
    let mut context = HashMapContext::new();
    // omitted, check the PR
    ...
    assert_eq!(
        eval_with_context_mut(
            "v = array(1,2,3,4,5); 
             v = v.push(6); 
             v.length == v.get(5)",
            &mut context
        ),
        Ok(Value::Boolean(true))
    );
}

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

No branches or pull requests

1 participant