Fork me on GitHub
#clojure
<
2024-05-08
>
vemv00:05:31

Is there, perchance, a defrecord alternative with optional fields? e.g I can construct it with fewer fields, or I can dissoc fields from it, and it remains being of instance of the underlying class (and not a vanilla map)

hiredman00:05:33

Because defrecord basis fields are actually object fields, and those cannot be removed from an object, you would need a new field to indicate if a given field has been dissoc'ed or not

vemv00:05:33

> Because defrecord basis fields are actually object fields, and those cannot be removed from an object, Yes, this aspect was making me hesitate. But perhaps there's some cool implementation out there.

hiredman00:05:26

I think maybe you could get away with some kind of sentinel value at best (nil is out, of course)

šŸ‘ 1
phronmophobic00:05:24

defrecord allows you to assoc on additional fields. You can do:

(defrecord MyFoo [])
If a field is optional, just leave it out. Associng and dissocing unspecified fields will still return a Foo.
> (-> (->MyFoo)
          (assoc :foo 42)
          (dissoc :foo)
          type)

user.MyFoo

vemv00:05:56

brilliant, thank you šŸ™‚ It's no issue to leave optional fields out, since there's some Malli validation going on anyway

Hendrik14:05:57

Is there a repl friendly way to set and change env vars?

andy.fingerhut14:05:21

You mean like bash environment variables? A child process cannot change the values of environment variables in a parent process. You can pass an environment of variable settings you wish to a new child process you create.

andy.fingerhut14:05:02

Some programs, e.g. ā€˜ssh-agent -sā€™, have as their standard output a bash script that the calling bash can do ā€˜sourceā€™ on.

Hendrik14:05:40

yes I mean environment variables.

andy.fingerhut14:05:15

But that thing with ssh-agent and similar programs only actually changes the calling bash process's environment variable values if you explicitly invoke them in a way that the calling bash uses source somehow, e.g. source ssh-agent -s``

dpsutton14:05:40

you can introduce a shim that is amenable to that. grab the env and throw it in a map, and then an api that lets you set overrides and add to it. If you donā€™t reach out to the environment but this api you can ā€œmutateā€ the environment

dpsutton14:05:51

thereā€™s https://github.com/weavejester/environ and i think we have a few (alter-var-root #'env/env assoc :mb-db-type "h2") to achieve this in a bit hacky way but in tests and in some off code

Hendrik14:05:52

thanks for the links. To give you a bit more background. I use aero for configuration. Aero can reference env vars. However, I doubt that it will work with environ .

vemv15:05:29

Most times what one really wants is using JVM system properties instead They play great with tools.deps/Lein aliases, with raw java , with property files, can be set in runtime, etc

lukasz16:05:07

@U023LKF2PQV with aero you usually do not have to overwrite env vars - default values for env/envf readers or merging dev-time configs works better in my experience

didibus16:05:08

It's not possible basically. The child process cannot change the parent shell environment vars.