Open Discipol opened 1 year ago
I think you might have an incomplete understanding of nameof
, because it's actually the most convenient option here.
nameof
does not look at whatever object is passed into it; it looks at whatever symbol is given and replaces it with a string constant at compile time. So nameof(TileIndex)
will always resolve to "TileIndex"
; nameof
exists to simplify refactoring and searching. This also works for parameters, fields, properties, local variables...like I said, it operates on names.
Since nameof
doesn't look at the underlying object, it can't be used to derive a name from this
, therefore nameof(this)
is a compiler error.
If you're dead-set on not using nameof
, you could do one of the following...
"TileIndex"
to the base constructor instead of nameof(TileIndex)
. But then as soon as you rename TileIndex
, you'll have to find each use of TileIndex
in string literals. From experience, this is more annoying than writing out nameof(...)
.this
, then get the class name at runtime with this.GetType().Name
. But that adds unnecessary runtime overhead when starting the game.The best thing you can do is exactly what you're doing now. See here for more justification.
I think you might have an incomplete understanding of
nameof
, because it's actually the most convenient option here.
nameof
does not look at whatever object is passed into it; it looks at whatever symbol is given and replaces it with a string constant at compile time. Sonameof(TileIndex)
will always resolve to"TileIndex"
;nameof
exists to simplify refactoring and searching. This also works for parameters, fields, properties, local variables...like I said, it operates on names.Since
nameof
doesn't look at the underlying object, it can't be used to derive a name fromthis
, thereforenameof(this)
is a compiler error.If you're dead-set on not using
nameof
, you could do one of the following...
- Pass
"TileIndex"
to the base constructor instead ofnameof(TileIndex)
. But then as soon as you renameTileIndex
, you'll have to find each use ofTileIndex
in string literals. From experience, this is more annoying than writing outnameof(...)
.- (Ask Simon to) implement a base class constructor that does accept
this
, then get the class name at runtime withthis.GetType().Name
. But that adds unnecessary runtime overhead when starting the game.The best thing you can do is exactly what you're doing now. See here for more justification.
Thank you for the reply. This issue came from me copy/pasting an index file and forgetting to change the nameof(TileIndex). My goal was to minimize the amount of copy/paste/renamings.
I did want the super class to do GetType().Name. I wonder if I can create my own superclass and use a generic type. SO MANY THINGS TO PLAY WITH!
I find it very comfortable to put the name of the class as the index name, but I'd rather not HAVE to write nameOf. My request is having 1 or more super constructor overloads to omit that parameter and just do nameof(this) inside of it.