-
-
Notifications
You must be signed in to change notification settings - Fork 658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module-level static declarations (closes #7452) #8460
Conversation
Why is it called "globals" in AST but "module statics" after typing? |
Naming is hard :) Well the idea is that the untyped AST does not care about whether it's implemented as statics or not, so it uses the term "global", which might as well be "module-local" or something, I'm not really sure. In the typed AST it's implemented as statics collected in a class, so it makes sense to name it as that. |
9575b29
to
ab8db8d
Compare
3d29516
to
b3b83eb
Compare
c333f12
to
f0fd752
Compare
Thinking about this with a "fresh" head, maybe we should officially call them module statics. It is easy to pronounce, close to the implementation and rather easy to understand: "like class statics, but module statics". |
So you could have module level static functions, with module level properties and constants ( finals ) soon to be implemented, but are you going to change EGlobal to EModule ? |
EStatic, I guess, but yeah :) |
Will modules have some kind of initialiser hook? I mean, in js you can just add code to the body to be ran when at runtime when the module is interpreted and it can be quite useful. |
I am not sure what exactly are you asking here. In JS modules are loaded at run-time, while in Haxe we just load them at compile-time to find and build the classes, there's no concept of a module at run-time. |
I'm aware that it's a compile-time feature. I was just curious if some abstraction for initialization would be provided that would then run at runtime, but I'm probably not fully aware of the implementation and how it's going to work. |
I wonder if defining |
I think it will, but so will var initializations - both will be executed at program startup. |
5eae1e5
to
55970a3
Compare
273154e
to
ebf3630
Compare
9bf5973
to
5e9d742
Compare
This should close #7452 |
(blindly) rebased on top development with all the latest changes, let's see if tests still pass |
72651ec
to
991550d
Compare
eff9b32
to
61dee7c
Compare
I need to check and add test for the behaviour of module-statics regarding #9367, and will merge afterwards. |
A fun side effect of this evolution is that one could create module level code in, say, Looking awesome overall! |
Any guess as to what release this will land in? |
Whoops. Sorry, just realized https://github.com/HaxeFoundation/haxe-evolution/blob/master/proposals/0007-module-level-funcs.md says 4.2.0. |
Still kinda to do. For the "display stuff" there are separate issues now, rtti and access controls are... also waiting for specific issues :) |
* support metadata in var declaration syntax HaxeFoundation/haxe#9618 * print arrow functions correctly in haxe.macro.Printer HaxeFoundation/haxe#9151 * Module-level static declarations HaxeFoundation/haxe#8460 * [parser] allow `function` as package name HaxeFoundation/haxe#7697 * compatibility with Haxe 4.1
This PR implements module-level function, var and property declaration (proposed and discussed in HaxeFoundation/haxe-evolution#24):
Generated JS
This is implemented the following way:
type_def
variant:EStatic of (placed_access, class_field_kind) definition
EStatic
s are collected into a list ofstatic
fields for an implicitly created class (fields arepublic
unless explicitly defined asprivate
, just like types)cl_kind
:KModuleStatics of module_def
m_statics : tclass option
import Mod
will import all the module-level staticsimport Mod.func [as name]
will first try importing module-levelfunc
andMod.func
static fieldimport Mod.*
will NOT import module-level statics (debatable, but consistent with it not importing other module-level declarations)-main Mod
works with module-levelmain
functionusing Mod
will consider module-level static extension functionsmacro
functions are also supportedhaxe.macro.Type.ClassKind
variant:KModuleStatics(module:String)
haxe.macro.Expr.TypeDefKind
variant:TDStatic(kind:FieldType, ?access:Array<Access>)
KModuleStatics
class and instead output functions and vars directly (implemented for JS)static
/final
modifiers)TODO checklist:
import pack.Module.moduleLevel
import pack.Module.moduleLevel as otherName
pack.Module.moduleLevel
access (the MORDOR)resume
@:access
)@:rtti
(ugh)haxe.PosInfos
Context.defineType/defineModule
for staticsmain
--macro
calls and@:build
(tame eval)import Mod.*
private
when importing not to pollute the local namespace (prevents@:privateAccess
but is consistent with private types)Things that can be done after the merge: #9436