digitalmars.D.learn - Function templates with more than one declaration?
- Brian Palmer (9/9) Apr 14 2008 I'm building a simple atom (symbol, whatever) library for a project, but...
- boyd (17/35) Apr 14 2008 Would a function template work in this case perhaps?
- Brian Palmer (14/24) Apr 14 2008 Thanks for the quick response, Boyd -- I probably should have explained ...
- Bill Baxter (6/14) Apr 14 2008 That's another oldie but a goodie. Been discussed before. At least it
I'm building a simple atom (symbol, whatever) library for a project, but I'm
not really satisfied with my syntax for static atom literals. My template:
template atom(string name) {
static Atom myAtom;
static this() { myAtom = Atom(name); }
Atom atom() { return myAtom; } /* Hopefully this will be inlined, haven't
tested that yet... */
}
usage:
Atom name = atom!("name").atom;
The ideal, in my mind, would be to create Atom literals with the same
Atom("name") syntax usable for creating Atoms at runtime. I'm pretty sure that
won't be possible until templates can override functions, though. But I'd at
least like to get rid of that last spurious `.atom` at the end of the
declaration. Has anybody run into this situation before? What's the reason for
only allowing "Implicit Template Properties" when the template declares exactly
one member, anyway?
Apr 14 2008
Would a function template work in this case perhaps?
Atom atom(string name)(){
static Atom myAtom;
myAtom =3D Atom(name);
return myAtom;
}
Cheers,
Boyd
-------
On Mon, 14 Apr 2008 19:59:46 +0200, Brian Palmer <d brian.codekitchen.ne=
t> =
wrote:
I'm building a simple atom (symbol, whatever) library for a project, b=
ut =
I'm not really satisfied with my syntax for static atom literals. My =
template:
template atom(string name) {
static Atom myAtom;
static this() { myAtom =3D Atom(name); }
Atom atom() { return myAtom; } /* Hopefully this will be inlined, =
haven't tested that yet... */
}
usage:
Atom name =3D atom!("name").atom;
The ideal, in my mind, would be to create Atom literals with the same =
=
Atom("name") syntax usable for creating Atoms at runtime. I'm pretty =
sure that won't be possible until templates can override functions, =
though. But I'd at least like to get rid of that last spurious `.atom`=
=
at the end of the declaration. Has anybody run into this situation =
before? What's the reason for only allowing "Implicit Template =
Properties" when the template declares exactly one member, anyway?
-- =
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Apr 14 2008
boyd Wrote:
Would a function template work in this case perhaps?
Atom atom(string name)(){
static Atom myAtom;
myAtom = Atom(name);
return myAtom;
}
Cheers,
Boyd
Thanks for the quick response, Boyd -- I probably should have explained what
Atoms do, and why I wanted that static constructor in there. Depending on how
the Atom is being used, your approach could case Atom(name) to be called each
time, instead of once during module initialization. For example:
void doSomething(string[atom] params) {
doSomethingElse(params[atom!("name")]);
}
would call Atom("name") during each call to doSomething(). For Atoms to improve
performance they need to be stored after the first lookup.
Sometimes it pays to step away from the computer for a minute though -- I
really just needed to use two templates where I was using one:
Atom atom(string name)() {
return _atom!(name).myAtom;
}
private template _atom(string name) {
static Atom myAtom;
static this() { myAtom = Atom(name); }
}
Apr 14 2008
Brian Palmer wrote:
The ideal, in my mind, would be to create Atom literals with the same
Atom("name") syntax usable for creating Atoms at runtime. I'm pretty
sure that won't be possible until templates can override functions,
though. But I'd at least like to get rid of that last spurious
`.atom` at the end of the declaration. Has anybody run into this
situation before? What's the reason for only allowing "Implicit
Template Properties" when the template declares exactly one member,
anyway?
That's another oldie but a goodie. Been discussed before. At least it
should be possible to have some extra private members inside those
"Implicit Template Property" templates. But right now there's no way to
express "private to the template".
--bb
Apr 14 2008









boyd <gaboonviper gmx.net> 