Hello!
So, I was wondering if something like this would be a viable and sensible feature:
var a = @[]
a.add "hello"
echo a.type # seq[string]
I have been googling but can't find a language that does this.
OCaml's the only language I know where this level of inference is both possible and actually commonly relied on. Interactively:
# let x = [| |];;
val x : 'a array = [||]
# let x = Array.append x [|"of string"|];;
val x : string array = [|"of string"|]
# print_string x.(0);;
of string- : unit = ()
In Nim you have to be a little more explicit but the reader benefits as well as the compiler.
var x1 = newSeq[string]()
var x2: seq[string] = @[]
proc x3[T](v: T): seq[T] =
result.add v
echo (x1, x2, x3("a string")).typeof
nim is static typed compiled language
It has nothing to do with compiled/non-compiled lang, as it's possible to infer type from usage.
In OCaml a very useful part of your IDE (in Emacs) is a tool that asks the compiler to tell you what a type is - because the inference is so relied on that it can be hard to tell just from a function body, as neither the function nor the parameters typically have explicit types.
With machine learning doing type inference, you'll naturally need machine learning to also tell you what types things have, even more than in OCaml. A solution that needs itself as a solution.
I think we may expect the new generation of computer languages in the next couple of years, ...
Correct and already happening. Natural language is the new programming language.
... with ML support, things like add types to untyped code looks like easy for ML to solve.
There is so much more that you can do with ML. ;-)
We need banners and stickers! "Programmed in Nim! No AI involved!"
Nim - Premium, Hand Made Code!
Types are useful in some places and not so much in others.
Usually it's useful to define inputs and outputs for block of code. Like types for the signature and return of a function.
As for what's inside of that block of code - usually it's trivial to read it without types. Say you have function of 5-30 lines of code, do you really need types like i: int, etc. they contribute zero to readability or understanding.
I think we may expect the new generation of computer languages in the next couple of years, with ML support, things like add types to untyped code looks like easy for ML to solve.
Sounds like a great way to slow down compilation by several orders of magnitude and randomly introduce mysterious errors.
I think it conflicts with overloading. Consider
var s = @[]
s.add @[1, 2, 3]
If typeof(s) is seq[int], it will become @[1, 2, 3] after being added, but if it is seq[seq[int]], then it will became @[@[1, 2, 3]]. It happens because add is overloaded for either adding an element or adding a whole sequence. If s is a seq[seq[int]], then @[1, 2, 3] will be added as a single element, but if s is seq[seq[int]], then @[1, 2, 3] is viewed as a sequence to be concatenate with s. Therefore it's impossible to know which overloaded proc is invoked, without knowing the type of s.