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

Request for generic alias #2803

Open
david50407 opened this issue Jun 11, 2016 · 10 comments
Open

Request for generic alias #2803

david50407 opened this issue Jun 11, 2016 · 10 comments
Labels
help wanted This issue is generally accepted and needs someone to pick it up kind:feature topic:compiler topic:lang:type-system

Comments

@david50407
Copy link
Contributor

In Crystal, we can alias a type like this:

alias Foo = SomeGeneric(Int32)

And all Foo will be replaced with SomeGeneric(Int32).

But we cannot alias as a generic type like this:

alias Bar(T) = TwoTypesGeneric(String, T)

Can Crystal support this syntax for a generic alias?
It will be useful to avoid writing long type name.

@asterite
Copy link
Member

I think it's possible and shouldn't be that hard to do.

@bew
Copy link
Contributor

bew commented May 25, 2017

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 

@straight-shoota straight-shoota added status:someday help wanted This issue is generally accepted and needs someone to pick it up and removed status:someday labels Dec 3, 2020
@al6x
Copy link

al6x commented Aug 9, 2021

+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?

@HertzDevil
Copy link
Contributor

Somewhere it was mentioned that #5155 is a blocker for this, but I never understand why.

@asterite
Copy link
Member

It's probably not.

@HertzDevil
Copy link
Contributor

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)

@straight-shoota
Copy link
Member

Should aliases that don't use all of their generic parameters be allowed?

I don't think so. What purpose would that have?

Can generic aliases be used to reopen types?

I actually didn't know you could do that with non-generic aliases 🤔 Again, what would be the purpose?

What happens when a generic alias instance does not resolve to a generic instance?

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.

@Blacksmoke16
Copy link
Member

Bump, ran into a use case where this would have been perfect.

@revskill10
Copy link

Not sure what's the solution for this ? Or we must keep boilerplate everwhere, like A | Nil ?

Is something like this possible ? alias Nilable(A) = A | Nil ?

@Blacksmoke16
Copy link
Member

@revskill10 I'm not sure I follow what you mean by "boilerplate everywhere". A | Nil can be expressed as A?. Plus even with this feature Nilable(A) would be more characters than A | Nil on its own...

ref: https://crystal-lang.org/reference/syntax_and_semantics/type_grammar.html#nilable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted This issue is generally accepted and needs someone to pick it up kind:feature topic:compiler topic:lang:type-system
Projects
None yet
Development

No branches or pull requests

8 participants