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

FR - Infer type without declaration #36

Open
arjunmenon opened this issue Jan 11, 2018 · 3 comments
Open

FR - Infer type without declaration #36

arjunmenon opened this issue Jan 11, 2018 · 3 comments

Comments

@arjunmenon
Copy link
Contributor

Hey
Just wanted to mention if it is feasible to modify the AST to infer variable type from the value it is set?

Currently, in C specific examples I was exploring, variables are declared.
Recently was looking into Mirah and their that is not required even if calling Java methods. It is more ruby like. It properly compiles to classes, without any penalty.

@v0dro
Copy link
Member

v0dro commented Jan 11, 2018

So the reason for not having type inference is that variables without a type specified as inferred as ruby objects and all operations performed on them delegate to the Ruby interpreter. For example:

i = 1
i = i + 1

will translate to:

VALUE i = INT2FIX(1);
ruby_interpreter_add_method(i, 1);

If we infer the type as a C type there will be no scope for assigning different objects to the same variable, which is possible in Ruby.

@arjunmenon
Copy link
Contributor Author

Hey

If we infer the type as a C type there will be no scope for assigning different objects to the same variable, which is possible in Ruby.

I was not referring to that.
If you loook at this example C function file, the variable a is specifically declared as int.
For syntactic sugar, may be it is possible to infer a as int from the value itself.
Since, we have already declared first_c_function takes two int, any variable using its params can be assumed to be int as well, unless explicitly type casted.

I am no expert on AST or how you have actually implemented the Type System. Just a thought.
This old project does the same, but in a different manner.

@v0dro
Copy link
Member

v0dro commented Jan 11, 2018

You have a point. But the system you are proposing is quite advanced given the stage of development of Rubex. In order to implement what you suggest it will be necessary to perform two passes of the code for type inference: one pass that understands how every variable is used and the second pass that will infer the type based on the usage of variables whose types are unspecified.

As of now I want to focus on building basic features that will make Rubex a go-to tool for writing Ruby C extensions and CUDA programming in Ruby.

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

No branches or pull requests

2 participants