-
Notifications
You must be signed in to change notification settings - Fork 263
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
A HasAttribute<T> function in DomNodeAdapter.cs #15
Comments
Hello, JelleVM, Good question. You're talking about the DomNodeAdapter's
So, your code would change from:
to:
Right? We could add another convenience method to DomNodeAdapter, to save the client from having to call (and discover) the DomNode.IsAttributeDefault() method. I don't think I would call this convenience method "HasAttribute" though, because conceptually, a DomNode "has" all the attributes that are defined for it. --Ron |
Hi Ron, thanks for your reply. Thanks for pointing me to the What would you do in the following scenario: your DomNode has an
int m_someVariable;
if (HasAttribute(assets.SomeType.myOptionalTypeAttribute))
m_someVariable = GetAttribute(assets.SomeType.myOptionalTypeAttribute);
else
m_someVariable = ComputeValueOfOptionalAttribute(); // heavy computation
Now the Could maybe
public bool HasAttribute(AttributeInfo attributeInfo) /\* or "IsAttributeNull" or "HasOptionalAttribute" */
{
return GetLocalAttribute(attributeInfo) == null;
}
|
Hi JelleVM, I see the problem; thank you for carefully explaining it. I think you're right, that this would be a useful method. Client code can call GetLocalAttribute() and check the result, but that's not very readable. I need to think about the name for a bit, write unit tests, and ask coworkers before I check this in. (I'm in Japan for a conference this week, announcing that LevelEditor is open source!) The problem I see with
--Ron |
Hi Ron, Thanks for considering it! It's indeed quite an uncommon user case, but that's why I brought it up. |
I'll check this new method on DomNode shortly. Thanks again for the good suggestion!
|
I have a Dom Node that has optional attributes, like:
I would like to be able to check if my node has this attribute at some given time. However, the
GetAttribute<T>
function returnsdefault(T)
when the attribute doesn't exist. Personally I'd just return null, but I guess that's a design decision. In any case, I think aHasAttribute<T>
function would be a worthwhile addition to the framework.I just implemented it as follows in my classes inheriting it:
The text was updated successfully, but these errors were encountered: