Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 22:45
    Blacksmoke16 synchronize #12966
  • 12:51
    straight-shoota closed #12224
  • 12:51
    straight-shoota closed #13034
  • 12:15
    straight-shoota milestoned #12921
  • 12:15
    straight-shoota milestoned #12921
  • 10:44
    straight-shoota edited #12921
  • 02:39
    cyangle synchronize #12921
  • 01:08
    cyangle synchronize #12921
  • 01:08
    cyangle synchronize #12921
  • Feb 03 22:11
    straight-shoota labeled #12794
  • Feb 03 22:11
    straight-shoota assigned #12794
  • Feb 03 22:07
    straight-shoota synchronize #12959
  • Feb 03 21:38
    straight-shoota milestoned #12983
  • Feb 03 21:38
    straight-shoota milestoned #12983
  • Feb 03 17:37
    straight-shoota milestoned #13045
  • Feb 03 17:37
    straight-shoota milestoned #13045
  • Feb 03 16:49
    straight-shoota review_requested #13045
  • Feb 03 16:49
    straight-shoota review_requested #13045
  • Feb 03 16:49
    straight-shoota demilestoned #13045
  • Feb 03 16:49
    straight-shoota demilestoned #13045
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?
i.e. w/o needing a file
didactic-drunk
@didactic-drunk
This is an idea I'm playing with. Is it possible to translate "#{class}.{method}" to a fixed offset at compile time? Example:
@[Measure]
def foo
end

macro finished
  {% for meth in ... %}
    def {{meth...}}
      slot = {{ slot_for @type, meth.name }}
      Fiber.current.measurements[slot].measure do
        previous_def
      end
    end
  {% end %}
end
George Dietrich
@Blacksmoke16
so that the methods are created in a specific order?
Quinton Miller
@HertzDevil
however this index is the overload order not the declaration order
it's still unique though