Repository navigation
Description
Class Syntax
Here is the proposal to add a new syntax for defining custom classes in scripts. They may eventually become a replacement for static classes in the classes.db.
Let's take a look at an example:
class Actor
putInCar(car)
036A: put_actor self in_car car
end
end
var
0@: Actor
end
[email protected]($car)
Here we defined a class named Actor that has one method putInCar in. putInCar method has one parameter car. Then we declared a variable 0@ of the type Actor and finally called method putInCar on that class instance.
Important Notes
- class declaration is lazy. it means no code is produced until it gets actually used in the code. if we were to remove
[email protected]($car)no code was produced in this script. - each method receives an implicit variable
selfthat represents the class instance the method is called on. in this case it is the variable0@ - in this example
[email protected]($car)gets compiled into036A: put_actor 0@ in_car $car
Design Choices
-
when there is a method call statement the compiler takes the method body and replaces the statement with the entire body. it means the method could have many instructions and possibly even labels and they will be put in place of the call statement. in this sense class method are more templates than functions as in the other languages.
-
this however does not prevent local variables from being altered inside the method body. it means there is no separate scope created when the class method is called and if you, for example, change the variable 0@ in the class method it will be a side-effect for the caller:
class Actor
putInCar(car)
036A: put_actor self in_car car
0@ = 2
end
end
0@ = 1
[email protected]($car)
// 0@ is now equal to 2
There are a few possible choices to this dilemma:
- leave it as is, considering this a liberate language design decision (when you invoke subroutines with
gosubthey could alter local scope as well) - forbid using local variables syntax inside methods. only use arguments passed into the method. this could be inconvenient if you need to keep a local state.
- add extra prologue and epilogue before and after each method call that will store and restore all local variables. this adds extra overhead in runtime
- use CLEO's scm_function that basically does # 3. this adds unnecessary dependency on CLEO.
- however if preserving a local scope is important, one could consider using a scm function inside a method body:
class Actor
putInCar(car)
0AB1: call_scm_func @local 0
:local
036A: put_actor self in_car car
0@ = 2
0AB2: ret 0
end
end
0@ = 1
[email protected]($car)
// 0@ is still 1
Activity
x87 commentedon Jul 24, 2020
Conditional Methods
method could be conditional:
gets compiled as
Design Choices
If the method is a template that simply gets inserted in-place of a call statement (see Design choices in the first post) you can not put anything except conditional opcodes in a method to be able to use it in the conditional expression. i.e. this would not be allowed:
x87 commentedon Jul 24, 2020
Returning Results
It's unclear how to work with returning a value from the methods, i.e consider:
how to represent
x,y,zin the code?Design Choices
always having to pass a variable to store a result into can be tedious for simple cases like:
one would want to simply have
resultthen will represent the left side of the call expression:then
this adds a lot of unusual syntax into the language.
resultfor a single value and pass variables into the method for multiple values:wmysterio commentedon Jul 25, 2020
Powerful! The question is: will the compiler pick up the declared classes if they are described in another file and were included via the
{$i}directive. There are plans to write such classes (I already have everything ready for this) and I would like to save the developer from copying everything every time.I also wanted to ask: will it be possible to add constants to a class? Like this
x87 commentedon Aug 21, 2020
@wmysterio
yes, the compiler definitely should be able to read class declarations from imported files.
static class variables could be a nice feature! Will keep that in mind.
x87 commentedon Dec 23, 2020
Class instances should support inline declaration similar to primitive types:
x87 commentedon Sep 18, 2023
todo: update this proposal after #263
x87 commentedon Sep 6, 2024
2024 Draft