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

Introduced a built-in deref function specifically designed to recursively dereference any provided value. #506

Closed
wants to merge 3 commits into from

Conversation

ckganesan
Copy link
Contributor

This PR introduces the deref function, designed to efficiently dereference given values across multiple levels of pointers, enhancing value accessibility in our expr.

@antonmedv
Copy link
Member

Can you show a use case? Right now we have bytecode OpDeref which is automatically added and does exactly the same thing.

@ckganesan
Copy link
Contributor Author

Can you show a use case? Right now we have bytecode OpDeref which is automatically added and does exactly the same thing.

The OpDeref bytecode is exclusively supported for operators, and it does not extend to other elements like functions or chain nodes. Please find the below example

	a := "Hello"
	ptrA := &a
	ptrPtrA := &ptrA

	code := `lower(ptrA)`
	env := map[string]interface{}{
		"ptrA":    ptrA,
		"ptrPtrA": ptrPtrA,
	}

	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}

	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}
	fmt.Print(output)
panic: cannot use *string as argument (type string) to call lower  (1:7)
 | lower(ptrA)
 | ......^

After changing the above code like below

	code := `lower(deref(ptrA))` // output 'hello'
        code := `deref(ptrPtrA) | lower()` // output 'hello'
        code := `len(deref(ptrPtrA))` // output 5

This function seamlessly handles single or multiple pointers by dereferencing the value regardless of its pointer depth.

@ckganesan
Copy link
Contributor Author

The deref function provides flexibility for users familiar with their expression environment objects, allowing them to selectively invoke dereferencing only when necessary. This approach optimizes performance by avoiding unnecessary dereferencing across all operators, regardless of whether a pointer is present.

@antonmedv
Copy link
Member

What about adding OpDeref opcode for arguments if needed? This way your example with string will work.

I was thinking about adding deref() myself, but decided what it will be easier for inexperienced users to do all the dereference via opcodes.

@ckganesan
Copy link
Contributor Author

What about adding OpDeref opcode for arguments if needed? This way your example with string will work.

I was thinking about adding deref() myself, but decided what it will be easier for inexperienced users to do all the dereference via opcodes.

Got it, i will update as per your comments

@ckganesan ckganesan closed this Jan 9, 2024
@ckganesan ckganesan deleted the builtin-deref branch January 26, 2024 13:59
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

Successfully merging this pull request may close these issues.

2 participants