Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create constructor overload for PrimaryEntityIndex and EntityIndex without name #1042

Open
Discipol opened this issue Oct 11, 2022 · 3 comments

Comments

@Discipol
Copy link

[CustomEntityIndex(typeof(GameContext))]
public sealed class TileIndex : PrimaryEntityIndex<GameEntity, uint>
{
	public TileIndex(GameContext context) : base( nameof(TileIndex), etc.
}

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.

@JesseTG
Copy link

JesseTG commented Oct 11, 2022

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...

  • Pass "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(...).
  • (Ask Simon to) implement a base class constructor that does accept 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.

@Discipol
Copy link
Author

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...

  • Pass "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(...).
  • (Ask Simon to) implement a base class constructor that does accept 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.

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!

@JesseTG
Copy link

JesseTG commented Oct 14, 2022

Your editor probably has a file templates feature that'll let you fill out a form or something. I use Rider, so I use the feature described here. This is exactly what file templates are for!

@sschmid sschmid added this to Entitas Jul 2, 2023
@github-project-automation github-project-automation bot moved this to Todo in Entitas Jul 2, 2023
@sschmid sschmid removed the status in Entitas Jul 2, 2023
@sschmid sschmid moved this to New in Entitas Jul 3, 2023
@sschmid sschmid removed the status in Entitas Jul 3, 2023
@sschmid sschmid moved this to New in Entitas Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: New
Development

No branches or pull requests

2 participants