Unfortunately parametric types did not pass through new() correctly
resulting gibberish. Specifying the type explicitly works fine:
macro gentype(N, typename, fieldtype)
fields = [:($(symbol("I_$i"))::$(symbol("$fieldtype"))) for i=1:N
]
quote
immutable $(typename)
$(fields...)
end
end
end
Thanks,
Kaj
On Monday, March 9, 2015 at 6:05:10 PM UTC+2, Kaj Wiik wrote:
>
>
>
> This is more elegant Julian way that I tried to find, thanks!
>
> Kaj
>
>
> On Monday, March 9, 2015 at 1:43:14 PM UTC+2, Simon Danisch wrote:
>>
>> Hi,
>> I'm working on FixedSizeArrays, which does pretty much what you have
>> tried here.
>> Simplified version from constructors.jl
>> <https://github.com/SimonDanisch/FixedSizeArrays.jl/blob/master/src/constructors.jl>
>> :
>> macro gentype(N, typename)
>> fields = [:($(symbol("I_$i"))::T) for i=1:N]
>> quote
>> immutable $(typename){T}
>> $(fields...)
>> end
>> end
>> end
>>
>>
>> Am Donnerstag, 5. März 2015 11:02:51 UTC+1 schrieb Kaj Wiik:
>>>
>>> I have been trying to write a macro that would generate fields in a
>>> for-loop.
>>>
>>> However, when I try to generate the first line I get an error:
>>>
>>> julia> macro deftype(name,artype,num)
>>> ex1 = :(esc( immutable $name))
>>> ERROR: syntax: unexpected ")"
>>>
>>> julia> macro deftype(name,artype,num)
>>> ex1 = :(esc(quote immutable $name end))
>>> ERROR: syntax: extra token ")" after end of expression
>>>
>>>
>>> There must be a way to do this, I cannot find how...
>>>
>>> Thanks,
>>> Kaj
>>>
>>>