@Tim is it not recommended because of the use of eval to change the global
state?
On Monday, October 5, 2015 at 12:39:48 PM UTC-7, Tim Holy wrote:
>
> On Monday, October 05, 2015 07:19:48 PM Jameson Nash wrote:
> > in short: you can't.
>
> In long: you can :-), but it's not recommended. There may be exactly one
> case
> in which this happens in all of Base julia, and that was from before we
> had
> fast tuples (yet there are still perhaps reasons to keep it). So you
> should be
> really, really sure you want to do this.
>
> As always, you can figure out how this works by looking at the typical
> expressions, e.g.,
>
> ex = :(immutable Foo <: GenType end)
>
>
> Here's a demo:
>
> julia> module MyModule
>
> abstract GenType
>
> let counter = 0
> global foo
> function foo()
> eval(MyModule, Expr(:type, false, Expr(:<:, symbol("Foo",
> counter+=1), :GenType), :()))
> end
> end
>
> export foo
>
> end
> WARNING: replacing module MyModule
> MyModule
>
> julia> MyModule.Foo1
> ERROR: UndefVarError: Foo1 not defined
>
> julia> MyModule.foo()
>
> julia> MyModule.Foo1
> MyModule.Foo1
>
> julia> MyModule.Foo2
> ERROR: UndefVarError: Foo2 not defined
>
> julia> MyModule.foo()
>
> julia> MyModule.Foo2
> MyModule.Foo2
>
> --Tim
>
>
>
>
> >
> > alternatives include using a Tuple or using a Dict
> >
> > On Mon, Oct 5, 2015 at 2:53 PM Damien <[email protected] <javascript:>>
> wrote:
> > > Hi all,
> > >
> > > I'm writing code in which I generate a composite type and various
> methods
> > > based on the fields of another type.
> > >
> > > I can do this at the expressions level with macros, but can I also
> > > generate the new type definition in a plain function using
> introspection?
> > >
> > > Best
>
>