Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

no382001
Copy link
Contributor

related to #2431

we can store values in variables as in [expr x = $f1]. But something like [expr var($s1) = $f2] doesn't work, the object doesn't create and we get a 'expr: syntax error'

since (all of the?) functions in expr resolve to a concrete value, to solve this problem, i'd propose another function that returns only a variable reference to the value

so in the case of [expr function($s1) = $f2] where function is only syntactically correct in its current position, if a variable matches $s1 it would internally resolve to: first [expr a = 2.5] where a is the variable and 2.5 is the value of $f2, and then a would be bound to that variable.

the function implementation is identical to ex_var with the exception of variable binding, of course.

i'm open to suggestions.

@porres
Copy link
Contributor

porres commented Sep 23, 2024

the function implementation is identical to ex_var with the exception of variable binding, of course.

sorry, I didn't get the difference

@no382001
Copy link
Contributor Author

the internal logic is the same, it just does not resolve the variable by max_ex_var

pure-data/src/x_vexp_fun.c

Lines 1533 to 1542 in 1ed152a

switch(argv->ex_type) {
case ET_SYM:
if (argv->ex_flags & EX_F_TSYM)
/*
* SDY - we do not have the idx value here, so we are passing 0
* var() cannot evaluate the sys_idx variable
*/
max_ex_var(expr, gensym(argv->ex_ptr), optr, 0);
else
max_ex_var(expr, (t_symbol *) argv->ex_ptr, optr, 0);

        switch(argv->ex_type) {
                case ET_SYM:
                        if (argv->ex_flags & EX_F_TSYM)
                                optr->ex_ptr = gensym(argv->ex_ptr);
                        else
                                optr->ex_ptr = argv->ex_ptr;
                        return;

@porres
Copy link
Contributor

porres commented Sep 23, 2024

Well, sorry, I'm really confused. Are you saying you did create a new function named function?

Or is this just a PR that closes #2431 ? Meaning, if the PR is taken, can [expr var(a) = 2.5] work and set [v a] to '2.5' when taking a bang?

@umlaeute
Copy link
Contributor

umlaeute commented Sep 24, 2024

i'm not sure whether i groked function() either.

to assign values to symbols defined at run-time, how about using a value = assign(sym, value) function?
as in:

[list foo 10(
|
[expr assign($s1, $f2)]
|
|10\

@umlaeute
Copy link
Contributor

umlaeute commented Sep 24, 2024

i'm not sure whether i groked function() either.

after reading the code, i see that the problem is actually just one of bad wording in the description of this PR.
the newly introduced function is ref() (not function()), so the syntax would be:

[expr ref($s1) = $f2]

i still think an assign(name, value) function is easier to understand.

@porres
Copy link
Contributor

porres commented Sep 24, 2024

Assign makes more sense, or var could just take one or two arguments. If two, the second is assigned to the first

@no382001
Copy link
Contributor Author

sorry for not being clear, i wasnt decided on the name thats why i wrote 'function', but your 'assign' actually makes more sense! and is considerably less messy

@porres
Copy link
Contributor

porres commented Sep 24, 2024

assign makes more sense, but I would avoid creating a new function altogether, and I like my idea of having an option for 2 args var() better and I believe Shahrokh will prefer that (I'm emailing him about this PR).

@umlaeute
Copy link
Contributor

to be honest, i do not really see how var($s2, $f1) is really readable.
but anyhow, Shahrokh is the one to decide.

@Spacechild1
Copy link
Contributor

Spacechild1 commented Sep 24, 2024

What about set(name, value)? Should be pretty self-explaining. I'd find this a bit more concise than assign, but I'd also be ok with the latter.

@no382001
Copy link
Contributor Author

wouldn't preventing it from being callable before a = make the syntax less intuitive? if you can already assign a value to a variable like this: [expr x = 1], then calling set(name, value) to reassign a value might feel less intuitive.

@umlaeute
Copy link
Contributor

depends.
name = value is certainly intuitive.
however, i find ref(name) = value less intuitive than set(name, value).

(i also think that = assignments shouldn't have gone into expr in the first place; there's a reason that s-expressions don't use it)

@sebshader
Copy link
Contributor

sebshader commented Sep 25, 2024

What about set(name, value)? Should be pretty self-explaining. I'd find this a bit more concise than assign, but I'd also be ok with the latter.

there's no 'upvote' on github so.. +1 vote for set
though I have to say also, the string processing and accessing variables make sense to be part of [expr] but imo setting variables seems somewhat out of scope, given that there's no 'real' control flow anyways it would probably encourage unnecessarily opaque/verbose/confounding expr instances

at some point you may as well include a small c compiler or interpreter or something instead (or I suppose you could keep extending the expr 'language' to include control flow and local variables etc.)

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.

5 participants