Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 02:34
    Blacksmoke16 labeled #13023
  • 02:19
    cyangle labeled #13023
  • 02:19
    cyangle opened #13023
  • 00:41
    devnote-dev labeled #13022
  • 00:41
    devnote-dev opened #13022
  • Jan 27 23:05
    straight-shoota labeled #13021
  • Jan 27 23:05
    straight-shoota labeled #13021
  • Jan 27 23:05
    straight-shoota labeled #13021
  • Jan 27 23:05
    straight-shoota labeled #13021
  • Jan 27 23:05
    straight-shoota opened #13021
  • Jan 27 22:40
    straight-shoota labeled #13020
  • Jan 27 22:40
    straight-shoota labeled #13020
  • Jan 27 22:40
    straight-shoota labeled #13020
  • Jan 27 22:40
    straight-shoota opened #13020
  • Jan 27 21:18
    stefandd edited #13018
  • Jan 27 20:46
    etra0 review_requested #13006
  • Jan 27 18:30
    Blacksmoke16 closed #13007
  • Jan 27 18:30
    Blacksmoke16 closed #13019
  • Jan 27 18:21
    wyhaines closed #13019
  • Jan 27 16:56
    wyhaines labeled #13019
George Dietrich
@Blacksmoke16
but i just combined them into 1 method
Kirk Haines
@wyhaines
Yeah. You can't get away from storing the class/type of Proc or Struct somewhere, in some form.
From IRC (bridge bot)
@FromIRC
<Guest64> random question: is there a way for crystal to expand macros in other files, then move the expanded code into another file for use? if that makes any sense.
From IRC (bridge bot)
@FromIRC
<Guest64> i wish there was better docs for macros somewhere. it seems to have all these cool features but i cant make heads or tails of them
George Dietrich
@Blacksmoke16
you already asked this and we answered it twice :P
From IRC (bridge bot)
@FromIRC
<Guest64> ha, i suppose i did, my bad
George Dietrich
@Blacksmoke16
i will say that mutable constant approach is kinda a smell
im still of the opinion you dont even need a macro
From IRC (bridge bot)
@FromIRC
<Guest64> i guess the problem i am trying to solve is that i have all these function prototypes that vary radically, but the internal body consists mostly of the same method calls plus a bit of dynamic logic based on the function being called, hence the yeild in the macro. i think in the end i was just exploring different options so that i did not have to
<Guest64> write boilerplate 50+ times
Kirk Haines
@wyhaines

A macro generates code only in the location in which it is executed.

However.....you can make what you want to do work with macros.

Macros have access to constants.

So, if you generate all of your procs, assigned to a constant name, under a common namespace, then in your main.cr you can iterate over
that namespace (see Crystal::Maros::TypeNode in the Crystal API docs), find the names of all of your Proc constants, and build your master hash.

George Dietrich
@Blacksmoke16
i.e. in the previous example the logic that was like arg0 + arg1 is the stuff thats unique per proc? but there is also common logic between them all?
From IRC (bridge bot)
@FromIRC
<Guest64> yes corrent, a fair bit of common logic
<Guest64> im reading https://crystal-lang.org/api/1.0.0/Crystal/Macros/TypeNode.html atm like wyhaines suggested
Kirk Haines
@wyhaines
TypeNode just shows you what you have access to within a TypeNode. i.e. #constants, so you can iterate constants.
George Dietrich
@Blacksmoke16
Kirk Haines
@wyhaines
The fact that macros have access to constants means that you can use them to store and gather information from a wide variety of locations, and use it in a central location.
George Dietrich
@Blacksmoke16
auto registers proc types in the collection type, and shows how you can do before/after logic
can remove the #register method then too
From IRC (bridge bot)
@FromIRC
<Guest64> wait i am a bit confised here. you can use {% %} and {{ }} outside of a macro def block?
George Dietrich
@Blacksmoke16
yes, this is also macro code
macro some_name are reusable portions of macro code, but you can also use the macro code outside of them
the end result is the same. Code generated by the macro code is included where it's called as if you manually typed it
From IRC (bridge bot)
@FromIRC
<Guest64> okay, that is pretty cool now, im starting to understand this system a bit more
<Guest64> thanks for the aid
George Dietrich
@Blacksmoke16
Np
From IRC (bridge bot)
@FromIRC
<Guest64> wynhaines: just to circle back on what you said. if i define a proc with a constant such as MY_PROC = -> (){}, if i used {% Proc.constants %} I should be returned an array of all constants that contain a proc?
George Dietrich
@Blacksmoke16
no
From IRC (bridge bot)
@FromIRC
<Guest64> im just trying to understand the macros and typenode system as a whole now, outside of my project
George Dietrich
@Blacksmoke16
sec
George Dietrich
@Blacksmoke16
you're basically mutating the value of a constant at compile time
which you could then later iterate over in other macro code
George Dietrich
@Blacksmoke16
However at this point, it has less use cases with the introduction of annotations
From IRC (bridge bot)
@FromIRC
<Guest64> @Blacksmoke16: yea your example makes sense, but i was referring to when wynhaines said that you can use #constants to iterate constants
<Guest64> unless i mistoook what he was meaning
George Dietrich
@Blacksmoke16
ah that would be like
class Foo
  ONE = 1
  TWO = 2
end

{{pp Foo.constants}} # => [One, Two]
From IRC (bridge bot)
@FromIRC
<Guest64> hmm that gives me some ideas to test out. is there a way to access constants at the top level, or only within class/struct objects?
George Dietrich
@Blacksmoke16
That example is on the top level
Oh
I think that'll be possible in the next version, until then it's probably still a good practice to namespace your stuff
From IRC (bridge bot)
@FromIRC
<Guest64> true
<Guest64> https://www.bookstack.cn/read/crystal-lang/syntax_and_semantics-macros.md i found this and things are starting to click together now. thanks again
Kirk Haines
@wyhaines
@guest64 -- I can give you a little example of the kinds of thing that I am talking about in a bit.
Kirk Haines
@wyhaines

@guest64 --

module MyProcs
   Proc1 = ->(arg0 : Int32, arg1 : Int32) { arg0 + arg1 }
end

module MyProcs
  Proc2 = ->(arg0 : String, arg1 : String) { arg0.to_i64 + arg1.to_i64 }
end

macro build_a_hash
  {%
    foo = {} of Nil => Nil
    MyProcs.constants.each do |const|
      foo[const.stringify] = "MyProcs::#{const.id}".id
    end
  %}

MyHash = {{ foo.stringify.id }}
end

build_a_hash

pp MyHash
pp typeof(MyHash)

You are still going to have problems with types, though, if you have a single hash that holds everything.

Try it and you will see what I mean.

George Dietrich
@Blacksmoke16
can you build a binary from input via stdin or something?