Defining a variable programmatically #45
Replies: 3 comments 4 replies
-
Not at the moment, but I can see how it could be useful. Lua has
Yes, this works, the intention is to allow things like:
and I didn't consider preventing using
That's not available at the moment. Currently IDs are always known at compile time, otherwise they're assumed to be globals, i.e. discoverable at runtime. We could have
Yes it's confusing,
I think given the current limitations of the language, I would just work with values needing to be accessed via a map, and then if the module name would be unergonomic in context then maybe I'd assign a new name for it?
|
Beta Was this translation helpful? Give feedback.
-
Thanks for finding time to answer my questions!
I'll describe my use case to let you better understand what I actually need. There will be unit generators (ugens). I'd like to make them a simple data type (instances of map), which you can use to connect them to each other and use arithmetic operations on them. Interconnected ugens could form a graph of another ugen, which you can use inside graphs of other ugens to define new ugens. There will be either the ugens, which are defined on the Csound side (i.e. bindings for ugens) or the ugens defined inside Koto. To define a ugen in Koto I need to get a name of it to define it on the Csound side. That could be a random name, but it's not good at scale, and I'd like to be able to easily interoperate with the engine, having an ability to reference a ugen (in Csound it's "opcode") at any point. It's better explained by code: # suppose we have a function to define a ugen and have some ugens already
# ugen_def inputs, outputs, graph
synth = ugen_def (k("freq"), k("add"), k("mul")),
(a("out"),
|params|
# sinosc is another ugen
l = sinosc.a params.freq, params.add, params.mul
r = sinosc.a params.freq + 10, params.add, params.mul
out l, r
# so now we have a ugen synth, which we can use in definitions of other
# ugens as we saw with sinosc above
synth2 = ugen_def (k("freq"), k("add"), k("mul")),
(a("out"),
|params|
out (synth.a params.freq * 0.5, params.add, params.mul)
# but here we don't know the name of the ugen we defined. The name is needed
# to define the corresponding ugen (i.e. opcode) in Csound.
# To solve this issue, we can modify API and do something like this:
ugen_def "synth",
(k("freq"), k("add"), k("mul")),
(a("out"),
|params|
# sinosc is another ugen
l = sinosc.a params.freq, params.add, params.mul
r = sinosc.a params.freq + 10, params.add, params.mul
out l, r
# We just pass the name to ugen definition. But the question now is how we access the
# new ugen. Perfectly that would be an automatically instantiated variable, so we could
# just start to use `synth` as we do with other ugens. Otherwise we need
# some store for ugens and import them, something like:
import ugen_store.synth
# But it's an additional step I'd like to avoid.
# Another option could be combining the first method with the second, but it's redundancy,
# which may not only confuse, but also lead to issues, because ugens can have different
# names inside the user's context and in the engine and we can't track it, as we don't know
# the name in the user's context. So the question is: how to add a variable programmatically? Can I, for example, do something like While I was writing it, I've gotten an idea. What if I share the compiler with the UPD it looks like I don't even need sharing the compiler and can just use I'm not sure about the context, though. Can I define a variable in (or import something into) the parent or global context this way? Or |
Beta Was this translation helpful? Give feedback.
-
@ales-tsurko I hope you don't mind, I've decided to open up Discussions, and figured this issue was a good candidate for a wider discussion. My thinking is that Discussions can be used for general brainstorming / looking for solutions to problems, which can then result in concrete Issues, which can then be worked on, so, Discussions -> Issues -> PRs. |
Beta Was this translation helpful? Give feedback.
-
Can I define a variable using a function? Like this, for example:
I tried to find a way to do this today surfing the source code. And here are my thoughts/questions on this so far:
self
/this
for current context? If I could add keys to the current context, the key would be the variable.import
in a wrong way:import
statement. Some kind of import all (from fake import *
orimport fake.*
) would work, by I didn't find how to do this either.Is there any other ways to do this? Or, may be, solutions for the issues I faced in the above points?
Beta Was this translation helpful? Give feedback.
All reactions