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
Allow generic-typed class methods/variables in abstract classes #8861
Comments
Generic types can't have generic class variables. I think there's a separate issue for that already. |
I couldn't find it either but essentially Todays current behavior is class Bar(T)
class_property x : Int32 = 0
end
Bar.x = 1
Bar(Int32).x = 2
Bar(String).x = 3
pp! Bar.x # => 3
pp! Bar(Int32).x # => 3
pp! Bar(String).x # => 3 Instead of pp! Bar.x # => 1
pp! Bar(Int32).x # => 2
pp! Bar(String).x # => 3 |
I think this is the same issue that I'm experiencing. I'm trying to create a generic class variable. Without GenericsWhat I'm trying to accomplish is to get around the fact that class variables do not persist between sub-classes. So let's say for example that I have an The solution is to use a singleton for the internal counter class EntityCounter
@@counter = 0
# class methods for managing counter...
end
class Entity
def dostuff
EntityCounter.increment
# do stuff
end
end Now the counter is the same for With GenericsHere is a more complex example where I want to create a pool of resources by type. Each resource in the pool has a reference counter (sort of like a file reference count), so resources can be garbage collected once they have no more references. I have several different types of resources that need different pools. Say for example a Here is what I'd like to do: class Reference(T)
# instance vars and methods for managing reference
end
class ReferencePool(T)
@@references = Hash(String, Reference(T)).new
# class methods for managing pool
end
class Texture
def addstuff
ReferencePool(Texture).add("texture-key", Texture.new)
end
def dostuff
ReferencePool(Texture).use("texture-key")
end
end
# etc... Assuming the above code worked, I could easily create new types with their own pool of references. However, the compiler just complains that it can't infer the type for |
I think it's a good use case. using System;
using System.Collections.Generic;
class C<T>
{
static Dictionary<String, T> stuff = new Dictionary<String, T>();
public static int foo() {
return stuff.Count;
}
}
class Application
{
static void Main()
{
C<int>.foo();
}
} |
I would also prefer if class variables are scoped to instantiated generic class. It is more powerful. If there is a need for shared class variables across all instances, there can be a helper non-generic class created for that end. But we might need to wait. I doubt it will change soon. |
I looked at the code and this might not be hard to implement... but I don't have the time right now. |
I ran into this limitation when attempting to define an abstract base class which accepted a generic type. While I somewhat understand the compiler needing to infer the type of the generic in order to generate appropriate code, however abstract classes should be treated as non-instantiatable code which sub-classes inherit from and implement the missing bits to make instantiatable. What struck me as odd about the error message, is it's coming from the scope of
Foo
(where the type of genericT
should be known), but notBase
(where the class variable@@foo
is defined via theclass_getter
macro and given the typeT?
).Example
Compiler Output
The text was updated successfully, but these errors were encountered: