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

Infer more attribute types #2614

Merged
merged 5 commits into from Feb 16, 2018
Merged

Infer more attribute types #2614

merged 5 commits into from Feb 16, 2018

Conversation

xymus
Copy link
Contributor

@xymus xymus commented Feb 15, 2018

Extend the detection of the static type of attributes from their literal values to support three new cases:

  • Simple arrays like [0, 1, 2] and [new Set[Int], new Set[Int]]. However, it does not accept arrays with an explicit type because we can't subtype/anchor at that point, as far as I know.

  • Negative integers and floats. This cheats a bit as the return type of the unary - is defined in the core libary. However this should help 99.9% of the time, in particular for Nit beginners, and a workaround is to declare the attribute static type when defining a different kernel library.

  • The once keyword.

class A
       # Now detected
       var i = -1
       var f = -1.0
       var a = [0, 1]
       var o = once [0, 1]

       # These are refused
       var a1 = [0, 1.0, "a"] # Different types
       var a2 = [0, 1: Int] # Can't reliably check subtypes
       var a4 = [1+1] # Expression
       var o1 = once [0, "a"] # Forwarded error
end

You may want to review commit by commit as the first commit is a small refactoring.

Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
@xymus xymus changed the title Detect more types for attributes Infer more attribute types Feb 15, 2018
privat added a commit that referenced this pull request Feb 16, 2018
Extend the detection of the static type of attributes from their literal values to support three new cases:

* Simple arrays like `[0, 1, 2]` and `[new Set[Int], new Set[Int]]`. However, it does not accept arrays with an explicit type because we can't subtype/anchor at that point, as far as I know.

* Negative integers and floats. This cheats a bit as the return type of the unary - is defined in the core libary. However this should help 99.9% of the time, in particular for Nit beginners, and a workaround is to declare the attribute static type when defining a different kernel library.

* The `once` keyword.

~~~
class A
       # Now detected
       var i = -1
       var f = -1.0
       var a = [0, 1]
       var o = once [0, 1]

       # These are refused
       var a1 = [0, 1.0, "a"] # Different types
       var a2 = [0, 1: Int] # Can't reliably check subtypes
       var a4 = [1+1] # Expression
       var o1 = once [0, "a"] # Forwarded error
end
~~~

---

You may want to review commit by commit as the first commit is a small refactoring.

Pull-Request: #2614
@privat privat merged commit 72741c1 into nitlang:master Feb 16, 2018
@xymus xymus deleted the infer-attr-types branch February 17, 2018 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants