some inconsistencies with function-like grammar

unless i kept the language very simple, there are bound to be some inconsistencies in the syntax of oh-lang. there are some minor ones between declaring function-like things and calling them.

here are the different ways to declare function-like things:
  • my_fn(args...): #return_instance is declaring a standard function that returns an instance of type #return_instance
  • m[args...]: #return_instance is for declaring a subscript access overload in a class body, which will return an instance of #return_instance†
  • fn[generic_args...](args...): #return_instance is a generic function
  • #my_type(args...): #my_type is for declaring a constructor, which returns an instance of #my_type. usually this is seen as #m(args...): #m inside a class body, since #m is shorthand for the class type.
  • #my_type[generic_args...]: #another_type is for declaring a class, which does not return an instance of #another_type but uses it for a typedef
  • #my_type[generic_args...](args...): #my_type[generic_args...] is for declaring a generic constructor, which returns a specified instance of #my_type
here are the different ways to call function-like things:
  • my_fn(args...) is calling a standard function that will return an instance of something (possibly null)
  • some_var[args...] is for calling a subscript access overload and will return an instance
  • fn[generic_args...](args...) is calling a generic function, which will return an instance
  • #my_type(args...) is for calling a constructor, which returns an instance of #my_type (or technically could be a result type)
  • #my_type[generic_args...] is for specifying a generic typedef, which does not return an instance, but instead is a valid type designator. e.g., my_array: #array[#f32].
  • #my_type[generic_args...](args...) is for calling a generic constructor, which returns a specified instance of #my_type.
in particular, there are a few minor inconsistencies/concerns:
  • i would have liked to see #my_fn(...) and #my_type[...] both "return" types, but the first one returns an instance. this inconsistency is mostly for convenience of the type name to variable name correspondence. if you have some_variable_name as an argument of some random type, you can use #some_variable_name(...) as a constructor to build other instances of that same type, and not some other more confusing syntax like some_variable_name(...). it would make me want to get rid of subscript access overloading, but being able to do some_array[3] rather than some_array at(3) is super engrained. i also wanted to avoid new if possible, so #my_type(args...) seemed like the best way to go.
  • fn[generic_args...](args...) could potentially be confused with (some_var[args...])(new_args...); i.e., if your subscript accessor returns a function, we'll likely require you to use the parenthetical version or some_var[args...] fn(new_args...) to avoid confusion. the alternative is to make the grammar scope-aware, but that doesn't seem ideal.
  • types are not exactly first-class citizens in oh-lang. as a strongly-typed language, oh-lang needs to be able to say what the type of something is at compile time, so returning a type from a function (e.g., my_random_type[]: #type { if random() < 0.5 { #int } else { #f64 } } isn't possible. however, types are not flying economy either, since oh-lang offers things like type reflection.
†: i probably won't allow defining subscript functions outside of class bodies, because it's then one more identifier type i have to distinguish.

Comments