From e06d83649c3a82c06000dad970c3fa474c206831 Mon Sep 17 00:00:00 2001 From: Nathan Singer Date: Fri, 1 Dec 2023 14:15:52 -0500 Subject: [PATCH] Updates the style guide to include the proper use of the `::` operator (#80006) As the title says. Feel free to suggest changes if you feel like my addition is not well detailed / accurate enough. --- .github/guides/STYLE.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/.github/guides/STYLE.md b/.github/guides/STYLE.md index 9ca0ad3fa4680..9c462b99df648 100644 --- a/.github/guides/STYLE.md +++ b/.github/guides/STYLE.md @@ -303,6 +303,47 @@ world.log << "[apples] apples left, taking one." apples-- ``` +### initial() versus :: +`::` is a compile time scope operator which we use as an alternative to `initial()`. +It's used within the definition of a datum as opposed to `Initialize` or other procs. + +```dm +// Bad +/atom/thing/better + name = "Thing" + +/atom/thing/better/Initialize() + var/atom/thing/parent = /atom/thing + desc = inital(parent) + +// Good +/atom/thing/better + name = "Thing" + desc = /atom/thing::desc +``` + +Another good use for it easy access of the parent's variables. +```dm +/obj/item/fork/dangerous + damage = parent_type::damage * 2 +``` + +```dm +/obj/item/fork + flags_1 = parent_type::flags_1 | FLAG_COOLER +``` + + +It's important to note that `::` does not apply to every application of `initial()`. +Primarily in cases where the type you're using for the initial value is not static. + +For example, +```dm +/proc/cmp_subsystem_init(datum/controller/subsystem/a, datum/controller/subsystem/b) + return initial(b.init_order) - initial(a.init_order) +``` +could not use `::` as the provided types are not static. + ## Procs ### Getters and setters