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

typealias Maybe{T} Union(T,Nothing) #1134

Closed
StefanKarpinski opened this issue Aug 8, 2012 · 9 comments
Closed

typealias Maybe{T} Union(T,Nothing) #1134

StefanKarpinski opened this issue Aug 8, 2012 · 9 comments
Assignees
Labels
status:won't change Indicates that work won't continue on an issue or pull request

Comments

@StefanKarpinski
Copy link
Sponsor Member

Can this be made to work? Currently it's an error:

julia> typealias Maybe{T} Union(T,Nothing)
type error: typealias: expected Type{T<:Top}, got TypeVar
 in anonymous at no file
@ghost ghost assigned JeffBezanson Aug 8, 2012
@JeffBezanson
Copy link
Sponsor Member

This has various problems. If Maybe{T} is used as an argument type, nothing can't match it because we don't know what T is. Or, we could let T be Nothing in that case, but then the maybe isn't doing anything for you.

These kinds of unions are ambiguous. Matching Foo{S} against Union(T,Foo{T}) could mean T is Foo{S} or S.

One problem is that we use these "type constructors" also as types; you expect to say Maybe and have it mean anything that could come from one of those. In this case it is equivalent to Any. If we didn't allow this, and insisted that these be applied to actual types before they could be used, then we wouldn't have these problems, but then you also couldn't use them in argument patterns. They would be equivalent to functions that return types.

@StefanKarpinski
Copy link
Sponsor Member Author

Got it. I'll just close.

@pao
Copy link
Member

pao commented Aug 8, 2012

Fortunately, I'm not sure that the Maybe type is all that useful in Julia. You could always add a method for the nothing case:

# like Maybe{Int}
foo(x::Int) = x^2
foo(x::Nothing) = NaN

I can think of a few things Maybe should be able to do that this can't, though. Was there a motivating case?

@JeffBezanson
Copy link
Sponsor Member

Sometimes it is useful to define a Nil{T} singleton, as for lists.

@pao
Copy link
Member

pao commented Aug 8, 2012

You have that pattern in list.jl without the assistance of a Maybe-like type.

@StefanKarpinski
Copy link
Sponsor Member Author

Yeah, this wasn't for a particular reason. I'm content not to have this — as Jeff points out, it doesn't really work for us.

@andrewcooke
Copy link
Contributor

for people coming from googling "julia maybe type" - there's now Nullable which seems to be similar http://julia.readthedocs.org/en/latest/manual/types/#nullable-types-representing-missing-values

@link2xt
Copy link

link2xt commented Oct 27, 2017

@nalimilan
Copy link
Member

Update to the update: #22682 and #23642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

6 participants