rubex icon indicating copy to clipboard operation
rubex copied to clipboard

FR - Infer type without declaration

Open arjunmenon opened this issue 7 years ago • 3 comments

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.

arjunmenon avatar Jan 11 '18 09:01 arjunmenon

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.

v0dro avatar Jan 11 '18 09:01 v0dro

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.

arjunmenon avatar Jan 11 '18 10:01 arjunmenon

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.

v0dro avatar Jan 11 '18 12:01 v0dro