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

in some cases we could get more from type declarations #271

Closed
JeffBezanson opened this issue Dec 1, 2011 · 10 comments
Closed

in some cases we could get more from type declarations #271

JeffBezanson opened this issue Dec 1, 2011 · 10 comments
Assignees
Labels
performance Must go faster

Comments

@JeffBezanson
Copy link
Sponsor Member

Currently type declarations are implemented with convert. The problem is, in a case like this

x = convert(Array{Float64,2}, ????)

there are so many matching convert methods that we can't infer the result type. We could either cheat and assume convert always returns something of the right type or, probably better, add a layer of typeassert to make sure convert returns the right thing in this case.

@ghost ghost assigned JeffBezanson Dec 1, 2011
@StefanKarpinski
Copy link
Sponsor Member

I've often thought about having the ability to declare the type of a function as a whole. Something along the lines of

convert :: {T}(Type{T}, Any, ...) --> T

Not sure about the syntax, of course, but the idea is that this is a generic way of saying that the convert function always takes two or more arguments, the first of which must be a Type value, the rest of which may be anything, and that it always returns a value the type specified by its first argument. Other examples would be much simpler than this.

This would also potentially serve to declare a generic function before declaring any particular methods of it, which would occasionally be handy.

@ViralBShah
Copy link
Member

Do we have plans to do anything here?

@StefanKarpinski
Copy link
Sponsor Member

The latter suggestion (mine) is already a separate issue. I'm not sure about convert, although I'd be willing to require that convert always return an object of the demanded type.

@ViralBShah
Copy link
Member

It seems reasonable to me that convert should return an object of the demanded type. If it doesn't, we should get a nice error though.

@StefanKarpinski
Copy link
Sponsor Member

I'm not sure how we would go about inserting a type check. If there was a type check, then the inference system would automatically know the type, but the only way I can think to make that work is to have a do_convert function that we add methods to and have convert call it and then check the type. But that's an extra layer I really wouldn't want to have. I'd say it's not worth it unless there's evidence that many convert calls are not getting precisely inferred result types.

@JeffBezanson
Copy link
Sponsor Member Author

For declaring function types, we need 2 things: declaring the return type of one method, and of the whole generic function.

@StefanKarpinski
Copy link
Sponsor Member

Ah, yes. If we could write something like this:

convert::(T::Type,x)-->T

then we could express this for the entire convert function. Would be a generally useful feature, but seems like a lot of work to get right. Also reintroduces function types, which is a questionable complication at this point.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 1, 2013

can we close this in favor of the more descriptive issue #1090?

@JeffBezanson
Copy link
Sponsor Member Author

No, these are different issues.

@simonster
Copy link
Member

It would also be great to have this for length (always an Int AFAIK as long as there's no BigRange) and Array(T, x) (always an Array{T}, although the number of dimensions depends on whether or not x is a tuple).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

No branches or pull requests

5 participants