Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 03:01
    Blacksmoke16 synchronize #11126
  • 01:04
    postmodern edited #11518
  • 01:03
    postmodern edited #11518
  • 00:56
    postmodern labeled #11518
  • 00:56
    postmodern opened #11518
  • Nov 30 23:58
    caspiano opened #11517
  • Nov 30 23:14
    HertzDevil labeled #11516
  • Nov 30 23:14
    HertzDevil labeled #11516
  • Nov 30 23:14
    HertzDevil labeled #11516
  • Nov 30 23:14
    HertzDevil opened #11516
  • Nov 30 23:12
    HertzDevil synchronize #11496
  • Nov 30 22:28
    straight-shoota synchronize #11512
  • Nov 30 22:18
    HertzDevil synchronize #11496
  • Nov 30 21:44
    straight-shoota opened #11515
  • Nov 30 21:44
    straight-shoota labeled #11515
  • Nov 30 21:39
    straight-shoota labeled #11514
  • Nov 30 21:39
    straight-shoota opened #11514
  • Nov 30 21:36
    HertzDevil synchronize #11298
  • Nov 30 21:26
    straight-shoota labeled #11513
  • Nov 30 21:24
    straight-shoota milestoned #11513
mfiano
@mjfiano:matrix.org
[m]
Usually generating a temporary variable that is guaranteed to be unique is the way to solve this, which I see Crystal can do
George Dietrich
@Blacksmoke16
mhm, those can also be keyed like a hash
which can be quite helpful, when you also bring unique indexes into things
mfiano
@mjfiano:matrix.org
[m]
Maybe your head wont explode reading this, but starts out with the same (bad) example: https://malisper.me/once-only/
But as you can see, the once-only macro definition is rather complex to solve such a common issue
Was just wondering if Crystal stood on the shoulder of the giants that have solved it decades ago and included such a thing in the language :)
George Dietrich
@Blacksmoke16
hmm
you can deff run into this as you just saw, like if you did {{x}} * {{x}} but it's also fairly easy to fix
macro square(v)
  %v1 = {{v}}
  %v1 * %v1
end
macros are deff a more advanced concept, they also aren't as type safe and have quite a few quirks :P
mfiano
@mjfiano:matrix.org
[m]
Oh sure, the solution has an easy fix, but:
  • It's not always obvious that there is a problem to solve.
  • An automated solution (a macro) is fairly complicated.
  • It saves a lot of boilerplate (over time)
mfiano
@mjfiano:matrix.org
[m]
Yeah, macros are advanced and come with a lot of footguns that take years of experience to even realize their existence, especially in Lisp, which has a much more powerful macro system. I will refrain from asking more macro questions until I have had time to see how they map over :)
George Dietrich
@Blacksmoke16
mfiano
@mjfiano:matrix.org
[m]
Oh that issue surprised me...
In Lisp a macro can evaluate at compile time arbitrary pieces of code, even methods that are shared with other macros or runtime code.
I did not know Crystal couldn't do that
George Dietrich
@Blacksmoke16
its a pita
because of that missing piece that you cant define reusable macro code
i.e. that you could use in two separate macros
From IRC (bridge bot)
@FromIRC
<riza> ^
George Dietrich
@Blacksmoke16
you can call a macro from a macro, but that just is moreso nested expansion, which doens't help in this case
From IRC (bridge bot)
@FromIRC
<riza> @Blacksmoke16 what do you mean fresh variables can be "keyed like a hash"
From IRC (bridge bot)
@FromIRC
<riza> hm, I'm not instantly sure how that would be useful, but it's neat to see regardless
George Dietrich
@Blacksmoke16
main use case afaik is if you want to create them in a loop
otherwise if you just did %key, it would be overridden on each iteration
mfiano
@mjfiano:matrix.org
[m]
I see. Also I really like Crystal and I don't mean to sound like a Lisp snob, because there are plenty of them. Lisp is nice, but it's too dynamic for some tasks...which is one thing that makes its macro system powerful. Every phase of the compiler can be invoked by any other phase by the user, which means macros can be expanded at runtime and all that. It's a mind!@#$, really.
From IRC (bridge bot)
@FromIRC
<riza> Yeah I suppose that could be a useful way to replace https://github.com/mosquito-cr/mosquito/blob/master/src/mosquito/base.cr#L10-L12
<riza> or several places where the finished macro is used
George Dietrich
@Blacksmoke16
use an annotation and iterate over types that have it
then could build a hash literal at build time or whatever you need
or what's that method doing actually?
cant just build a hash of Mosquito::Job.class => Mosquito::Job?
From IRC (bridge bot)
@FromIRC
<riza> Mosquito::Job.class.to_s => Mosquito::Job.class
George Dietrich
@Blacksmoke16
it's been a while since i been in the code, what's this do again?
From IRC (bridge bot)
@FromIRC
<riza> when a job is popped out of redis, it allows the runner to do a lookup in that hash to convert the job name in redis to a reference to the job class
George Dietrich
@Blacksmoke16
can you just use the FQN of the job class as the key?
oh wait
this is so that you can go from a string from redis to an actual type
should feature request Foo::Bar.class.from_json %("Foo::Bar") :P
mfiano
@mjfiano:matrix.org
[m]
Anyway, I have to try to erase my complicated Lisp macro memory to learn the basics of Crystal, else I'm going to be violating DRY a lot in this practice project.
George Dietrich
@Blacksmoke16
most of the time you dont need them either
mfiano
@mjfiano:matrix.org
[m]
(which I hope to evolve into more than a practice project as the basis for what I want to use Crystal for)
The alternative is I just DRY without care and figure out if a macro will make it more maintainable later when I'm more accustomed.
🤷‍♂️
From IRC (bridge bot)
@FromIRC
<riza> my honest advice is to avoid macros where possible (in both lisp and crystal). they're an amazing toolset when you need it but most problems can be solved elegantly without.
<riza> over-macro-ing makes code hard to maintain
mfiano
@mjfiano:matrix.org
[m]
I am torn. While I do agree, what has kept Lisp a viable language since the 50's is that new language features and are just a macro away. There's only so much you can do without creating new compiler extensions (macros).
From IRC (bridge bot)
@FromIRC
<riza> Can these fresh variable hashes be iterated, or do you have to keep the list of keys around
<riza> keep/pass