tre icon indicating copy to clipboard operation
tre copied to clipboard

feature: First class functions

Open zegl opened this issue 7 years ago • 5 comments

  • [x] Assign a func to a variable
  • [x] Execute func from variable
  • [ ] Clojures (variables outside the func are available inside the func)

zegl avatar Feb 06 '18 08:02 zegl

Variables outside the func are available inside the func

Do you mean that closures are currently not supported? I also noticed that you cannot assign a module-scoped variable either; the compilation ends with a nill pointer exception.

prologic avatar Nov 15 '19 01:11 prologic

Do you mean that closures are currently not supported?

Yes, exactly. The compilation of the following program will fail:

package main

import "external"

func main() {
	foo := 100
	fn := func() {
		external.Printf("foo = %d\n", foo) // error: use of undefined value '%foo-1'
	}
	fn()
}

zegl avatar Nov 15 '19 08:11 zegl

Got it! Any plans to implement Closures soon? :) I'd have a crack at it myself but I'm still learning the in/outs of LLVM :/ I certainl yknow how to implement Closures as I've done in monkey-lang and mio-lang

prologic avatar Nov 15 '19 08:11 prologic

I'd really like to implement closures one day, I've never implemented it in a language, so it should be a fun challenge! Inspiration to work on tre usually comes back when the winter arrives in the northern hemisphere, so I'll probably start hacking on this project again soon.

zegl avatar Nov 15 '19 09:11 zegl

That timing might work out in that case :) I won't put up a PR to give you a chance to hack and learn! I need to go learn LLVM anyway so I'm using your project tre as a basis to learn from (tinygo is a bit much of a codebase to learn from!)

prologic avatar Nov 15 '19 09:11 prologic