-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Request for generic alias #2803
Comments
I think it's possible and shouldn't be that hard to do. |
Bump My use-case isn't for long names, but for something like: record Success(T), value : T
record Failure, message : String
alias Result(T) = Success(T) | Failure This mimic Functional Programming's Variants: // In F#
type Result<'a> =
| Success of 'a
| Failure of string |
+1, by the way, it's the 6th most voted feature in the Crystal issues, is there any timeline when it's planned to be implemented? |
Somewhere it was mentioned that #5155 is a blocker for this, but I never understand why. |
It's probably not. |
Let's sort out some of the formalities then. Should aliases that don't use all of their generic parameters be allowed? alias Foo(T) = Int32
alias Bar(X, Y) = Array(Y) Can generic aliases be used to reopen types? (probably not, even when the type variables match exactly) alias A(T) = Array(T)
class A; end
class A(T); end
alias H(U) = Hash(Int32, {U, U})
class H; end
class H(U); end
class H(K, V); end
# for reference, non-generic aliases to generic instances are not allowed
alias Foo = Array
class Foo; end # okay
alias Foo32 = Array(Int32)
class Foo32; end # Error: Foo32 is not a class, it's a generic class What happens when a generic alias instance does not resolve to a generic instance? alias Foo(T) = Array
class A
class X(U); end
end
alias Bar(T) = T::X
Bar(A) |
I don't think so. What purpose would that have?
I actually didn't know you could do that with non-generic aliases 🤔 Again, what would be the purpose?
I think that should be disallowed. I'd keep it as minimal as possible. We can allow advanced features later if a use case shows up. |
Bump, ran into a use case where this would have been perfect. |
Not sure what's the solution for this ? Or we must keep boilerplate everwhere, like Is something like this possible ? |
@revskill10 I'm not sure I follow what you mean by "boilerplate everywhere". ref: https://crystal-lang.org/reference/syntax_and_semantics/type_grammar.html#nilable |
In Crystal, we can alias a type like this:
And all
Foo
will be replaced withSomeGeneric(Int32)
.But we cannot alias as a generic type like this:
Can Crystal support this syntax for a generic alias?
It will be useful to avoid writing long type name.
The text was updated successfully, but these errors were encountered: