-1

I am trying to create a empty tuple inside macro but getting error - Error: undefined macro method 'TypeNode#new'


{% empty_tuple = Tuple.new %}

Here is link to crystal play - https://play.crystal-lang.org/#/r/8mxf

If i can't do this way, please tell me if there is any alternative

Update 1

I am trying to initialize a class - cases are some class constructor has arguments and some has not

So i thought of using tuple with splat for argument passing.

Here is crystal play link of what i am trying to do - https://play.crystal-lang.org/#/r/8n4g

Update 2

You can create a tuple using direct syntax

args = {1}

but this won't work in case you want to create empty tuple because hash syntax is similar & compiler throws error for providing type for hash declaration.

So i thought about using constructor syntax - Tuple.new

but it didn't work either.

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32
  • Sounds like you're abusing macros a bit, I can't fathom a legit usecase for this. Anyways, just using a constant outside macro land should work and be possible to reference in macro land. – Jonne Haß Feb 27 '20 at 09:10
  • @JonneHaß i am working on big project, so i need to use macro at multiple place to get things done. Could you please provide me a crystal play link of what you are saying ? – Ujjwal Kumar Gupta Feb 27 '20 at 11:11
  • Why do you need this? – asterite Feb 28 '20 at 00:13
  • @asterite i am trying to create an instance of class & using splat for args pass to constructor. I am able to solve this problem using conditional check but initially i thought about this way. – Ujjwal Kumar Gupta Feb 28 '20 at 02:39
  • 1
    Can't you create the empty tuple outside of macro code? Why do you need it in macro code? – asterite Feb 28 '20 at 11:14

1 Answers1

0

I was trying to create instance of many classes using macro & that's why i chooses this approach.

As i was unable to create empty tuple in macro. I came up with a hack - what if i get args of some method which does not have any args which means it will be empty tuple right ?

but it was not, it was an empty array

And again i was stucked. So i used

if else

to solve this problem. Basically check if class needs to be initiated with args, then create a class & pass arguments in constructor else initiate class without any args.

Hope this helps someone.

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32