Strings Mathematics [True/False] Operators and Symbols [ternary operator] General (Control Flow/Debugging) Dates Objects [Type/TypeFull] New Features Timelines Sections (Single-Line Examples): Core Functions (e.g. print, noop, swap, version/OS version, sleep/set timer, assert) Error Handling (try/catch/finally, throw) Loops Keywords / Constants Function/Class Definitions Assign/Declare Single Variables Assign/Declare Multiple Variables File Extensions/Artwork Concatenate Strings/Integers The Universal Function Library Project: Details Sections (Multi-Line Examples): Multi-Line Strings Sleep/Tick Counts (benchmark tests) Function Examples: Add/Sum Function Examples: FillParamVar/FillParamFixed Class Examples (Full) Class Examples (Rectangle) Note: UFL: Universal Function Library, a set of around 100-300 standard functions for all programming languages. Languages do not have to implement anything exactly to the UFL specification, it is a guide. See lower down for further details. Section: Core Functions UFL: Swap [consider 'Swap(&a, &b)' or 'Swap(a, b)' or a swap statement 'swap a b'][destructuring assignment is unclear when longer variable names are used] AutoHotkey: ___ C++: swap [e.g. std::swap(a, b)] C#: ___ [can use: (a, b) = (b, a)] [note: destructuring assignment] Crystal: ___ [can use: a, b = b, a] [note: destructuring assignment] Excel: ___ Excel VBA: ___ Go: ___ [can use: a, b = b, a] [note: destructuring assignment] Java: ___ JavaScript: ___ [can use: [a, b] = [b, a]] [note: destructuring assignment] Kotlin: ___ PHP: ___ [can use: [$a, $b] = [$b, $a]] [also: list($a, $b) = [$b, $a]] [note: destructuring assignment] Python: ___ [can use: a, b = b, a] [note: destructuring assignment] R: ___ Ruby: ___ [can use: a, b = b, a] [note: destructuring assignment] Rust: ___ [can use: (a, b) = (b, a)] [note: destructuring assignment] Scala: ___ Swift: swap [e.g. swap(&a, &b)] [also: (a, b) = (b, a)] [note: destructuring assignment] UFL: Noop ['no operation', a function that does nothing] AutoHotkey: ___ [can use: {}] [note: in some contexts this creates an object] C++: ___ [can use: ;] C#: ___ [can use: ;] Crystal: ___ [can use: nil] Excel: ___ Excel VBA: ___ Go: ___ Java: ___ [can use: ;] JavaScript: ___ [can use: {} (typically with no semicolon)] [note: in some contexts this creates an object] Kotlin: ___ PHP: ___ [can use: ;] Python: ___ [can use: pass] R: ___ Ruby: ___ [can use: nil] Rust: ___ [can use: {} (typically with no semicolon)] Scala: ___ Swift: ___ UFL: PrintWithNewLine [or PrintLn][print string and a newline character] AutoHotkey: ___ [can use: OutputDebug/MsgBox/ToolTip/TrayTip] C++: std::println(vVar) [also: std::printf(vVar)] [note: std::println (C++23)] [also: std::cout << vVar << "\n"] [also: std::cout << vVar << std::endl] C#: Console.WriteLine(vVar) Crystal: p vVar [also: puts vVar] [also: print vVar, "\n"] [note: p keyword/p()/puts keyword/puts()] [note: p() prints strings as code (e.g. "a\"b"), rather than their appearance (e.g. a"b)] [note: puts() doesn't print arrays newline-separated (Ruby does)] Excel: ___ Excel VBA: Debug.Print vVar [note: text is printed to the Immediate Window] Go: fmt.Println(vVar) Java: System.out.println(vVar) JavaScript: console.log(vVar) [also: alert(vVar)] [note: to see more console methods: console.log(console)] Kotlin: println(vVar) PHP: var_dump($vVar) [also: echo $vVar . "\n"] [also: echo $vVar . "<br>"] [note: var_dump() always prints a line break, print_r() prints a line break for objects but not values] Python: print(vVar) R: print(vVar) [also: cat(vVar, "\n")] [also: (vVar)] [note: print() prints strings as code (e.g. "a\"b"), rather than their appearance (e.g. a"b)] [also: writeLines(), noquote(), paste(), paste0()] Ruby: p vVar [also: puts vVar] [also: print vVar, "\n"] [note: p keyword/p()/puts keyword/puts()] [WARNING: puts() prints arrays newline-separated] [note: p() prints strings as code (e.g. "a\"b"), rather than their appearance (e.g. a"b)] Rust: println!("{}", vVar) Scala: println(vVar) Swift: print(vVar) UFL: PrintWithoutNewLine [or PrintNoLn][print string without a line break (newline) character] AutoHotkey: ___ [can use: OutputDebug/MsgBox/ToolTip/TrayTip] C++: std::print(vVar) [note: std::print (C++23)] [also: std::cout << vVar] C#: Console.Write(vVar) Crystal: print [note: print keyword/print()] [note: print()/p()/puts() print arrays on one line (in Ruby puts() prints arrays newline-separated)] Excel: ___ Excel VBA: Debug.Print vVar; [note: use a semicolon to omit the line break] [note: text is printed to the Immediate Window] [also: set active cell value: ActiveCell.Value = vValue] [also: set value of cell below active cell: ActiveCell.Offset(1, 0).Value = vValue] Go: fmt.Print(vVar) Java: System.out.print(vVar) JavaScript: ___ Kotlin: print(vVar) PHP: echo $vVar [note: echo keyword/echo()/print keyword/print()/var_export() never print a line break, print_r() prints a line break for objects but not values] [MAJOR WARNING: echo/print/print_r print true/false/null as "1"/""/"" (var_dump()/var_export() print true/false/NULL)] Python: print(vVar, end="") R: cat [e.g. cat(vVar)] Ruby: print [note: print keyword/print()] [note: print() and p() print arrays on one line] Rust: print!("{}", vVar) Scala: print(vVar) Swift: print(vVar, terminator:"") UFL: PrintNewLine [or PrintBlankLine][print a line break (newline) character]['print a blank line' (if following a line break)] AutoHotkey: ___ [can use: OutputDebug/MsgBox/ToolTip/TrayTip] C++: std::cout << "\n" C#: Console.WriteLine() Crystal: puts [also: print "\n"] Excel: ___ Excel VBA: Debug.Print Go: fmt.Println() Java: System.out.println() JavaScript: console.log() Kotlin: println() PHP: echo "\n" [also: print "\n"] [also: print_r("\n")] Python: print() R: cat("\n") Ruby: puts [also: print "\n"] Rust: println!() [also: println!("")] [also: println!("{}", "")] Scala: println() Swift: print() UFL: (PrintCustom) [define a custom function/macro 'myprint' to print a string (and a line break)][all handle at least 0 and 1 params unless stated][see also: MakeFuncForEachDemoPrint] AutoHotkey: myprint := MsgBox [also (append to clipboard): myprint := (vVar:="") => A_Clipboard .= vVar "`r`n"] C++: template<typename T=char[]> void myprint(T const& vVar="") {std::cout << (vVar) << "\n";} [also (a custom macro, which doesn't handle 0 params): #define myprint(vVar) std::cout << (vVar) << "\n"] C#: public static void myprint<T>(T vVar) {Console.WriteLine(vVar);} public static void myprint() {Console.WriteLine();} [note: 1 custom function with 2 overloads] Crystal: def myprint(*oVar); oVar.size>0 ? p(oVar[0]?) : puts(); end Excel: ___ Excel VBA: Function myprint(Optional vVar = ""): Debug.Print vVar: End Function Go: myprint := fmt.Println Java: public static <T> void myprint(T vVar) {System.out.println(vVar);} public static void myprint() {System.out.println();} [note: 1 custom function with 2 overloads] JavaScript: myprint = console.log; Kotlin: fun myprint(vVar: Any?="") : Unit {println(vVar)} [also: val myprint: (Any?)->Unit = ::println] PHP: function myprint($vVar="") {func_num_args() && var_export($vVar); echo("\n");} Python: myprint = print R: myprint = \(vVar) if (nargs()>0) print(vVar) else cat("\n") Ruby: def myprint(*oVar); oVar.size>0 ? p(oVar[0]) : puts(); end Rust: macro_rules! myprint {($vVar:expr)=>{println!("{:?}", $vVar)};()=>{println!()}} [note: a custom macro] [e.g. usage: myprint!(vVar)] Scala: var myprint = (vValue:Any) => println(vValue) Swift: func myprint(_ vVar: Any="") -> () {print(vVar)} [also: can use 'Void' instead of '()'] UFL: PrintType [print type of value (with newline)][see also: Any.Type/Any.TypeFull] AutoHotkey: ___ [can use: MsgBox(Type(oObj))] [also: MsgBox(Object.Prototype.ToString.Call(oObj))] C++: std::printf("%s", typeid(oObj).name()) [also: std::cout << typeid(oObj).name() << "\n"] C#: Console.WriteLine(oObj.GetType().Name) [also: Console.WriteLine(oObj.GetType().FullName)] Crystal: p oObj.class [also: p oObj.class.name.split("::").last] Excel: =TYPE(A1) [also: =ERROR.TYPE(A1)] Excel VBA: Debug.Print TypeName(oObj) Go: fmt.Printf("%T\n", oObj) Java: System.out.println(oObj.getClass().getName()) [also: System.out.println(oObj.getClass().getSimpleName())] JavaScript: console.log(typeof oObj) [also: console.log(Object.prototype.toString.call(oObj))] Kotlin: println(oObj::class.qualifiedName) [also: oObj!!::class.qualifiedName] [also: println(oObj::class.simpleName)] PHP: var_dump(gettype($oObj)) [also: echo gettype($oObj) . "\n"] Python: print(type(oObj).__name__) [also: print(type(oObj))] R: typeof(oObj) [also: class(oObj)/mode(oObj)/storage.mode(oObj)] Ruby: p oObj.class [also: p oObj.class.name.split("::").last] Rust: println!("{}", type_name_of_val(&oObj)) [requires: use std::any::type_name_of_val] Scala: println(oObj.getClass().getSimpleName()) [also: println(oObj.getClass().getName())] Swift: print(String(describing:type(of:oObj))) [also: print(String(reflecting:type(of:oObj)))] UFL: (PrintShowMoreDigits) [to permanently/temporarily show more digits][e.g. print 2**70, e.g. print 1e20] AutoHotkey: ___ C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ Go: ___ Java: ___ JavaScript: ___ [can use: BigInt(vNum).toString()] Kotlin: ___ PHP: ___ Python: ___ R: ___ [can use (default 7 sig figs, max 22 sig figs): options(digits=22)] [also (prevent 1e+5, and higher powers of 10 being printed in '1e+#' form, default 0): options(scipen=999)] Ruby: ___ Rust: ___ Scala: ___ Swift: ___ UFL: Version [get the programming language version] AutoHotkey: A_AhkVersion C++: __cplusplus C#: ___ Crystal: Crystal::VERSION Excel: INFO("release") Excel VBA: ___ [e.g. #If VBA6 Then] [e.g. constants: VBA7/VBA6, Win64/Win32/Mac] [also: Application.Version (for the Excel version number)] Go: runtime.Version() [requires: import "runtime"] Java: System.getProperty("java.version") [also: Runtime.version().version().get(0)] JavaScript: ___ Kotlin: KotlinVersion.CURRENT PHP: phpversion() Python: sys.version [requires: import sys] R: paste(version[c("version.string")]) [also: paste(getRversion())] [also: paste(version[c("major", "minor")], collapse=".")] [also (multiple properties): version] [also (multiple properties): R.Version()] Ruby: RUBY_VERSION Rust: ___ Scala: ___ [can use: util.Properties.versionNumberString] [WARNING: states 2.x, when the version is 3.x] Swift: ___ [e.g. #if swift(>=5.9)] UFL: OSVersion [get the operating system type/version (e.g. Windows 10)] AutoHotkey: A_OSVersion C++: ___ C#: Environment.OSVersion Crystal: ___ [can use: Crystal::DESCRIPTION] Excel: INFO("osversion") [e.g. 'Windows (32-bit) NT 6.02'] [also: INFO("system") (e.g. 'pcdos')] Excel VBA: Application.OperatingSystem [e.g. 'Windows (32-bit) NT 6.02'] [e.g. #If Win64 Then] [e.g. constants: Win64/Win32/Mac] Go: runtime.GOOS [requires: import "runtime"] Java: System.getProperty("os.name") [also: 'os.version'/'os.arch'] JavaScript: navigator.userAgent [also: navigator.platform] Kotlin: System.getProperty("os.name") [also: 'os.version'/'os.arch'] PHP: php_uname() Python: platform.platform() [also: platform.system()/platform.release()/platform.version()/sys.platform/os.name] R: paste(version["platform"]) [also: version["arch"]/version["os"]/version["system"]] [also (multiple properties): version] [also (multiple properties): R.Version()] Ruby: RUBY_PLATFORM Rust: env::consts::OS [requires: use std::env] [also: env::consts::FAMILY, env::consts::ARCH] Scala: System.getProperty("os.name") [also: 'os.version'/'os.arch'] Swift: ProcessInfo().operatingSystemVersion UFL: Sleep [perhaps consider friendly format options e.g. 'm'/'s'] AutoHotkey: Sleep [e.g. 1 sec: Sleep(1000)] C++: std::this_thread::sleep_for [e.g. 1 sec: std::this_thread::sleep_for(std::chrono::milliseconds(1000))] [also: sleep/usleep/nanosleep] C#: System.Threading.Thread.Sleep [e.g. 1 sec: System.Threading.Thread.Sleep(1000)] Crystal: sleep [e.g. 1 sec: sleep 1] [also: sleep(1)] Excel: ___ Excel VBA: Application.Wait [e.g. Application.Wait (Now + TimeValue("0:00:01"))] [also: the Winapi (kernel32\Sleep)] Go: time.Sleep [e.g. 1 sec: time.Sleep(1 * time.Second)] [requires: import "time"] Java: Thread.sleep [e.g. 1 sec: Thread.sleep(1000)] [also: TimeUnit.SECONDS.sleep] [note: both require a try/catch block] JavaScript: ___ [can use: 'while' and 'new Date()'] [can use (inside an async function): await new Promise(r => setTimeout(r, 1000))] Kotlin: Thread.sleep [e.g. 1 sec: Thread.sleep(1000)] [also: TimeUnit.SECONDS.sleep] PHP: sleep [also: usleep] [e.g. 1 sec: sleep(1)] [e.g. 1 sec: usleep(1000000)] Python: time.sleep [e.g. 1 sec: sleep(1)] [note: sleep accepts fractions of seconds, e.g. 0.5 sec: sleep(0.5)] R: Sys.sleep [e.g. 1 sec: Sys.sleep(1)] Ruby: sleep [e.g. 1 sec: sleep 1] [also: sleep(1)] Rust: thread::sleep [e.g. 1 sec: thread::sleep(time::Duration::from_millis(1000))] [requires: use std::{thread, time}] Scala: Thread.sleep [e.g. 1 sec: Thread.sleep(1000)] Swift: sleep [also: usleep] [e.g. 1 sec: sleep(1)] [e.g. 1 sec: usleep(1000000)] UFL: TickCount [get the current time (e.g. in milliseconds) (typically centisecond accuracy, or a higher resolution if possible), take the difference of 2 timestamps to get the time elapsed][e.g. milliseconds since program started, milliseconds since PC switched on][see also: BenchmarkTest] AutoHotkey: A_TickCount [e.g. vMSec := A_TickCount] [note: A_TickCount, a built-in variable] [type: Integer] C++: std::chrono::system_clock::now [e.g. oTime = std::chrono::high_resolution_clock::now()] [also: the Winapi (kernel32\GetTickCount64)] [type (a time point): e.g. NSt3__26chrono10time_pointINS0_12steady_clockENS0_8durationIxNS_5ratioILx1ELx1000000000EEEEEEE] [requires: #include <chrono>] C#: DateTime.Now [e.g. oTime = DateTime.Now] [type: DateTime] Crystal: Time.local [e.g. oTime = Time.local] [also: Time.utc] [type (both): Time] Excel: NOW [e.g. milliseconds: TEXT(NOW()*86400000,"0")] [also: TEXT(NOW(),"hh:mm:ss.000")] [note: NOW() returns days plus fractions of days] [note: Copy, Paste Special..., Values, to lock the values] [type (NOW): 1 (Number)] [type (TEXT): 2 (Text)] Excel VBA: Timer [e.g. vSec = Timer] [note: seconds plus fractions of seconds] [type: Single] Go: time.Now() [e.g. oTime := time.Now()] [requires: import "time"] [type: time.Time] Java: System.currentTimeMillis [e.g. vMSec = System.currentTimeMillis()] [also: System.nanoTime] [type (both): long] JavaScript: performance.now [e.g. vMSec = performance.now()] [type: number] Kotlin: System.currentTimeMillis [e.g. vMSec = System.currentTimeMillis()] [also: System.nanoTime] [type: Long] PHP: microtime [e.g. $vSec = microtime(true)] [note: seconds plus fractions of seconds] [type: double] Python: time.time [e.g. vSec = time.time()] [note: seconds plus fractions of seconds] [type: float] R: Sys.time() [e.g. oTime = Sys.time()] [type: double (class: POSIXct POSIXt)] [type (difference of 2 times): double (class: difftime)] Ruby: Time.now [e.g. oTime = Time.now] [type: Time] Rust: time::SystemTime::now [e.g. oTime = time::SystemTime::now()] [requires: use std::time] [type: std::time::SystemTime] Scala: System.currentTimeMillis [e.g. vMSec = System.currentTimeMillis()] [also: System.nanoTime] [type (both): long] Swift: DispatchTime.now [e.g. vNSec = DispatchTime.now().uptimeNanoseconds] [requires: import Foundation] [type: UInt64] UFL: SetTimer [create a timer, e.g. to execute a function once at a specific time, or at regular intervals] AutoHotkey: SetTimer C++: ___ C#: System.Timers.Timer Crystal: Thread.new Excel: ___ Excel VBA: Application.OnTime Go: time.NewTimer [requires: import "time"] Java: java.util.Timer JavaScript: setInterval [also: setTimeout] Kotlin: java.util.Timer [also: android.os.Handler] PHP: ___ Python: threading.timer R: ___ Ruby: Thread.new Rust: ___ [can use: tokio::spawn] Scala: java.util.Timer [e.g. oTimer = new java.util.Timer()] Swift: Timer.scheduledTimer UFL: Assert [for testing: confirm that a value is equal to another value, else throw] AutoHotkey: ___ C++: assert [e.g. assert(vVar1 == vVar2)] [note: assert macro] [requires: #include <cassert>] C#: Debug.Assert [e.g. Debug.Assert(vVar1 == vVar2)] [also: Trace.Assert] [requires: using System.Diagnostics] [WARNING: may need to enable debug mode, and/or use listeners] Crystal: should [e.g. vVar1.should eq(vVar2)] [requires: require "spec"] Excel: ___ [can use: can simply type in expressions e.g. '=A1=A2'] [can use: IF] Excel VBA: Debug.Assert [e.g. Debug.Assert vVar1 = vVar2] Go: ___ Java: assert [e.g. assert vVar1 == vVar2] [e.g. assert vVar1 == vVar2 : "my error message"] [WARNING: -enableassertions (or -ea) switch must be on] [note: assert keyword] [can use: if (!vCond) throw new AssertionError()] JavaScript: ___ Kotlin: assert [e.g. assert(vVar1 == vVar2)] [also: assertEquals] [e.g. assertEquals(vVar1, vVar2)] [WARNING: assertEquals parameter order is 'expected' then 'actual'] [requires (assertEquals): import kotlin.test.assertEquals] PHP: assert [e.g. assert($vVar1 == $vVar2)] Python: assert [e.g. assert vVar1 == vVar2] [note: assert statement] R: stopifnot [e.g. stopifnot(vVar1 == vVar2)] [e.g. stopifnot("my error message" = vVar1 == vVar2)] Ruby: assert_equal [e.g. assert_equal(vVar1, vVar2)] [requires: require "test/unit/assertions"] [requires: include Test::Unit::Assertions] Rust: assert [e.g. assert!(vVar1==vVar2, "my error message")] [also: assert_eq!] [note: assert/assert_eq macros] Scala: assert [e.g. assert(2+2 == 4)] Swift: assert [e.g. assert(vVar1 == vVar2)] Section: Error Handling UFL: Try/Catch/Finally [note: are braces needed for branches] AutoHotkey: try/catch/finally C++: try/catch/___ C#: try/catch/finally Crystal: begin/rescue/ensure/end Excel: ___/___/___ Excel VBA: ___/___/___ [can use: 'On Error Resume Next' (disables error reporting, but the Err object is still updated), 'On Error Goto MyLabel:' (e.g. can be used as a catch block workaround), 'On Error Goto 0' (default, enables error reporting)] Go: ___/___/___ Java: try/catch/finally JavaScript: try/catch/finally Kotlin: try/catch/finally PHP: try/catch/finally Python: try/except/finally R: ___/___/___ [can use: tryCatch (with params: expr/error/finally, also warning)] Ruby: begin/rescue/ensure/end Rust: ___/___/___ Scala: try/catch/finally Swift: do/catch/___ [also: try] [also: defer] UFL: Throw [code to indicate that an error has occurred][typically 'throw' within a 'try' block leads to 'catch'/'finally' blocks being executed, and code outside a 'try' block ends the program] AutoHotkey: throw Error("my message") C++: throw std::runtime_error("my message") [note: various classes derive from std::exception] [can use (with catch, but not throw): std::exception] C#: throw new Exception("my message") Crystal: raise "my message" Excel: ___ Excel VBA: Err.Raise 1234, , "my message" Go: ___ [also: panic] Java: throw new Exception("my message") [also: Error()] JavaScript: throw new Error("my message") Kotlin: throw Exception("my message") PHP: throw new Exception("my message") Python: raise Exception("my message") R: ___ [can use: stop("my message")] Ruby: raise "my message" Rust: ___ Scala: throw new Exception("my message") [also: Error()] Swift: ___ [can use: throw NSError(domain:"my message", code:1234)] [WARNING: lacks a convenient message field, workaround: use domain field] [also: fatalError("my message")] [requires (NSError): import Foundation] UFL: Error [(or Exception)][create an object with information describing an error] AutoHotkey: Error [note: Exception in AHK v1] C++: std::runtime_error [note: various classes derive from std::exception] [can use (with catch, but not throw): std::exception] C#: Exception Crystal: Exception [e.g. raise Exception.new("my message")] Excel: ___ Excel VBA: ___ Go: ___ [also: panic] Java: Exception [also: Error()] JavaScript: Error Kotlin: Exception PHP: Exception [also: ErrorException(), Error()] Python: Exception R: ___ [also: tryCatch/stop] Ruby: StandardError [e.g. raise StandardError.new "my message"] Rust: ___ Scala: Exception [also: Error()] Swift: ___ [can use: NSError()] [also: fatalError()] [requires (NSError): import Foundation] UFL: FatalError [or Exit][end program/script early, e.g. for testing/debugging][the exact behaviours differ across languages] AutoHotkey: ExitApp() C++: exit(1) [e.g. 0 for success, non-zero for failure] C#: throw new Exception() [WARNING: this would be caught if within a try block] Crystal: abort Excel: ___ Excel VBA: Exit Sub [note: different from 'End Sub'] Go: ___ [also: panic] Java: System.exit(0) JavaScript: throw "" [WARNING: this would be caught if within a try block] Kotlin: exitProcess(0) [note: import kotlin.system.exitProcess] [also (to just throw an exception): throw Exception()] PHP: die Python: raise SystemExit [also: sys.exit()] [WARNING: if the current thread is not the main thread, code may still execute] R: ___ [also: stop] Ruby: abort [note: in some situations, 'rescue'/'ensure' blocks can still be executed] Rust: process::exit(1) [requires: use std::process] Scala: System.exit(0) Swift: fatalError() Section: Loops UFL: LoopCountIncDemo [(or LoopRangeIncDemo/LoopIncDemo)][loop n times (loop from 1 to 10, both inclusive)][see also: Range.New] AutoHotkey: Loop 10 [note (value): A_Index will contain 1, 2, 3 etc] C++: for (int i=1; i<=10; i++) [note: ++i may be preferable to i++] C#: for (int i=1; i<=10; i++) Crystal: (1..10).each do |i| [also: 1.upto(10) do |i|] [afterwards: end] Excel: ___ Excel VBA: For i = 1 To 10 [afterwards: Next] Go: for i := 1; i <= 10; i++ Java: for (int i=1; i<=10; i++) JavaScript: for (let i=1; i<=10; i++) Kotlin: for (i in 1..10) [note: *doesn't work*: for (i in 1 to 10)] [also: for (i in 1.rangeTo(10))] PHP: for ($i=1; $i<=10; $i++) Python: for i in range(1, 10+1): R: for (i in 1:10) [also: for (i in seq(1, 10, 1))] [also: for (i in seq_len(10))] Ruby: for i in 1..10 [also: (1..10).each do |i|] [also: 1.upto(10) do |i|] [afterwards: end] Rust: for i in 1..=10 Scala: for(i <- 1 to 10) Swift: for i in 1...10 [also (if value is unused): for _ in 1...10] UFL: LoopCountIncDemoFloat [(or LoopRangeIncDemoFloat/LoopIncDemoFloat)][loop through a float range, with a step (loop from -10 to 10, with step 0.5, both inclusive)][see also: Range.NewWithStep] AutoHotkey: ___ [can use: Loop 41] [e.g. x := vStart+vStep*(A_Index-1)] [beforehand: vStart := -10, vStep := 0.5] [note (value): A_Index will contain 1, 2, 3 etc] C++: for (double x=-10; x<=10; x += 0.5) C#: for (double x=-10; x<=10; x += 0.5) Crystal: (-10..10).step(0.5).each do |x| [afterwards: end] Excel: ___ Excel VBA: For x = -10 To 10 Step 0.5 [afterwards: Next] Go: for x := -10.0; x <= 10.0; x += 0.5 Java: for (double x=-10; x<=10; x += 0.5) JavaScript: for (let x=-10; x<=10; x += 0.5) Kotlin: ___ [can use: for (x in (-100..100 step 5).map{it/10.0 as Double})] PHP: for ($x=-10; $x<=10; $x += 0.5) Python: ___ [can use: for x in (i/10 for i in range(-100, 100+1, 5)):] R: for (x in seq(-10, 10, 0.5)) Ruby: for x in (-10..10).step(0.5) [also: (-10..10).step(0.5).each do |x|] [afterwards: end] Rust: ___ [can use: for x in (-100..=100).step_by(5).map(|i|i as f64 * 0.1)] Scala: ___ [can use: for(x <- Range.inclusive(-100, 100, 5).map(_/10.toDouble))] Swift: for x in stride(from:-10, through:10, by:0.5) [also (if value is unused): for _ in stride(from:-10, through:10, by:0.5)] UFL: LoopCountIncExcDemo [(or (LoopRangeExcDemo)][loop n times (loop from 0 inclusive to 10 exclusive)][i.e. 0 to 9 inclusive] AutoHotkey: ___ [can use: Loop 10] [note (value): A_Index-1] C++: for (int i=0; i<10; i++) [note: ++i may be preferable to i++] C#: for (int i=0; i<10; i++) Crystal: (0...10).each do |i| [afterwards: end] Excel: ___ Excel VBA: For i = 0 To (10 - 1) [note: parentheses unnecessary, for clarity] [afterwards: Next] Go: for i := 0; i < 10; i++ [also: for i := range 10] Java: for (int i=0; i<10; i++) JavaScript: for (let i=0; i<10; i++) Kotlin: for (i in 0..<10) [also: for (i in 0 until 10)] [also: for (i in 0.rangeUntil(10))] PHP: for ($i=0; $i<10; $i++) Python: for i in range(0, 10): [WARNING: range uses exclusive end] R: for (i in 0:(10-1)) Ruby: for i in 0...10 [also: (0...10).each do |i|] [afterwards: end] Rust: for i in 0..10 [WARNING: .. uses exclusive end] Scala: for(i <- 0 until 10) Swift: for i in 0..<10 [also (if value is unused): for _ in 0..<10] UFL: LoopCountDecDemo [(or LoopRangeDecDemo)][loop n times (loop from 10 to 1, both inclusive)] AutoHotkey: ___ [can use: Loop 10] [note (value): 11-A_Index will give 10, 9, 8 etc] C++: for (int i=10; i>=1; i--) [note: --i may be preferable to i--] C#: for (int i=10; i>=1; i--) Crystal: (10).downto(1) do |i| [afterwards: end] Excel: ___ Excel VBA: For i = 10 To 1 Step -1 [afterwards: Next] Go: for i := 10; i >= 1; i-- Java: for (int i=10; i>=1; i--) JavaScript: for (let i=10; i>=1; i--) Kotlin: for (i in 10 downTo 1) PHP: for ($i=10; $i>=1; $i--) Python: for i in range(10, 1-1, -1): R: for (i in 10:1) [also: for (i in seq(10, 1, -1))] Ruby: for i in (10).downto(1) [also: (10).downto(1) do |i|] [afterwards: end] Rust: for i in (1..=10).rev() Scala: for(i <- 10 to 1 by -1) Swift: for i in stride(from:10, through:1, by:-1) [also: for i in (1...10).reversed()] [note: use 'to' instead of 'through' for exclusive end] [note: if value is unused, use _] UFL: LoopCountDecExcDemo [(or (LoopRangeDecExcDemo)][loop n times (loop from 10 exclusive to 0 inclusive)][i.e. 9 to 0 inclusive] AutoHotkey: ___ [can use: Loop 10] [note (value): 10-A_Index will give 9, 8, 7 etc] C++: for (int i=10-1; i>=0; i--) [note: --i may be preferable to i--] C#: for (int i=10-1; i>=0; i--) Crystal: (10-1).downto(0) do |i| [afterwards: end] Excel: ___ Excel VBA: For i = (10 - 1) To 0 Step -1 [afterwards: Next] Go: for i := 10 - 1; i >= 0; i-- Java: for (int i=10-1; i>=0; i--) JavaScript: for (let i=10-1; i>=0; i--) Kotlin: for (i in 10-1 downTo 0) PHP: for ($i=10-1; $i>=0; $i--) Python: for i in range(10-1, 0-1, -1): R: for (i in (10-1):0) [also: for (i in seq(10-1, 0, -1))] Ruby: for i in (10-1).downto(0) [also: (10-1).downto(0) do |i|] [afterwards: end] Rust: for i in (0..10).rev() Scala: for(i <- 10-1 to 0 by -1) Swift: for i in stride(from:10-1, through:0, by:-1) [also: for i in (0..<10).reversed()] [note: use 'to' instead of 'through' for exclusive end] [note: if value is unused, use _] UFL: LoopInfinite [infinite loop (e.g. loop 1 to infinity, infinite range) (an alternative: 0 to infinity)] AutoHotkey: Loop [also: while True] [note (value): A_Index will contain 1, 2, 3 etc] C++: for(;;) [also: while(1)] [also: for (int i=1;; i++)] C#: for(;;) [also: while(true)] [also: for (int i=1;; i++)] Crystal: loop do [also: while true] Excel: ___ Excel VBA: Do While True [afterwards: Loop] Go: for Java: for(;;) [also: while(true)] [also: for (int i=1;; i++)] JavaScript: for(;;) [also: while(1)] [also: for (let i=1;; i++)] Kotlin: while(true) [also: for (i in generateSequence(1){it+1})] PHP: also: for(;;) [also: while(1)] [also: for ($i=1;; $i++)] Python: while True: [also: for i in itertools.count(start=1):] R: while (TRUE) Ruby: loop do [also: while true] Rust: loop [also: while true] Scala: while(true) Swift: while true [also: for i in 1...] [also (if value is unused): for _ in 1...] UFL: Break/Continue [break: end a loop (break out of a loop)][continue: end current loop iteration (skip to next iteration)] AutoHotkey: break/continue C++: break/continue C#: break/continue Crystal: break/next Excel: ___/___ Excel VBA: Exit For/___ Go: break/continue Java: break/continue JavaScript: break/continue Kotlin: break/continue PHP: break/continue Python: break/continue R: break/next Ruby: break/next Rust: break/continue Scala: ___/___ [can use: breakable and break] [requires (breakable): import scala.util.control.Breaks._] Swift: break/continue UFL: (BreakMult)/(ContinueMult) [break: break out of multiple levels of a loop][continue: continue applied to multiple levels of a loop] AutoHotkey: break MyLabel/continue MyLabel [also: e.g. 2 levels: break 2/continue 2] C++: ___/___ C#: ___/___ Crystal: ___/___ Excel: ___/___ Excel VBA: ___/___ Go: break MyLabel/continue MyLabel Java: break MyLabel/continue MyLabel JavaScript: break MyLabel/continue MyLabel Kotlin: break@MyLabel/continue@MyLabel PHP: ___/___ [also: e.g. 2 levels: break 2/continue 2] Python: ___/___ R: ___/___ Ruby: ___/___ Rust: break 'MyLabel/continue 'MyLabel Scala: ___/___ Swift: break MyLabel/continue MyLabel Section: Keywords / Constants UFL: IfKeywords [if-statement][note: state if bool/parentheses/braces needed] AutoHotkey: if c / else if c / else [note: parentheses *not* needed] C++: if (c) / else if (c) / else C#: if (c) / else if (c) / else [WARNING: c must be a bool] Crystal: if c / elsif c / else / end [note: parentheses *not* needed] Excel: IF(c,x,y) [note: c must be a bool or numeric] Excel VBA: If c Then / ElseIf c Then / Else / End If [note: c must be a bool or numeric] [note: parentheses *not* needed] Go: if c / else if c / else [WARNING: c must be a bool] [note: parentheses *not* needed] Java: if (c) / else if (c) / else [WARNING: c must be a bool] JavaScript: if (c) / else if (c) / else Kotlin: if (c) / else if (c) / else [WARNING: c must be a bool] [note: can be used in expressions] PHP: if (c) / else if (c) / else Python: if c: / elif c: / else: [note: parentheses *not* needed] R: if (c) / else if (c) / else [note: c must be a bool or numeric] [note: 'else if'/'else' must be preceded by '}' on the same line] Ruby: if c / elsif c / else / end [note: parentheses *not* needed] Rust: if c / else if c / else [WARNING: c must be a bool] [WARNING: requires braces for 1-line statements] [note: can be used in expressions] [note: parentheses *not* needed] Scala: if (c) / else if (c) / else [WARNING: c must be a bool] [note: can be used in expressions] Swift: if (c) / else if (c) / else [WARNING: c must be a bool] [WARNING: requires braces for 1-line statements] UFL: SwitchKeywords [switch statement] AutoHotkey: switch/case/default [note: no fall-through] C++: switch/case/default [note: uses fall-through] [WARNING: the input value cannot be a string] C#: switch/case/default [note: no fall-through] [WARNING: C# requires 'break' for *every* case/default (i.e. it looks like fall-through is possible, but it is not, the breaks cannot be removed)] Crystal: case/when/else/end [note: no fall-through] Excel: ___/___/___ Excel VBA: Select/Case/Case Else/End Select [note: no fall-through] Go: switch/case/default [note: no fall-through (unless use 'fallthrough' keyword one or more times)] Java: switch/case/default [note: uses fall-through] [also: switch expressions: also switch/case/default (but *no* fall-through)] JavaScript: switch/case/default [note: uses fall-through] Kotlin: when/(pattern)/else [note: no fall-through] PHP: switch/case/default [note: uses fall-through] Python: match/case/case _ [note: no fall-through] R: ___/___/___ [can use: switch() one-liners, equivalent to a map/dictionary key lookup, returns NULL if not found] [note: no fall-through] Ruby: case/when/else/end [note: no fall-through] Rust: match/(pattern)/_ [note: no fall-through] [note: must be exhaustive (i.e. all possible values must be covered) (workaround: add a default case)] Scala: match/case/case _ [note: no fall-through] [WARNING: throws if no match (workaround: add a default case)] Swift: switch/case/default [note: no fall-through] [note: must be exhaustive (i.e. all possible values must be covered) (workaround: add a default case)] UFL: Null [note: to delete a variable, see VarDelete/Any.DelRef][see also: Optional.NewNull] AutoHotkey: unset [note: case-insensitive] C++: nullptr [note: NULL macro expands to 0] [also (functions): void] C#: null [also (functions): void] Crystal: nil Excel: #NULL! [e.g. type '#NULL!' or '=A1 A2' or '=SUM(A1 A2)' into a cell] [note: use ERROR.TYPE() to get the error type] Excel VBA: Nothing [note: also Empty/Null] [e.g. Set vVar = Nothing] [e.g. vVar = Empty] [e.g. vVar = Null] [e.g. obtain Empty: a variable that hasn't been used yet/ReDim an array then get a value/store the result of a function with no return value] Go: nil [e.g. var vNil []int = nil] Java: null [also (functions): void] JavaScript: null [also: undefined] [also (array values): 'elided'/'empty'] [also: void operator] Kotlin: null [also: Nothing e.g. vVar is Nothing?] [also (functions): Unit] [note: Void is a Java class (with no special meaning in Kotlin)] PHP: null [note: case-insensitive] [also (functions): void, never] Python: None [also: del statement] R: NULL [also: NA] [note: NA and NaN are different] Ruby: nil Rust: ___ [also (option objects): None] [e.g. vNone: Option<i32> = None] Scala: null [also (functions): Unit] Swift: nil [also (functions): Void] UFL: NullDemo [assign null to a variable] AutoHotkey: vVar := unset [note: Type(unset) throws] C++: auto vVar = nullptr [type: Dn] C#: string vVar = null [note: null.GetType().Name throws] Crystal: vVar = nil [type: Nil] Excel: #NULL! [type: 16 (error type: 1)] Excel VBA: Set vVar = Nothing [type: Nothing] [also: vVar = Null] [type: Null] [also: vVar = Empty] [type: Empty] Go: vVar interface{} = nil [type: <nil>] Java: Integer vVar = null [note: null.getClass().getSimpleName()) throws] JavaScript: vVar = null [type: object] [also: vVar = undefined] [type: undefined] Kotlin: vVar = null [note: null::class.simpleName throws] PHP: $vVar = null [type: Null] Python: vVar = None [type: NoneType] R: vVar = NULL [type (NULL): NULL] [also: vVar = NA] [type (NA): logical] Ruby: vVar = nil [type: NilClass] Rust: vVar: Option<i32> = None [type: core::option::Option<i32>] Scala: vVar = null [note: null.getClass().getSimpleName()) throws] Swift: vVar: Int? = nil [type: e.g. Optional<Int>] UFL: IsNull [see also: Null/Array.KeyIsEmpty/FuncVoidType/Optional.IsNull] AutoHotkey: vIsNull = !IsSet(vVar) C++: ___ [e.g. (std::string): vIsNull = (vVar == (std::string)NULL)] [WARNING: NULL is defined as 0, so for many types, e.g. int, comparing with null is actually comparing with 0] C#: vIsNull = (vVar == null) [also: !vVar.HasValue] Crystal: vIsNull = vVar.nil? [also: vIsNull = (vVar == nil)] Excel: =ERROR.TYPE(A1)=1 Excel VBA: ___ [e.g. (TypeName(vVar) = "Nothing")] [e.g. (TypeName(vVar) = "Empty")] [e.g. (TypeName(vVar) = "Null")] [also (throws if vVar is not an object): vIsNull = vVar Is Nothing] [inverse: Not vVar Is Nothing] [also: IsNull(vVar)] [also: IsEmpty(vVar)] Go: vIsNull := (vVar == nil) Java: var vIsNull = (vVar == null) [e.g. optional object: !oOpt.isPresent()] JavaScript: vIsNull = (vVar === null) [also (is undefined): (vVar == undefined)] [also (is undefined): (typeof vVar == "undefined")] [WARNING: vVar == null returns true for null *and undefined*] Kotlin: vIsNull = (vVar == null) [also: vVar is Nothing?] [e.g. nullable object: (oOpt == null)] [inverse: vVar !is Nothing?] PHP: $vIsNull = is_null($vVar) [also: !isset($vVar)] [also: ($vVar == null)] Python: vIsNull = (vVar == None) [also: vVar is None] [inverse: vVar is not None] R: vIsNull = is.null(vVar) [also: is.na()/exists()/missing()] Ruby: vIsNull = vVar.nil? [also: vIsNull = (vVar == nil)] Rust: vIsNull = (vVar == None) [e.g. option object: (oOpt == None)] [also: oOpt.is_none()] [also: !oOpt.is_some()] Scala: vIsNull = !Option(vVar).isDefined [also (doesn't work on ints): var vIsNull = (vVar == null)] Swift: vIsNull = (vVar == nil) [e.g. optional object: (oOpt == nil)] UFL: VarDelete [note: typically: deleting an object instance deletes 1 reference, and only when the last reference is deleted, is the object deleted][see also: Any.DelRef] AutoHotkey: vVar := unset [note: in AHK v1: 'vVar := ""' and 'vVar := 0' were common] C++: delete vVar [also: delete[] vVar] [note: 'delete'/'delete[]' must be used for variables created with 'new'] [note: 'free' must be used for variables created with 'malloc'] C#: vVar = null Crystal: vVar = nil Excel: ___ Excel VBA: Set vVar = Nothing [note: doesn't work with all types] Go: vVar = nil Java: vVar = null JavaScript: vVar = null [also: vVar = undefined] [also: void operator] Kotlin: vVar = null PHP: unset($vVar) [also: $vVar = null] [note: unset() sets contents to null] Python: del vVar [also: vVar = None] R: vVar = NULL Ruby: vVar = nil Rust: drop(vVar) Scala: vVar = null [note: doesn't work on ints] Swift: vVar = nil UFL: Import [(or Include)] AutoHotkey: #Include [also: #IncludeAgain] C++: #include [e.g. #include <iostream>] [e.g. #include <string>] [e.g. #include <map>] C#: using [e.g. using System] [e.g. using System.Linq] [e.g. using System.Collections.Generic] Crystal: require [also: include/extend] Excel: ___ Excel VBA: ___ [can use: Workbooks.Open and Run] Go: import [e.g. import "fmt"] [note: the Go Playground auto-formats multiple imports into one] Java: import [e.g. import java.util.*] [e.g. import java.util.stream.*] JavaScript: import [also: <script src="MyFile.js"></script>] Kotlin: import PHP: include [also: require/include_once/require_once] Python: import [e.g. import sys] [e.g. from functools import reduce] R: library Ruby: require [also: include/extend] Rust: use [e.g. use std::time] Scala: import [e.g. import scala.math] [e.g. import scala.math._] Swift: import [e.g. import Foundation] Section: Function/Class Definitions UFL: (IsCallable) [does a variable contain something function-like/method-like] AutoHotkey: HasMethod(vVar) C++: ___ C#: ___ Crystal: vVar.responds_to? :call Excel: ___ Excel VBA: ___ Go: ___ Java: ___ JavaScript: ___ [can use: vVar instanceof Function] [also: typeof vVar == "function"] Kotlin: ___ PHP: is_callable($vVar) Python: callable(vVar) R: ___ [can use: class(vVar) == "function"] [also: typeof(vVar) == "closure"] Ruby: vVar.respond_to? :call Rust: ___ Scala: ___ [can use: vVar.getClass().getSimpleName().contains("Lambda")] Swift: ___ [note: can check if type contains ' -> ': String(describing:type(of:vVar))] UFL: (FuncAnonTypeDemo) [get type for 'MyAddAnon' examples in FuncDemoAdd (anonymous functions)] AutoHotkey: ___ [type: Func] C++: ___ [type: e.g. Z4mainE3$_0] C#: ___ [type: Func`3] Crystal: ___ [type: Proc(Int32, Int32, Int32)] Excel: ___ Excel VBA: ___ Go: ___ [type: func(int, int) int] Java: ___ [type: e.g. HelloWorld$$Lambda$1/0x0000000100060840] JavaScript: ___ [type: function] Kotlin: ___ [type: null] PHP: ___ [type: object] Python: ___ [type: function] R: ___ [type: closure (class: function)] Ruby: ___ [type: Proc] Rust: ___ [type: e.g. playground::main::MyAddAnon] Scala: ___ [type: e.g. Playground$$$Lambda$7748/0x00007f6c56502800] Swift: ___ [type: (Int, Int) -> Int] UFL: (FuncTypeDemo) [get type for 'MyAdd' examples in FuncDemoAdd (functions)] AutoHotkey: ___ [type: Func] C++: ___ [type: FiiiE] C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ Go: ___ [type: func(int, int) int] Java: ___ JavaScript: ___ [type: function] Kotlin: ___ PHP: ___ Python: ___ [type: function] R: ___ [type: closure (class: function)] Ruby: ___ Rust: ___ [type: e.g. playground::main::MyAdd] Scala: ___ [type: e.g. Playground$$$Lambda$7748/0x00007f6c56502800] Swift: ___ [type: (Int, Int) -> Int] UFL: FuncBelow [functions *can* be defined below where they are called] AutoHotkey: yes C++: yes [note: functions must be defined above where they are called (workaround: *declare* the function above where it is called)] C#: yes Crystal: yes Excel: ___ Excel VBA: yes Go: yes Java: yes JavaScript: yes Kotlin: no [WARNING: functions must be defined above where they are called] PHP: yes Python: no [WARNING: functions must be defined above where they are called] R: no [WARNING: functions must be defined above where they are called] Ruby: no [WARNING: functions must be defined above where they are called] Rust: yes Scala: yes Swift: yes UFL: FuncMutable [function parameters are mutable][i.e. can a parameter variable be overwritten] AutoHotkey: yes C++: yes C#: yes Crystal: yes Excel: ___ Excel VBA: yes Go: yes Java: yes JavaScript: yes Kotlin: no [WARNING: function parameter values (not object members) are read-only (they are not mutable) (workaround: do 'var vMyParamVar = vMyParamVar' to create a local copy, but it leaves a warning)] PHP: yes Python: yes R: yes Ruby: yes Rust: no [WARNING: function parameter values (not object members) are read-only (they are not mutable) (workaround: do 'let mut vMyParamVar = vMyParamVar' to create a local copy)] Scala: no [WARNING: function parameter values (not object members) are read-only (they are not mutable)] Swift: yes [WARNING: function parameter values (not object members) are read-only (they are not mutable) (workaround: do 'var vMyParamVar = vMyParamVar' to create a local copy)] UFL: FuncVoidType [return type in function definition, for function with no return value] AutoHotkey: ___ C++: void C#: void Crystal: ___ [can use: Nil] [can use: Void] [note: types can be omitted] Excel: ___ Excel VBA: ___ Go: ___ Java: void JavaScript: ___ Kotlin: Unit PHP: ___ Python: ___ R: ___ Ruby: ___ Rust: ___ [can use: '()', the unit type] [note: '()' can be omitted] Scala: Unit Swift: () [also: Void] UFL: FuncOmitReturn ['return' *can* be omitted in standard function definitions][implicit return][e.g. last-line return] AutoHotkey: no C++: no C#: no Crystal: yes Excel: ___ Excel VBA: ___ [note: return value is set by assigning to the function name] Go: no Java: no JavaScript: no Kotlin: no [note: 'return' can be omitted in an expression body, but not a block body] PHP: no Python: no R: yes Ruby: yes Rust: yes Scala: yes Swift: yes UFL: FuncAnonOmitReturn ['return' *can* be omitted in anonymous function definitions][implicit return] AutoHotkey: yes C++: no [WARNING: 'return' can't be omitted in anonymous functions] C#: yes Crystal: yes Excel: ___ Excel VBA: ___ Go: no [WARNING: 'return' can't be omitted in anonymous functions] Java: yes JavaScript: yes Kotlin: yes PHP: yes Python: yes R: yes Ruby: yes Rust: yes Scala: yes Swift: yes UFL: FuncMultReturn [function can return multiple values][i.e. can specify multiple variables to receive multiple values][e.g. via tuples, e.g. via destructuring assignment] AutoHotkey: no [can use: VarRef parameters (AHK v1: ByRef parameters)] C++: yes [e.g. std::tie(a, b, c) = MyRetMult()] [e.g. use std::make_tuple and return a tuple] C#: yes [e.g. (a, b, c) = MyRetMult()] [also: can use 'out' parameters] Crystal: yes [e.g. a, b, c = MyRetMult.call()] Excel: ___ Excel VBA: no Go: yes [e.g. a, b, c := MyRetMult()] Java: no JavaScript: yes [e.g. [a, b, c] = MyRetMult()] Kotlin: yes [e.g. var (a, b, c) = MyRetMult()] [note: can use Pair/Triple, and destructuring declaration (but not destructuring assignment, i.e. can initialise variables, but not update variables)] PHP: no Python: yes [e.g. a, b, c = MyRetMult()] R: no Ruby: yes [e.g. a, b, c = MyRetMult.call()] Rust: yes [e.g. (a, b, c) = MyRetMult()] Scala: yes [e.g. var (a, b, c) = MyRetMult()] [note: can use Tuple, and destructuring declaration (but not destructuring assignment, i.e. can initialise variables, but not update variables)] Swift: yes [e.g. (a, b, c) = MyRetMult()] UFL: FuncAnonKeyword [keyword (if any) needed to call anonymous functions] AutoHotkey: ___ [deprecated: AHK v1: Call (AHK v2 onwards: none)] C++: ___ C#: ___ Crystal: call Excel: ___ Excel VBA: ___ Go: ___ Java: ___ [e.g. accept/apply/get/test, applyAsDouble/applyAsInt/applyAsLong, getAsBoolean/getAsDouble/getAsInt/getAsLong] JavaScript: ___ Kotlin: ___ PHP: ___ Python: ___ R: ___ Ruby: call Rust: ___ Scala: ___ Swift: ___ UFL: FuncVarSupersede [can create a variable with the same name a standard function, and supersede it][e.g. an anonymous function can supersede/override/'overwrite' a standard function] AutoHotkey: no [can use: a function's behaviour can be changed via MyFunc.DefineProp("Call", {Call:MyFuncNew})] [note: AHK throws if attempt to assign to a variable with the same name as a function] C++: yes C#: yes Crystal: no [note: myFunc() and myFunc.call() would call different functions] [note: 'myFunc', not 'MyFunc', because function names can't start with a capital letter] Excel: no Excel VBA: no Go: yes Java: no [note: MyFunc() and MyFunc.apply() (or accept()/get()/test() etc) would call different functions] JavaScript: yes Kotlin: yes PHP: no [note: a variable name requires '$', $MyFunc won't override MyFunc] Python: yes R: yes Ruby: no [note: MyFunc() and MyFunc.call() would call different functions] Rust: yes Scala: no [note: a double definition error would occur (or a function overload occurs)] Swift: yes UFL: FuncOverload [can do function overloading?][e.g. same name, different param count (or different param types)] AutoHotkey: no C++: yes [note: C does not support function overloading] C#: yes Crystal: yes Excel: ___ Excel VBA: no Go: no Java: yes JavaScript: no Kotlin: yes PHP: no Python: no R: no Ruby: no Rust: no Scala: yes Swift: yes UFL: FuncDefineMult [can a standard function be defined more than once][i.e. anonymous functions don't count][see also: FuncBelow] AutoHotkey: no [note: can't have 2 functions with the same name] C++: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] C#: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] Crystal: yes (overloads) [MAJOR WARNING: function overloads are allowed, and *can* also have 2 functions with the same name/types/param counts, where the bottommost function takes precedence (it as though the earlier functions didn't exist)] [i.e. you can have multiple overloads, but some are silently discarded] Excel: ___ Excel VBA: no [note: can't have 2 functions with the same name] Go: no [note: can't have 2 functions with the same name] Java: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] JavaScript: yes [WARNING: the bottommost function takes precedence, it as though the earlier functions didn't exist] Kotlin: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] PHP: no [note: can't have 2 functions with the same name] Python: yes [note: the function called is the nearest function above where the call is made (i.e. the same logic as defining/calling anonymous functions)] R: yes [note: all functions are anonymous functions, assigned to variables] Ruby: yes [note: the function called is the nearest function above where the call is made (i.e. the same logic as defining/calling anonymous functions)] Rust: no [note: can't have 2 functions with the same name] Scala: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] Swift: yes (overloads) [note: function overloads are allowed, but can't have 2 functions with the same name/types/param counts] UFL: FuncCallOmitLeading [can omit leading/middle params in function calls?][e.g. MyFunc(,2)][e.g. MyFunc(1,,3)] AutoHotkey: yes C++: no C#: no Crystal: no Excel: yes Excel VBA: yes Go: no Java: no JavaScript: no [can use (to omit a parameter): undefined] [e.g. MyFunc(undefined, 2)] Kotlin: no PHP: no Python: no R: yes Ruby: no Rust: no Scala: no Swift: no UFL: FuncStatic [functions can store local static variables?][note: there are usually short/short-ish workarounds e.g. closures][e.g. increment every time the function is used] AutoHotkey: yes C++: yes C#: no Crystal: no Excel: ___ Excel VBA: yes Go: no Java: no JavaScript: no [can use: add a property to the function] Kotlin: no PHP: yes Python: no [can use: add a property to the function] R: no Ruby: no Rust: yes [note: has to be within an 'unsafe' block] Scala: no Swift: no UFL: FuncPassName [or MethodPassName][pass function by name as a function call parameter][e.g. pass a sort comparator function, e.g. pass a function to map/reduce/filter/for each] AutoHotkey: MyFunc [note: AHK v1: "MyFunc", also: Func("MyFunc")] C++: ::MyFunc [also: MyFunc] [note: '::MyFunc' calls function if it exists, 'MyFunc' calls variable if it exists, else function if it exists] C#: MyFunc Crystal: &->myFunc [e.g. &->myFunc(Int32,Int32)] [e.g. &->oObj.myMethod(String)] [also (anonymous function): &oFunc] [note: see 'Proc' in the documentation] Excel: ___ Excel VBA: "MyFunc" [e.g. Application.Run("MyFunc")] [note: Application.Run works on custom, but not built-in functions] Go: MyFunc Java: MyClass::MyFunc JavaScript: MyFunc Kotlin: ::MyFunc PHP: "MyFunc" Python: MyFunc R: MyFunc Ruby: :MyFunc [e.g. &method(:MyFunc)] [e.g. &oObj.method(:MyMethod)] [also (anonymous function): &oFunc] Rust: MyFunc Scala: MyFunc Swift: MyFunc UFL: (FuncCopyBuiltInDemo) [copy (a reference to/or clone) a built-in function][see also: MakeFuncMapDemoDouble/PrintCustom] AutoHotkey: oFunc := MsgBox [e.g. usage: oFunc(123)] C++: ___ C#: Func<int,int,int> oFunc = Math.Max [e.g. usage: oFunc(3,4)] [note: in C#, can't create an anonymous function with return type 'void'] Crystal: ___ Excel: ___ Excel VBA: ___ [note: Application.Run works on custom, but not built-in functions] [e.g. Application.Run("MyFunc")] Go: oFunc := fmt.Println [e.g. usage: oFunc(123)] Java: ___ JavaScript: oFunc = console.log [e.g. usage: oFunc(123)] Kotlin: val oFunc: (Any?)->Unit = ::println [e.g. usage: oFunc(123)] [WARNING: this example doesn't handle omitting the param] PHP: $oFunc = "var_dump" [e.g. usage: $oFunc(123)] Python: oFunc = print [e.g. usage: oFunc(123)] R: oFunc = print [e.g. usage: oFunc(123)] Ruby: oFunc = ::Kernel.instance_method(:p).bind(0) [e.g. usage: oFunc.call(123)] Rust: ___ Scala: ___ [e.g. MyPrint = println(_: Any)] [e.g. MyPrint0 = println(_: Unit)] [note: syntax for referring to a specific function overload] Swift: let oFunc: (_ vNum1: Int, _ vNum2: Int) -> Int = max [e.g. usage: oFunc(3,4)] UFL: FuncDefaultValue [function parameter default values][e.g. make param 2 optional in the MyAdd() examples below] AutoHotkey: MyAdd(vNum1, vNum2:=100) C++: int MyAdd(int vNum1, int vNum2=100) C#: public static int MyAdd(int vNum1, int vNum2=100) Crystal: def myAdd(vNum1, vNum2=100) Excel: ___ Excel VBA: Function MyAdd(vNum1, Optional vNum2 = 100) Go: ___ [MAJOR WARNING: Go lacks default function arguments, and it lacks function overloading, workaround: each function 'overload' needs a different name] Java: ___ [WARNING: Java lacks default function arguments, workaround: function overloading] JavaScript: function MyAdd(vNum1, vNum2=100) Kotlin: fun MyAdd(vNum1: Int, vNum2: Int=100) : Int PHP: function MyAdd($vNum1, $vNum2=100) Python: def MyAdd(vNum1, vNum2=100): R: MyAdd = function(vNum1, vNum2=100) Ruby: def MyAdd(vNum1, vNum2=100) Rust: ___ [MAJOR WARNING: Rust lacks default function arguments, and it lacks function overloading, and 'functions' via macros require using '!', workaround: each function 'overload' needs a different name] Scala: def Add(vNum1:Int, vNum2:Int=100) : Int = Swift: func MyAdd(_ vNum1: Int, _ vNum2: Int = 100) -> Int UFL: FuncBindDemo [or FuncPartialDemo/FuncBoundDemo][bind a parameter to a function e.g. 'multiply' becomes 'double' (by binding 2 as the 1st param)][create an anonymous function, by modifying an existing existing function, by pre-filling one or more arguments][see also: MakeFuncReduceDemoMul] AutoHotkey: MyDouble := MyMul.Bind(2) C++: auto MyDouble = std::bind(MyMul, 2, std::placeholders::_1) C#: ___ Crystal: MyDouble = MyMul.partial(2) Excel: ___ Excel VBA: ___ Go: ___ Java: ___ JavaScript: MyDouble = MyMul.bind(null, 2) Kotlin: ___ PHP: ___ Python: MyDouble = partial(MyMul, 2) R: ___ Ruby: MyDouble = MyMul.curry[2] Rust: ___ [also: impl: functions can be created by adding parameters to a function that uses impl] Scala: ___ Swift: ___ UFL: FuncCountArgs [count arguments passed to a normal (non-variadic) function][note: variadic functions usually provide an array with a 'length' property/method] AutoHotkey: ___ C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ [can use (if vVar is a Variant): vIsPassed = Not IsMissing(vVar)] Go: ___ Java: ___ JavaScript: arguments.length Kotlin: ___ PHP: func_num_args() Python: ___ R: nargs() Ruby: ___ Rust: ___ Scala: ___ Swift: ___ UFL: FuncParamPassed [or IsPassed/!IsOmitted/!IsMissing][check if a param was passed (to a normal (non-variadic) function) or whether it received the default value] AutoHotkey: ___ C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ [can use (if vVar is a Variant): vIsPassed = Not IsMissing(vVar)] Go: ___ Java: ___ JavaScript: ___ [can use: choose a default value that isn't 'undefined', compare against 'arguments[vIndex]'] [WARNING: this elided (empty) key check, works on arrays but not on 'arguments': (vKey in Array.prototype.slice.call(arguments))] Kotlin: ___ PHP: ___ [can use (0-based param index): array_key_exists($vIndex, func_get_args())] Python: ___ R: ___ [can use: vIsPassed = !missing(vVar)] Ruby: ___ [can use: MyFunc(a, b = c = "default")] [i.e. if b passed, c is nil, else both are 'default'] Rust: ___ Scala: ___ Swift: ___ UFL: (FuncCallDynamic) [call a function dynamically (based on string containing a function name)][workaround: store function references in an array/map][see also: Array.Get/Map.Get/Object.Get/VarGetDynamic] AutoHotkey: vRet := %vFuncName%(vArg1, vArg2) C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: vRet = Application.Run(vFuncName, vArg1, vArg2) Go: ___ Java: ___ JavaScript: vRet = new Function("return " + vFuncName)()(vArg1, vArg2) Kotlin: ___ PHP: $vRet = $vFuncName($vArg1, $vArg2) [also: call_user_func($vFuncName, $vArg1, $vArg2)] [also: call_user_func_array($vFuncName, [$vArg1, $vArg2])] Python: ___ [can use: vRet = globals()[vFuncName](vArg1, vArg2)] R: vRet = do.call(vFuncName, list(vArg1, vArg2)) Ruby: vRet = send(vFuncName, vArg1, vArg2) [also: vRet = oObj.send(vMtdName, vArg1, vArg2)] Rust: ___ Scala: ___ Swift: ___ UFL: ClassPropComputed [or ClassHasComputedProps][can create computed/dynamic properties that look like standard properties e.g. 'Obj.MyProp', 'Obj->MyProp' (based on getters/setters)][for languages that lack this, 'getXXX'/'setXXX' methods are a common alternative][see also: ClassFull] AutoHotkey: yes C++: no C#: yes Crystal: yes Excel: ___ Excel VBA: yes Go: no Java: no JavaScript: yes Kotlin: yes PHP: yes Python: yes R: no Ruby: yes Rust: no Scala: yes Swift: yes Section: Assign/Declare Single Variables UFL: VarDecl [declare a variable, without assigning a value] AutoHotkey: ___ [e.g. (within a function): local a] C++: int a; C#: int a; Crystal: a: Int32 Excel: ___ Excel VBA: Dim a As Integer Go: var a int Java: int a; JavaScript: var a; Kotlin: var a: Int PHP: ___ Python: ___ R: ___ [can use: vText = character()] [also: vNum = numeric()] [also (equivalent to numeric()): vNum = double()] Ruby: ___ Rust: let mut a: i32; Scala: var a: Int = _ [note: using '_' assigns the default value for that type] Swift: var a: Int UFL: VarDeclSetAuto [or VarDeclAssignAuto][assign variable (infer type)] AutoHotkey: vText := "abc" C++: auto vText = "abc" C#: var vText = "abc" Crystal: vText = "abc" Excel: ___ Excel VBA: vText = "abc" Go: vText := "abc" [also: var vText = "abc"] [MAJOR WARNING: if 'myvar declared and not used' then 'Go build failed.' (workaround: _ = myvar)] Java: var vText = "abc" JavaScript: vText = "abc" Kotlin: var vText = "abc" PHP: $vText = "abc" Python: vText = "abc" R: vText = "abc" Ruby: vText = "abc" Rust: let vText = "abc" [also: let mut vText = "abc"] Scala: var vText = "abc" Swift: var vText = "abc" UFL: VarDeclSet [or VarDeclAssign][assign variable (specify type)][or specify type hints/type annotations] AutoHotkey: ___ C++: std::string vText = "abc" C#: string vText = "abc" [note: 'String' also works] Crystal: vText: String = "abc" Excel: ___ Excel VBA: ___ [note: declaration/assignment keywords: Dim/ReDim/Set] Go: var vText string = "abc" Java: String vText = "abc" JavaScript: ___ Kotlin: var vText: String = "abc" PHP: ___ Python: vText: str = "abc" R: ___ Ruby: ___ Rust: let mut vText: String = "abc".to_string() [e.g. let vNum: i32 = 123] Scala: var vText = "abc" [also: val] Swift: var vText: String = "abc" UFL: VarSet [or VarAssign][assign a variable (update value)] AutoHotkey: a := 1 C++: a = 1; C#: a = 1; Crystal: a = 1 Excel: ___ Excel VBA: a = 1 Go: a = 1 Java: a = 1; JavaScript: a = 1; Kotlin: a = 1 PHP: $a = 1; Python: a = 1 R: a = 1 Ruby: a = 1 Rust: a = 1; Scala: a = 1 Swift: a = 1 UFL: (VarGetDynamic) [retrieve the value of a variable dynamically][workaround: use an array/map][see also: Array.Get/Map.Get/Object.Get/FuncCallDynamic] AutoHotkey: vValue := %vVarName% [also: vValue := %vVarNamePart1%%vVarNamePart2%] C++: ___ C#: ___ Crystal: ___ Excel: ___ [can use (specify a cell address dynamically): e.g. INDIRECT("A1")] [e.g. INDIRECT("A"&ROW())] Excel VBA: ___ Go: ___ Java: ___ JavaScript: ___ [can use: vValue = window[vVarName]] [also: vValue = window[vVarNamePart1 + vVarNamePart2]] Kotlin: ___ PHP: $vValue = $$vVarName [also: $vValue = ${$vVarNamePart1 . $vVarNamePart2}] Python: ___ [can use: vValue = globals()[vVarName]] [also: vValue = globals()[vVarNamePart1 + vVarNamePart2]] R: vValue = get(vVarName) [also: vValue = get(paste0(vVarNamePart1, vVarNamePart2))] Ruby: ___ Rust: ___ Scala: ___ Swift: ___ UFL: (VarSetDynamic) [set the value of a variable dynamically][workaround: use an array/map][see also: Array.Set/Map.Set/Object.Set/FuncCallDynamic] AutoHotkey: %vVarName% := vValue [also: %vVarNamePart1%%vVarNamePart2% := vValue] C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ Go: ___ Java: ___ JavaScript: ___ [can use: window[vVarName] = vValue] [also: window[vVarNamePart1 + vVarNamePart2] = vValue] Kotlin: ___ PHP: $$vVarName = $vValue [also: ${$vVarNamePart1 . $vVarNamePart2} = $vValue] Python: ___ [can use: globals()[vVarName] = vValue] [also: globals()[vVarNamePart1 + vVarNamePart2] = vValue] R: assign(vVarName, vValue) [also: assign(paste0(vVarNamePart1, vVarNamePart2), vValue)] Ruby: ___ Rust: ___ Scala: ___ Swift: ___ Section: Assign/Declare Multiple Variables UFL: VarDeclMult [declare multiple variables, without assigning values] AutoHotkey: ___ [e.g. (within a function): local a, b, c] C++: int a, b, c; C#: int a, b, c; Crystal: a: Int32; b: Int32; c: Int32 Excel: ___ Excel VBA: Dim a, b, c As Integer Go: var a, b, c int Java: int a, b, c; JavaScript: var a, b, c; Kotlin: var a:Int; var b:Int; var c:Int PHP: ___ Python: ___ R: ___ [can use: a = b = c = character()] [also: a = b = c = numeric()] [also (equivalent to numeric()): a = b = c = double()] Ruby: ___ Rust: let (mut a, mut b, mut c): (i32, i32, i32); Scala: var a, b, c: Int = _ [note: using '_' assigns the default value for that type] Swift: var a, b, c: Int UFL: VarDeclSetMult [or VarDeclAssignMult][declare and assign multiple variables] AutoHotkey: ___ [e.g. (within a function): local a:=1, b:=2, c:=3] C++: int a=1, b=2, c=3; C#: int a=1, b=2, c=3; [also (destructuring assignment): var (a, b, c) = (1, 2, 3);] Crystal: a: Int32 = 1; b: Int32 = 2; c: Int32 = 3 Excel: ___ Excel VBA: ___ Go: var a, b, c int = 1, 2, 3 [also (type omitted): var a, b, c = 1, 2, 3] [note: destructuring assignment] Java: int a=1, b=2, c=3; JavaScript: var a=1, b=2, c=3; [also (destructuring assignment): var [a, b, c] = [1, 2, 3];] Kotlin: var a=1; var b=2; var c=3 [also: var a:Int=1; var b:Int=2; var c:Int=3] [also (destructuring assignment) (destructuring declaration): var (a, b, c) = arrayOf(1, 2, 3)] [also: var (a:Int, b:Int, c:Int) = arrayOf(1, 2, 3)] PHP: ___ Python: ___ R: ___ Ruby: ___ Rust: let (mut a, mut b, mut c): (i32, i32, i32) = (1, 2, 3); Scala: var (a, b, c) = (1, 2, 3) Swift: var a=1, b=2, c=3 [also: var a:Int=1, b:Int=2, c:Int=3] [also (destructuring assignment): var (a, b, c) = (1, 2, 3)] UFL: VarSetMult [or VarAssignMult][assign multiple variables] AutoHotkey: a:=1, b:=2, c:=3 C++: a=1; b=2; c=3; [also: a=1, b=2, c=3;] C#: a=1; b=2; c=3; [also (destructuring assignment): (a, b, c) = (1, 2, 3);] Crystal: a=1; b=2; c=3 [also (destructuring assignment): a, b, c = 1, 2, 3] Excel: ___ Excel VBA: a = 1: b = 2: c = 3 Go: a, b, c = 1, 2, 3 [note: destructuring assignment] Java: a=1; b=2; c=3; JavaScript: a=1; b=2; c=3; [also: a=1, b=2, c=3;] [also (destructuring assignment): [a, b, c] = [1, 2, 3];] Kotlin: a=1; b=2; c=3 PHP: $a=1; $b=2; $c=3; [also (destructuring assignment): [$a, $b, $c] = [1, 2, 3];] Python: a=1; b=2; c=3 [also (destructuring assignment): a, b, c = 1, 2, 3] R: a=1; b=2; c=3 Ruby: a=1; b=2; c=3 [also (destructuring assignment): a, b, c = 1, 2, 3] Rust: a=1; b=2; c=3; [also (destructuring assignment): (a, b, c) = (1, 2, 3);] Scala: a=1; b=2; c=3 Swift: a=1; b=2; c=3 [also (destructuring assignment): (a, b, c) = (1, 2, 3)] UFL: VarDeclSetMultSame [or VarDeclAssignMultSame][declare and assign multiple variables, to the same value][see also: VarSetAndReadIf] AutoHotkey: ___ [WARNING (only creates static variable 'a'): static a := b := c := 123] C++: ___ C#: ___ Crystal: ___ Excel: ___ Excel VBA: ___ Go: ___ [can use: var a, b, c int = 123, 123, 123] [also (type omitted): var a, b, c = 123, 123, 123] [note: destructuring assignment] Java: ___ JavaScript: var a = b = c = 123; [also (destructuring assignment): var [a, b, c] = Array(3).fill(123);] Kotlin: var (a, b, c) = IntArray(3){123} [also: var (a, b, c) = (123, 123, 123)] [note (both): destructuring assignment (destructuring declaration)] [also: var (a:Int, b:Int, c:Int) = IntArray(3){123}] [also: var (a:Int, b:Int, c:Int) = arrayOf(123, 123, 123)] PHP: ___ Python: ___ R: ___ Ruby: ___ Rust: let [mut a, mut b, mut c] = [123; 3]; [note: destructuring assignment] Scala: var a, b, c = 123 Swift: ___ [can use: var (a, b, c) = (123, 123, 123)] [note: destructuring assignment] UFL: VarSetMultSame [or VarAssignMultSame][assign multiple variables, to the same value][see also: VarSetAndReadIf] AutoHotkey: a := b := c := 123 C++: a = b = c = 123; C#: a = b = c = 123; Crystal: a = b = c = 123 [also (destructuring assignment): a, b, c = Array.new(3, 123)] Excel: ___ Excel VBA: ___ Go: ___ [can use: a, b, c = 123, 123, 123] [note: destructuring assignment] Java: a = b = c = 123; JavaScript: a = b = c = 123; [also (destructuring assignment): [a, b, c] = Array(3).fill(123);] Kotlin: ___ [note: can do destructuring declaration, but not destructuring assignment] PHP: $a = $b = $c = 123; [also (destructuring assignment): [$a, $b, $c] = array_fill(0, 3, 123);] Python: a = b = c = 123 [also (destructuring assignment): a, b, c = [123] * 3] R: a = b = c = 123 Ruby: a = b = c = 123 [also (destructuring assignment): a, b, c = Array.new(3, 123)] Rust: [a, b, c] = [123; 3]; [note: destructuring assignment] Scala: ___ Swift: ___ [can use: (a, b, c) = (123, 123, 123)] [note: destructuring assignment] UFL: VarSetAndReadIf [or VarAssignAndReadIf][update value, and read that new value, in a single statement, in an if-statement][note: it is generally best to avoid such code since 'if (a = b)' is usually a typo, with 'if (a == b)' being intended, however, sometimes code is much shorter/cleaner if this idiom is used (e.g. multiple such statements in one place)][see also: VarSetMultSame/VarDeclSetMultSame] AutoHotkey: if (vBool := true) C++: if (vBool = true) C#: if (vBool = true) Crystal: if (vBool = true) Excel: ___ Excel VBA: ___ Go: ___ Java: if (vBool = true) JavaScript: if (vBool = true) Kotlin: ___ PHP: if ($vBool = true) Python: if (vBool := True): [note: doesn't work: if (vBool = True):] [this is the source of the walrus operator debates] R: ___ Ruby: if (vBool = true) Rust: ___ Scala: ___ Swift: ___ Section: File Extensions UFL: FileExtension [file extension] AutoHotkey: ahk C++: cpp [also: c/cxx/cc] [also (header files): h/hpp/hxx/hh] C#: cs Crystal: cr Excel: xlsx [also (older Excel versions): xls] Excel VBA: xlsm [also (older Excel versions): xls] Go: go Java: java JavaScript: js [also (related extensions): htm/html/css] Kotlin: kt PHP: php Python: py R: r Ruby: rb Rust: rs Scala: scala, sc Swift: swift UFL: (ArtworkLogo) [programming language logo] AutoHotkey: white 'H', green background C++: white 'C++', blue hexagon C#: white 'C#', purple hexagon Crystal: black hexagon, white equilateral triangle Excel: white 'X', green background Excel VBA: 4 boxes, orange/blue/yellow/purple (VBA logo) Go: light blue 'GO', with light blue speed marks on left Java: blue cup of coffee, orange steam JavaScript: black 'JS', yellow background Kotlin: stylised purple 'K' [note: previous logo: stylised K with blue/purple, and orange(/magenta) stripe] PHP: black 'php', purple background (purple oval) Python: blue snake, yellow snake R: blue 'R', grey unfilled oval Ruby: red ruby (shape is similar to (not necessarily identical to) an octagon diamond) Rust: black 'R' inside a cog Scala: red spiral staircase, 3 tiers (based on staircase at the EPFL, Lausanne, Switzerland) Swift: white bird facing bottom-right corner, orange background UFL: (ArtworkMascot) [programming language mascot] AutoHotkey: ___ C++: ___ [note: not Keith (brown with lighter bits, rat)] C#: ___ [note: formerly Andy (human, C-shaped tie, '#' pocket)] [also: DotNet Bot (purple, spherical, robot)] Crystal: crow (black) Excel: ___ Excel VBA: ___ Go: Go Gopher (light blue) Java: Duke (black head/arms, white body, round red nose, shaped like an upside-down tooth) JavaScript: ___ Kotlin: Kodee (purple, robot, cat-like, screen-like) PHP: ElePHPant (purple, elephant) Python: ___ R: ___ Ruby: ___ Rust: Ferris (red/orange, crab) Scala: ___ Swift: ___ Section: Concatenate Strings/Integers UFL: StrIntConcat [can concatenate string and int directly?] AutoHotkey: yes [e.g. vText vNum] [e.g. vNum vText] [also: vText . vNum] [also: vNum . vText] C++: no [can use: vText + std::to_string(vNum)] [can use: std::to_string(vNum) + vText] C#: yes [e.g. vText + vNum] [e.g. vNum + vText] Crystal: no [can use: vText + vNum.to_s] [also: vNum.to_s + vText] Excel: yes [e.g. =A1&B1] [e.g. =B1&A1] Excel VBA: yes [e.g. vText & vNum] [e.g. vNum & vText] Go: no [can use: vText + strconv.Itoa(vNum)] [also: strconv.Itoa(vNum) + vText] [requires: import "strconv"] Java: yes [e.g. vText + vNum] [e.g. vNum + vText] JavaScript: yes [e.g. vText + vNum] [e.g. vNum + vText] Kotlin: depends on order [e.g. vText + vNum] [can use: "" + vNum + vText] PHP: yes [e.g. $vText . $vNum] [e.g. $vNum . $vText] Python: no [can use: vText + str(vNum)] [also: str(vNum) + vText] R: yes [e.g. paste0(vText, vNum)] [e.g. paste0(vNum, vText)] Ruby: no [can use: vText + vNum.to_s] [also: vNum.to_s + vText] Rust: no [can use: format!("{}{}", vText, vNum)] [can use: format!("{}{}", vNum, vText)] Scala: yes [e.g. vText + vNum] [e.g. s"$vNum$vText"] [e.g. s"${vNum}${vText}"] [deprecated: vNum + vText] Swift: no [can use: vText + String(vNum)] [can use: String(vNum) + vText] UFL: PrintKeyValueConcat [or PrintConcat/PrintTwoValuesConcat][note: will work for (at least) strings and ints][see also: StrIntConcat/Array.PrintWithIndex/Map.Print] AutoHotkey: MsgBox(vKey ": " vValue) C++: std::cout << vKey << ": " << vValue << "\n" [WARNING: std::to_string fails on std::string itself] C#: Console.WriteLine(vKey + ": " + vValue) [also: Console.WriteLine(oEntry.Key + ": " + oEntry.Value)] Crystal: p vKey.to_s + ": " + vValue.to_s Excel: =A1&": "&B1 Excel VBA: Debug.Print vKey & ": " & vValue Go: fmt.Printf("%v: %v\n", vKey, vValue) Java: System.out.println(vKey + ": " + vValue) [also: System.out.println(oEntry.getKey() + ": " + oEntry.getValue())] JavaScript: console.log(vKey + ": " + vValue) Kotlin: println("" + vKey + ": " + vValue) PHP: var_dump($vKey . ": " . $vValue) Python: print(str(vKey) + ": " + str(vValue)) R: print(paste(vKey, vValue, sep=": ")) [also: print(paste(vKey, oMap[[vKey]], sep=": "))] Ruby: p vKey.to_s + ": " + vValue.to_s Rust: println!("{}: {}", vKey, vValue) Scala: println(s"$vKey: $vValue") [also: println(s"${vKey}: ${vValue}")] Swift: print(String(vKey) + ": " + String(vValue)) [also: print(String(describing:vKey) + ": " + String(describing:vValue))] UFL: PrintKeyValueAsPair [or PrintAsPair/PrintAsTuple/PrintTwoValues][note: will work for (at least) strings and ints][see also: StrIntConcat/Array.PrintWithIndex/Map.Print/Tuple.NewDemo/MakeFuncForEachWithIndexDemoPrintIntStr] AutoHotkey: ___ [can use: MsgBox(vKey ": " vValue)] C++: std::apply([](auto&&... oArgs) {((std::cout << oArgs << ", "), ...);}, std::make_pair(vKey, vValue)) [note: prints trailing separator] [afterwards: std::cout << "\n"] [also: can replace 'std::make_pair' with 'std::make_tuple'] [requires (pair): #include <utility>] [requires (tuple): #include <tuple>] C#: Console.WriteLine((vKey, vValue)) [also: Console.WriteLine(new KeyValuePair<string,int>(vKey, vValue))] [requires (KeyValuePair): using System.Collections.Generic] Crystal: p ({vKey, vValue}) Excel: ___ [can use: =A1&": "&B1] Excel VBA: ___ [can use: Debug.Print vKey & ": " & vValue] Go: fmt.Println(vKey, vValue) [also: fmt.Println([...]interface{}{vKey, vValue})] Java: System.out.println(List.of(vKey, vValue)) [also: System.out.println(vKey + ": " + vValue)] [also: System.out.println(Map.entry(vKey, vValue))] [also: System.out.println(new AbstractMap.SimpleEntry<>(vKey, vValue))] [requires (all): import java.util.*] JavaScript: console.log([vKey, vValue]) [also: console.log(vKey, vValue)] [also: console.log(vKey + ": " + vValue)] Kotlin: println(Pair(vKey, vValue)) [also: println(listOf(vKey, vValue))] PHP: var_dump([$vKey, $vValue]) [also: var_dump($vKey . ": " . $vValue)] Python: print((vKey, vValue)) [also: print([vKey, vValue])] [also: print(vKey, vValue)] R: print(list(vKey, vValue)) [also: print(c(vKey, vValue))] [note: list(), unlike c(), preserves types, but uses multiple lines] [also: print(unlist(list(vKey, vValue)))] Ruby: p [vKey, vValue] Rust: println!("{:?}", (vKey, vValue)) Scala: println((vKey, vValue)) Swift: print((vKey, vValue)) [also: print(vKey, vValue)] UFL: PrintMultDemo [or PrintWithNewLineMultDemo/PrintThreeValuesDemo][print multiple values of different types on one line] AutoHotkey: ___ [can use: MsgBox(Format("{} {} {}", "a", 1, 1.1))] C++: ___ [can use: std::apply([](auto&&... oArgs) {((std::cout << oArgs << ", "), ...);}, std::make_tuple("a", 1, 1.1))] [note: prints trailing separator] [afterwards: std::cout << "\n"] [requires (tuple): #include <tuple>] C#: Console.WriteLine(("a", 1, 1.1)) Crystal: p ({"a", 1, 1.1}) [also (prints each on a separate line): p "a", 1, 1.1] Excel: ___ [can use: =A1&" "&B1&" "&C1] Excel VBA: Debug.Print Join(Array("a", 1, 1.1), " ") Go: fmt.Println("a", 1, 1.1) Java: System.out.println(List.of("a", 1, 1.1)) [requires: import java.util.*] JavaScript: console.log("a", 1, 1.1) Kotlin: println(listOf("a", 1, 1.1)) PHP: var_dump(["a", 1, 1.1]) [also: var_dump(implode(" ", ["a", 1, 1.1]))] [also (prints each on a separate line): var_dump("a", 1, 1.1)] Python: print("a", 1, 1.1) R: print(c("a", 1, 1.1)) [also: print(unlist(list("a", 1, 1.1)))] [also (prints across multiple lines, preserves types): print(list("a", 1, 1.1))] Ruby: p ["a", 1, 1.1] [also (prints each on a separate line): p "a", 1, 1.1] Rust: println!("{:?}", ("a", 1, 1.1)) [also: println!("{} {} {}", "a", 1, 1.1)] Scala: println(("a", 1, 1.1)) Swift: print("a", 1, 1.1) Section: Multi-Line Strings UFL: StrMultiLine [multi-line strings][WARNING: some approaches crop whitespace (e.g. all whitespace, e.g. some whitespace based on indentation)]
C: concatenate operator J: join method/function X: other [AutoHotkey] vTextC1 := "LINE 1`n" . "LINE 2`n" . "LINE 3" vTextC2 := "LINE 1`n" . "LINE 2`n" . "LINE 3" vTextX1 := " ;continuation section ( LINE 1 LINE 2 LINE 3 )" vTextX2 := " ;continuation section (Join`r`n LINE 1 LINE 2 LINE 3 )" MsgBox(vTextC1) MsgBox(vTextC2) MsgBox(vTextX1) MsgBox(vTextX2) [C++] //doesn't work: //std::string vTextC1 = "LINE 1\n" //+ "LINE 2\n" //+ "LINE 3"; //doesn't work: //std::string vTextC2 = "LINE 1\n" + //"LINE 2\n" + //"LINE 3"; //string literal concatenation (no operators needed): std::string vTextC1 = "LINE 1\n" "LINE 2\n" "LINE 3"; std::string vTextX1 = "LINE 1\n\ LINE 2\n\ LINE 3"; std::string vTextX2 = R"(LINE 1 LINE 2 LINE 3)"; std::cout << vTextC1 << "\n"; std::cout << vTextX1 << "\n"; std::cout << vTextX2 << "\n"; [C#] string vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; string vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; string vTextJ1 = String.Join("\n" , "LINE 1" , "LINE 2" , "LINE 3" ); string vTextJ2 = String.Join("\n", "LINE 1", "LINE 2", "LINE 3" ); string vTextX1 = @"LINE 1 LINE 2 LINE 3"; Console.WriteLine(vTextC1); Console.WriteLine(vTextC2); Console.WriteLine(vTextJ1); Console.WriteLine(vTextJ2); Console.WriteLine(vTextX1); [Crystal] vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3" vTextJ1 = [ "LINE 1", "LINE 2", "LINE 3" ].join("\n") vTextX1 = "LINE 1 LINE 2 LINE 3" vTextX2 = "LINE 1\n\ LINE 2\n\ LINE 3" vTextX3 = "LINE 1\n" \ "LINE 2\n" \ "LINE 3" vTextX4 = <<-MYTEXT LINE 1 LINE 2 LINE 3 MYTEXT vTextX5 = %q(LINE 1 LINE 2 LINE 3 ) puts vTextC1 puts vTextJ1 puts vTextX1 puts vTextX2 puts vTextX3 puts vTextX4 puts vTextX5 [Excel] [Excel VBA] vTextC1 = "LINE 1" & vbLf & _ "LINE 2" & vbLf & _ "LINE 3" vTextC2 = "LINE 1" & vbLf _ & "LINE 2" & vbLf _ & "LINE 3" Debug.Print vTextC1 Debug.Print vTextC2 [Go] //WARNING: Golang online compiler auto-indents code: //WARNING: Golang online compiler sometimes enforces a space after the comment symbol ('//'): vTextC1 := "LINE 1\n" + "LINE 2\n" + "LINE 3" vTextJ1 := strings.Join([]string{"LINE 1", "LINE 2", "LINE 3"}, "\n") //WARNING: preserves leading whitespace: vTextX1 := `LINE 1 LINE 2 LINE 3 ` fmt.Println(vTextC1) fmt.Println(vTextJ1) fmt.Println(vTextX1) [Java] String vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; String vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; String vTextJ1 = String.join("\n" , "LINE 1" , "LINE 2" , "LINE 3" ); String vTextJ2 = String.join("\n", "LINE 1", "LINE 2", "LINE 3" ); String vTextX1 = """ LINE 1 LINE 2 LINE 3"""; System.out.println(vTextC1); System.out.println(vTextC2); System.out.println(vTextJ1); System.out.println(vTextJ2); System.out.println(vTextX1); [JavaScript] vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3"; vTextJ1 = ["LINE 1" , "LINE 2" , "LINE 3" ].join("\n") vTextJ2 = [ "LINE 1", "LINE 2", "LINE 3" ].join("\n") vTextX1 = "LINE 1\n\ LINE 2\n\ LINE 3"; vTextX2 = `LINE 1 LINE 2 LINE 3`; console.log(vTextC1); console.log(vTextC2); console.log(vTextJ1); console.log(vTextJ2); console.log(vTextX1); console.log(vTextX2); [Kotlin] //doesn't work: //vTextC1 = "LINE 1\n" //+ "LINE 2\n" //+ "LINE 3" var vTextC1 = ("LINE 1\n" + "LINE 2\n" + "LINE 3") var vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3" var vTextJ1 = arrayOf("LINE 1" , "LINE 2" , "LINE 3" ).joinToString("\n") var vTextJ2 = arrayOf( "LINE 1", "LINE 2", "LINE 3" ).joinToString("\n") var vTextX1 = """LINE 1 LINE 2 LINE 3""" println(vTextC1) println(vTextC2) println(vTextJ1) println(vTextJ2) println(vTextX1) [PHP] $vTextC1 = "LINE 1\n" . "LINE 2\n" . "LINE 3"; $vTextC2 = "LINE 1\n" . "LINE 2\n" . "LINE 3"; $vTextJ1 = implode("\n", ["LINE 1" , "LINE 2" , "LINE 3" ]); $vTextJ2 = implode("\n", [ "LINE 1", "LINE 2", "LINE 3" ]); $vTextX1 = "LINE 1 LINE 2 LINE 3"; $vTextX1 = str_replace("\r", "", $vTextX1); //e.g. Nowdoc string: $vTextX2 = <<<'END' LINE 1 LINE 2 LINE 3 END; echo $vTextC1 . "\n"; echo $vTextC2 . "\n"; echo $vTextJ1 . "\n"; echo $vTextJ2 . "\n"; echo $vTextX1 . "\n"; echo $vTextX2 . "\n"; [Python] #doesn't work: #vTextC1 = "LINE 1\n" #+ "LINE 2\n" #+ "LINE 3" #doesn't work: #vTextC2 = "LINE 1\n" + #"LINE 2\n" + #"LINE 3" vTextC1 = ("LINE 1\n" + "LINE 2\n" + "LINE 3") vTextC2 = ("LINE 1\n" + "LINE 2\n" + "LINE 3") vTextJ1 = "\n".join(["LINE 1" , "LINE 2" , "LINE 3" ]) vTextJ2 = "\n".join([ "LINE 1", "LINE 2", "LINE 3" ]) vTextX1 = "LINE 1\n" \ + "LINE 2\n" \ + "LINE 3" vTextX2 = """LINE 1 LINE 2 LINE 3""" print(vTextC1) print(vTextC2) print(vTextJ1) print(vTextJ2) print(vTextX1) print(vTextX2) [R] vTextJ1 = paste("LINE 1" , "LINE 2" , "LINE 3" , sep="\n") vTextJ2 = paste("LINE 1", "LINE 2", "LINE 3", sep="\n") vTextX1 = "LINE 1 LINE 2 LINE 3" cat(vTextJ1); cat("\n") cat(vTextJ2); cat("\n") cat(vTextX1); cat("\n") [Ruby] vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3" vTextJ1 = [ "LINE 1", "LINE 2", "LINE 3" ].join("\n") vTextX1 = "LINE 1 LINE 2 LINE 3" vTextX2 = "LINE 1\n\ LINE 2\n\ LINE 3" vTextX3 = "LINE 1\n" \ "LINE 2\n" \ "LINE 3" vTextX4 = <<-MYTEXT LINE 1 LINE 2 LINE 3 MYTEXT vTextX5 = <<~MYTEXT LINE 1 LINE 2 LINE 3 MYTEXT vTextX6 = %q(LINE 1 LINE 2 LINE 3 ) puts vTextC1 puts vTextJ1 puts vTextX1 puts vTextX2 puts vTextX3 puts vTextX4 puts vTextX5 puts vTextX6 [Rust] let vTextX1 = "LINE 1 LINE 2 LINE 3"; let vTextX2 = "LINE 1\n\ LINE 2\n\ LINE 3"; let vTextX3 = concat!("LINE 1\n" , "LINE 2\n" , "LINE 3" ); println!("{}", vTextX1); println!("{}", vTextX2); println!("{}", vTextX3); [Scala] var vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3" var vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3" var vTextJ1 = String.join("\n" , "LINE 1" , "LINE 2" , "LINE 3" ) var vTextJ2 = String.join("\n", "LINE 1", "LINE 2", "LINE 3" ) var vTextX1 = """ LINE 1 LINE 2 LINE 3""" println(vTextC1) println(vTextC2) println(vTextJ1) println(vTextJ2) println(vTextX1) [Swift] var vTextC1 = "LINE 1\n" + "LINE 2\n" + "LINE 3" var vTextC2 = "LINE 1\n" + "LINE 2\n" + "LINE 3" var vTextJ1 = ["LINE 1" , "LINE 2" , "LINE 3" ].joined(separator:"\n") var vTextJ2 = [ "LINE 1", "LINE 2", "LINE 3" ].joined(separator:"\n") var vTextX1 = """ LINE 1 LINE 2 LINE 3 """ print(vTextC1) print(vTextC2) print(vTextJ1) print(vTextJ2) print(vTextX1)
Section: Sleep/Tick Counts (benchmark tests) UFL: BenchmarkTest [benchmark test][sleep and compare tick counts][see also: TickCount]
[AutoHotkey] vTick1 := A_TickCount Sleep(123) vTick2 := A_TickCount vDurationMSec := vTick2 - vTick1 MsgBox(vDurationMSec . " msec") [C++] #include <iostream> #include <chrono> #include <thread> auto vTick1 = std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(123)); auto vTick2 = std::chrono::high_resolution_clock::now(); auto vDurationMSec = std::chrono::duration_cast<std::chrono::milliseconds>(vTick2-vTick1).count(); std::cout << std::to_string(vDurationMSec) + " msec" << "\n"; [C#] var vTick1 = DateTime.Now; System.Threading.Thread.Sleep(123); var vTick2 = DateTime.Now; var vDurationMSec = (vTick2-vTick1).TotalMilliseconds; Console.WriteLine(vDurationMSec + " msec"); [Crystal] oTick1 = Time.monotonic sleep 123/1000 oTick2 = Time.monotonic vDurationMSec = (oTick2-oTick1).total_milliseconds puts vDurationMSec.to_s + " msec" [Excel] [Excel VBA] #If VBA7 Then Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) #Else Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) #End If vTick1 = Timer 'Application.Wait (Now + TimeValue("0:00:01")) Sleep (123) 'Winapi function kernel32\Sleep, more accurate than Application.Wait vTick2 = Timer vDurationMSec = Int((vTick2 - vTick1) * 1000) Debug.Print (vDurationMSec & " msec") [Go] //requires: import "time" //requires: import "strconv" oTime1 := time.Now() time.Sleep(123 * time.Millisecond) oTime2 := time.Now() vDurationMSec := oTime2.Sub(oTime1).Milliseconds() fmt.Println(strconv.Itoa(int(vDurationMSec)) + " msec") [Java] var vTick1 = System.currentTimeMillis(); try { Thread.sleep(123); } catch (Exception e) { } var vTick2 = System.currentTimeMillis(); var vDurationMSec = vTick2 - vTick1; System.out.println(vDurationMSec + " msec"); [JavaScript] vTick1 = performance.now(); var vDate = 123 + new Date().getTime(); while (new Date() < vDate) {} //noop vTick2 = performance.now(); vDurationMSec = vTick2 - vTick1; console.log(vDurationMSec + " msec"); [Kotlin] var vTick1 = System.currentTimeMillis() Thread.sleep(123) var vTick2 = System.currentTimeMillis() var vDurationMSec = vTick2 - vTick1 println("" + vDurationMSec + " msec") [PHP] $vTick1 = microtime(true); usleep(123*1000); //note: usleep uses microseconds (1000 microseconds = 1 millisecond) $vTick2 = microtime(true); $vDurationMSec = intval(($vTick2-$vTick1)*1000); var_export($vDurationMSec . " msec"); [Python] import time vTick1 = time.time() time.sleep(123/1000) vTick2 = time.time() vDurationMSec = int((vTick2-vTick1)*1000) print(str(vDurationMSec) + " msec") [R] oTime1 = Sys.time() Sys.sleep(123/1000) oTime2 = Sys.time() vDurationMSec = as.numeric(oTime2-oTime1) * 1000 print(sprintf("%0.4f msec", vDurationMSec)) #print(paste(vDurationMSec, "msec")) #for more decimal places [Ruby] oTick1 = Time.now sleep 123/1000.0 oTick2 = Time.now vDurationMSec = (oTick2-oTick1) * 1000 puts vDurationMSec.to_s + " msec" [Rust] use std::{thread, time}; let oDate1 = time::SystemTime::now(); thread::sleep(time::Duration::from_millis(123)); let oDate2 = time::SystemTime::now(); let vDurationMSec = oDate2.duration_since(oDate1).unwrap().as_millis(); println!("{}{}", vDurationMSec, " msec"); [Scala] var vTick1 = System.currentTimeMillis() Thread.sleep(123) var vTick2 = System.currentTimeMillis() var vDurationMSec = vTick2 - vTick1 println(s"$vDurationMSec msec") //println(s"${vDurationMSec} msec") //equivalent to line above [Swift] import Foundation var oTick1 = DispatchTime.now() usleep(123*1000) var oTick2 = DispatchTime.now() var vDurationNSec = oTick2.uptimeNanoseconds - oTick1.uptimeNanoseconds var vDurationMSec = vDurationNSec / 1000000 print(String(vDurationMSec) + " msec")
Function Examples: Add/Sum UFL: FuncDemoAdd/FuncDemoSum [custom 'MyAdd' and 'MySum' functions][and 'MyAddAnon' anonymous function][and print example values][see also: OpSpreadReceiveDemo]
[AutoHotkey] MyAdd(vNum1, vNum2) { return vNum1 + vNum2 } ;MyAddAnon := (vNum1, vNum2) => vNum1 + vNum2 ;MsgBox(MyAdd(1,2)) ;MsgBox(MyAddAnon(1,2)) MySum(oParams*) { vSum := 0 for _, vValue in oParams vSum += vValue return vSum } [C++] int MyAdd(int vNum1, int vNum2) { return vNum1 + vNum2; } //auto MyAddAnon = [](int vNum1, int vNum2) {return vNum1+vNum2;}; //std::cout << MyAdd(1,2) << "\n"; //std::cout << MyAddAnon(1,2) << "\n"; //WARNING: C++ doesn't have variadic functions, so accept a vector instead: int MySum(std::vector<int> oVec) { int vSum = 0; for (const auto& vValue : oVec) vSum += vValue; return vSum; } [C#] public static int MyAdd(int vNum1, int vNum2) { return vNum1 + vNum2; } //Func<int,int,int> MyAddAnon = (vNum1, vNum2) => vNum1 + vNum2; //Console.WriteLine(MyAdd(1,2)); //Console.WriteLine(MyAddAnon(1,2)); public static int MySum(params int[] oParams) { int vSum = 0; for (var i=0; i<oParams.Length; i++) vSum += oParams[i]; return vSum; } [Crystal] #MAJOR WARNING: Crystal forces function names (method names) to start with a lower-case letter: #WARNING: if try to define a function whose name starts with an upper-case letter, the error message is: 'Error: unexpected token: "("' def myAdd(vNum1, vNum2) return vNum1 + vNum2 end #MyAddAnon = ->(vNum1: Int32,vNum2: Int32) {vNum1+vNum2} #p myAdd(1,2) #p MyAddAnon.call(1,2) def mySum(*oParams) vSum = 0 oParams.each do |vValue| vSum += vValue end return vSum end [Excel] 'note: this can be used in sheet formulae (it accepts 2 parameters): 'e.g. =MyAdd(A1,A2) Function MyAdd(vNum1, vNum2) MyAdd = vNum1 + vNum2 End Function 'note: this can be used in sheet formulae (it accepts 1 parameter, which can be a range) (it does not use ParamArray): 'e.g. =MySum(A1:A10) Function MySum(oParams) vSum = 0 For Each vValue In oParams vSum = vSum + vValue Next MySum = vSum End Function [Excel VBA] Function MyAdd(vNum1, vNum2) MyAdd = vNum1 + vNum2 End Function 'Debug.Print MyAdd(1,2) Function MySum(ParamArray oParams()) vSum = 0 For Each vValue In oParams vSum = vSum + vValue Next MySum = vSum End Function [Go] // MAJOR WARNING: the Go Playground demands K&R style indentation: // WARNING: the Go Playground may add whitespace before and after '//': func MyAdd(vNum1 int, vNum2 int) int { return vNum1 + vNum2 } //MyAddAnon := func(vNum1, vNum2 int) int { return vNum1 + vNum2 } //fmt.Println(MyAdd(1,2)) //fmt.Println(MyAddAnon(1,2)) func MySum(oParams ...int) int { vSum := 0 for _, vValue := range oParams { vSum += vValue } return vSum } [Java] static int MyAdd(int vNum1, int vNum2) { return vNum1 + vNum2; } //import java.util.function.*; //BinaryOperator<Integer> MyAddAnon = (vNum1, vNum2) -> vNum1 + vNum2; //System.out.println(MyAdd(1,2)); //System.out.println(MyAddAnon.apply(1,2)); static int MySum(int... oParams) { int vSum = 0; for (var i=0; i<oParams.length; i++) vSum += oParams[i]; return vSum; } [JavaScript] function MyAdd(vNum1, vNum2) { return vNum1 + vNum2; } //MyAddAnon = (vNum1, vNum2) => vNum1 + vNum2; //console.log(MyAdd(1,2)); //console.log(MyAddAnon(1,2)); function MySum(...oParams) { vSum = 0; for (var i=0; i<oParams.length; i++) vSum += oParams[i]; return vSum; } [Kotlin] fun MyAdd(vNum1: Int, vNum2: Int) : Int { return vNum1 + vNum2 } //var MyAddAnon = {vNum1:Int, vNum2:Int -> vNum1+vNum2} //println(MyAdd(1,2)) //println(MyAddAnon(1,2)) fun MySum(vararg oParams: Int) : Int { var vSum = 0 for (i in 0 until oParams.size) vSum += oParams[i] return vSum } [PHP] function MyAdd($vNum1, $vNum2) { return $vNum1 + $vNum2; } //$MyAddAnon = fn($vNum1, $vNum2) => $vNum1 + $vNum2; //var_dump(MyAdd(1,2)); //var_dump($MyAddAnon(1,2)); function MySum(...$oParams) { $vSum = 0; foreach($oParams as $vValue) $vSum += $vValue; return $vSum; } [Python] def MyAdd(vNum1, vNum2): return vNum1 + vNum2 #MyAddAnon = lambda vNum1, vNum2 : vNum1 + vNum2 #print(MyAdd(1,2)) #print(MyAddAnon(1,2)) def MySum(*oParams): vSum = 0 for vValue in oParams: vSum += vValue return vSum [R] #note: this is creating an anonymous function, storing it as a variable: #note: both approaches are doing this, 'function' and '\': #note: in R, '<-' is often used for assignment, this website consistently uses '=': MyAdd = function(vNum1, vNum2) { return (vNum1 + vNum2) } #MyAddAnon = \(vNum1,vNum2) vNum1+vNum2 #print(MyAdd(1,2)) #print(MyAddAnon(1,2)) MySum = function(...) { oParams = list(...) vSum = 0 for (vValue in oParams) vSum = vSum + vValue return (vSum) } [Ruby] def MyAdd(vNum1, vNum2) return vNum1 + vNum2 end #MyAddAnon = ->(vNum1,vNum2) {vNum1+vNum2} #p MyAdd(1,2) #p MyAddAnon.call(1,2) def MySum(*oParams) vSum = 0 for vValue in oParams vSum += vValue end return vSum end [Rust] fn MyAdd(vNum1: i32, vNum2: i32) -> i32 { return vNum1 + vNum2; } //fn MyAddAnon (vNum1:i32, vNum2:i32) -> i32{vNum1+vNum2} //println!("{}", MyAdd(1,2)); //println!("{}", MyAddAnon(1,2)); //note: using implicit return syntax: //'The final expression in the function will be used as [the] return value.' fn MyAddAlt(vNum1: i32, vNum2: i32) -> i32 { vNum1 + vNum2 } //WARNING: Rust doesn't have variadic functions, so accept a vector instead: fn MySum(oVec: Vec<i32>) -> i32 { let mut vSum = 0; for vValue in oVec { vSum += vValue } return vSum; } [Scala] def MyAdd(vNum1:Int, vNum2:Int) : Int = { return vNum1 + vNum2 } //def MyAddAnon(vNum1:Int, vNum2:Int): Int = vNum1 + vNum2 //println(MyAdd(1,2)) //println(MyAddAnon(1,2)) def MySum(oParams: Int*) : Int = { var vSum = 0 for(vValue <- oParams) vSum += vValue return vSum } [Swift] func MyAdd(_ vNum1: Int, _ vNum2: Int) -> Int { return vNum1 + vNum2 } //var MyAddAnon: (_ vNum1:Int, _ vNum2:Int) -> Int = {vNum1,vNum2 in vNum1+vNum2} //print(MyAdd(1,2)) //print(MyAddAnon(1,2)) func MySum(_ oParams: Int...) -> Int { var vSum = 0 for vValue in oParams { vSum += vValue } return vSum }
Function Examples: FillParamVar/FillParamFixed UFL: FuncDemoFillParamVar/FuncDemoFillParamFixed [custom 'MulVar'/'MulFixed'/'MulMake' anonymous functions][demonstrates filling a function (or code equivalent to a function) (e.g. 'Mul': a * b) with a parameter that is either fixed (e.g. 'MulFixed'), or with a parameter that can be varied after the function is created (e.g. 'MulVar')][WARNING: 'MulVar' may be something that a user accidentally creates, when 'MulFixed' was intended, so the 'MulVar' code can be seen either as a how-to example or as a gotcha to avoid]
[AutoHotkey] ;MulVar: vMult := 5 oFuncMulVar := vNum => vMult * vNum MsgBox(oFuncMulVar(10)) ;50 vMult := 10 MsgBox(oFuncMulVar(10)) ;100 ;MulFixed: oFuncMulMake := vMult => (vNum => vMult * vNum) vMult := 5 oFuncMulFixed := oFuncMulMake(vMult) MsgBox(oFuncMulFixed(10)) ;50 vMult := 10 MsgBox(oFuncMulFixed(10)) ;50 [C++] //MulVar: int vMult = 5; auto oFuncMulVar = [&vMult](int vNum) {return vMult * vNum;}; std::cout << oFuncMulVar(10) << "\n"; //50 vMult = 10; std::cout << oFuncMulVar(10) << "\n"; //100 //MulFixed: auto oFuncMulMake = [](int vMult) {return [vMult](int vNum) {return vMult * vNum;};}; int vMult = 5; auto oFuncMulFixed = oFuncMulMake(vMult); std::cout << oFuncMulFixed(10) << "\n"; //50 vMult = 10; std::cout << oFuncMulFixed(10) << "\n"; //50 [C#] //MulVar: int vMult = 5; Func<int,int> oFuncMulVar = vNum => vMult * vNum; Console.WriteLine(oFuncMulVar(10)); //50 vMult = 10; Console.WriteLine(oFuncMulVar(10)); //100 //MulFixed: Func<int,Func<int,int>> oFuncMulMake = vMult => (vNum => vMult * vNum); int vMult = 5; var oFuncMulFixed = oFuncMulMake(vMult); Console.WriteLine(oFuncMulFixed(10)); //50 vMult = 10; Console.WriteLine(oFuncMulFixed(10)); //50 [Crystal] #MulVar: vMult = 5 oFuncMulVar = ->(vNum: Int32) {vMult * vNum} p oFuncMulVar.call(10) #50 vMult = 10 p oFuncMulVar.call(10) #100 #MulFixed: oFuncMulMake = ->(vMult: Int32) {->(vNum: Int32) {vMult * vNum}} vMult = 5 oFuncMulFixed = oFuncMulMake.call(vMult) p oFuncMulFixed.call(10) #50 vMult = 10 p oFuncMulFixed.call(10) #50 [Excel] [Excel VBA] [Go] //MulVar: vMult := 5 oFuncMulVar := func(vNum int) int { return vMult * vNum } fmt.Println(oFuncMulVar(10)) //50 vMult = 10 fmt.Println(oFuncMulVar(10)) //100 //MulFixed: oFuncMulMake := func(vMult int) func(int) int { return func(vNum int) int { return vMult * vNum } } vMult := 5 oFuncMulFixed := oFuncMulMake(vMult) fmt.Println(oFuncMulFixed(10)) //50 vMult = 10 fmt.Println(oFuncMulFixed(10)) //50 [Java] //requires: import java.util.function.*; //MulVar: //note: we use an int array (oMult), instead of an int (vMult): //if we used an int, we'd get an error: 'local variables referenced from a lambda expression must be final or effectively final': //i.e. we modify an array member, instead of overwriting an int: int[] oMult = {5}; ToIntFunction<Integer> oFuncMulVar = vNumLambda -> oMult[0] * vNumLambda; System.out.println(oFuncMulVar.applyAsInt(10)); //50 oMult[0] = 10; System.out.println(oFuncMulVar.applyAsInt(10)); //100 //MulFixed: Function<Integer,ToIntFunction<Integer>> oFuncMulMake = vMult -> (vNum -> vMult * vNum); int vMult = 5; var oFuncMulFixed = oFuncMulMake.apply(vMult); System.out.println(oFuncMulFixed.applyAsInt(10)); //50 vMult = 10; System.out.println(oFuncMulFixed.applyAsInt(10)); //50 [JavaScript] //MulVar: vMult = 5; oFuncMulVar = vNum => vMult * vNum; console.log(oFuncMulVar(10)); //50 vMult = 10; console.log(oFuncMulVar(10)); //100 //MulFixed: oFuncMulMake = vMult => (vNum => vMult * vNum); vMult = 5; oFuncMulFixed = oFuncMulMake(vMult); console.log(oFuncMulFixed(10)); //50 vMult = 10; console.log(oFuncMulFixed(10)); //50 [Kotlin] //MulVar: var vMult = 5 var oFuncMulVar = {vNum: Int -> vMult * vNum} println(oFuncMulVar(10)) //50 vMult = 10 println(oFuncMulVar(10)) //100 //MulFixed: var oFuncMulMake = {vMult: Int -> {vNum: Int -> vMult * vNum}} var vMult = 5 var oFuncMulFixed = oFuncMulMake(vMult) println(oFuncMulFixed(10)) //50 vMult = 10 println(oFuncMulFixed(10)) //50 [PHP] //MulVar: $vMult = 5; $oFuncMulVar = function($vNum) use (&$vMult) {return $vMult * $vNum;}; var_dump($oFuncMulVar(10)); //50 $vMult = 10; var_dump($oFuncMulVar(10)); //100 //MulFixed: $oFuncMulMake = fn($vMult) => (fn($vNum) => $vMult * $vNum); //$oFuncMulMake = function($vMult) {return fn($vNum) => $vMult * $vNum;}; //also worked $vMult = 5; $oFuncMulFixed = $oFuncMulMake($vMult); var_dump($oFuncMulFixed(10)); //50 $vMult = 10; var_dump($oFuncMulFixed(10)); //50 [Python] #MulVar: vMult = 5 oFuncMulVar = lambda vNum : vMult * vNum print(oFuncMulVar(10)) #50 vMult = 10 print(oFuncMulVar(10)) #100 #MulFixed: oFuncMulMake = lambda vMult : (lambda vNum : vMult * vNum) vMult = 5 oFuncMulFixed = oFuncMulMake(vMult) print(oFuncMulFixed(10)) #50 vMult = 10 print(oFuncMulFixed(10)) #50 [R] #MulVar: vMult = 5 oFuncMulVar = \(vNum) (vMult * vNum) print(oFuncMulVar(10)) #50 vMult = 10 print(oFuncMulVar(10)) #100 #MulFixed: oFuncMulMake = \(vMult) (\(vNum) (vMult * vNum)) vMult = 5 oFuncMulFixed = oFuncMulMake(vMult) print(oFuncMulFixed(10)) #50 vMult = 10 print(oFuncMulFixed(10)) #50 [Ruby] #MulVar: vMult = 5 oFuncMulVar = ->(vNum) {vMult * vNum} p oFuncMulVar.call(10) #50 vMult = 10 p oFuncMulVar.call(10) #100 #MulFixed: oFuncMulMake = ->(vMult) {->(vNum) {vMult * vNum}} vMult = 5 oFuncMulFixed = oFuncMulMake.call(vMult) p oFuncMulFixed.call(10) #50 vMult = 10 p oFuncMulFixed.call(10) #50 [Rust] //requires: use std::rc::Rc; //requires: use std::cell::RefCell; //MulVar: let oMult = Rc::new(RefCell::new(5)); let oFuncMulVar = |vNum:i32| -> i32{*oMult.borrow_mut() * vNum}; println!("{}", oFuncMulVar(10)); //50 *oMult.borrow_mut() = 10; println!("{}", oFuncMulVar(10)); //100 //MulFixed: fn oFuncMulMake(vMult:i32) -> impl Fn(i32) -> i32 {return move |vNum:i32| -> i32{vMult * vNum};} let mut vMult = 5; let oFuncMulFixed = oFuncMulMake(vMult); println!("{}", oFuncMulFixed(10)); //50 vMult = 10; println!("{}", oFuncMulFixed(10)); //50 [Scala] //MulVar: var vMult = 5 var oFuncMulVar = (vNum:Int) => vMult * vNum println(oFuncMulVar(10)) //50 vMult = 10 println(oFuncMulVar(10)) //100 //MulFixed: var oFuncMulMake = (vMult:Int) => ((vNum:Int) => vMult * vNum) var vMult = 5 var oFuncMulFixed = oFuncMulMake(vMult) println(oFuncMulFixed(10)) //50 vMult = 10 println(oFuncMulFixed(10)) //50 [Swift] //MulVar: var vMult = 5 var oFuncMulVar: (_ vNum: Int) -> Int = {vNum in vMult * vNum} print(oFuncMulVar(10)) //50 vMult = 10 print(oFuncMulVar(10)) //100 //MulFixed: var oFuncMulMake: (_ vMult:Int) -> ((Int) -> Int) = {vMult in {vNum in vMult * vNum}} var vMult = 5 var oFuncMulFixed = oFuncMulMake(vMult) print(oFuncMulFixed(10)) //50 vMult = 10 print(oFuncMulFixed(10)) //50
Class Examples (Full) UFL: ClassFull [custom 'zoo' class, 'MyClass', demonstrating methods/computed properties (via getters/setters)/simple properties, for classes (e.g. 'static' methods/properties) and instances, private/public(/protected) fields, and constructors/destructors][WARNING: some of the destructor and garbage collection techniques demonstrated are not recommended][note: some programming languages lack some of these core features][see also: Object.NewStrDemo/ClassPropComputed]
method/property summary: note: upper case means name must contain this string: = (instance constructor) - (default name e.g. MyClass/constructor/init) = CLASS METHOD (public) - MyClassMethod CLASS PROPERTY (PRIVATE read/write) - MyClassPropPrivate CLASS PROPERTY (public read/write) - MyClassProp [MyClassProp_Get/MyClassProp_Set] CLASS PROPERTY (public READ-ONLY) - MyClassPropReadOnly [MyClassPropReadOnly_Get] = instance METHOD (public) - MyMethod instance PROPERTY (PRIVATE read/write) - MyPropPrivate instance PROPERTY (public read/write) - MyProp [MyProp_Get/MyProp_Set] instance PROPERTY (public READ-ONLY) - MyPropReadOnly [MyPropReadOnly_Get] = (instance destructor) - (default name e.g. ~MyClass/finalize) = [also: a simple property (e.g. a public field, e.g. created via a macro)] CLASS PROPERTY (public read/write SIMPLE) - MyClassPropSimple CLASS PROPERTY (public READ-ONLY SIMPLE) - MyClassPropReadOnlySimple [a constant value] instance PROPERTY (public read/write SIMPLE) - MyPropSimple instance PROPERTY (public READ-ONLY SIMPLE) - MyPropReadOnlySimple [a constant value] = [note: property names of the form: 'MyClassPropPrivateReadOnlySimpleShort'] note: it is common for fields/variables to appear at the top of a class, often as public properties, or as private properties used by public methods note: for a class (static) method, 'this'/'self' typically refers to the class note: for an instance method, 'this'/'self' typically refers to the instance note: instead of 'MyClass.', to refer to the class object from inside an instance method, a non-hardcoded alternative, in JavaScript: 'this.constructor.' note: computed (dynamic) properties, typically appear like oObj.MyProp, but use getters/setters and private properties under the hood note: sometimes 'short' is used to denote alternate shorter syntaxes for method/property definition note: in some languages the expectation is that you never delete (or set to null) class instances (they are deleted when they go out of scope, i.e. when the function they were created in has finished executing) note: read-only fields/variables may be denoted in a variety of ways (e.g. const/final/val/let) WARNING: some of the destructor and garbage collection techniques demonstrated are not recommended simple checklist: - fields (variables) - properties: fields (variables) as simple properties - methods - methods: constructor - properties: computed properties (getters/setters) - properties/methods: getXXX/setXXX methods (if computed properties unavailable) - fields (variables): public/private/protected - methods: destructor - methods: destructor: force garbage collection - methods/properties: class (static) methods/properties - properties: read-only (const) properties ================================================== [AutoHotkey] myprint := (vVar:="") => A_Clipboard .= String(vVar) "`r`n" class MyClass { ;note: AutoHotkey doesn't have private properties, ;a convention is to use the prefix '_' for would-be private properties ;note: AHK class/instance objects can be assigned any property not specified in the class static _MyClassPropPrivate := "MyClassPropPrivate" static _MyClassPropPrivateReadOnly := "MyClassPropPrivateReadOnly" static MyClassPropSimple := "MyClassPropSimple" _MyPropPrivate := "MyPropPrivate" _MyPropPrivateReadOnly := "MyPropPrivateReadOnly" MyPropSimple := "MyPropSimple" static MyClassMethod() { myprint("MyClassMethod") } static MyClassProp { get { myprint("MyClassProp_Get") myprint("MyClassPropPrivate:" A_Space this._MyClassPropPrivate) return this._MyClassPropPrivate } set { ;note: special variable: 'value' myprint("MyClassProp_Set") return this._MyClassPropPrivate := value } } static MyClassPropReadOnly { get { myprint("MyClassPropReadOnly_Get") myprint("MyClassPropPrivateReadOnly:" A_Space this._MyClassPropPrivateReadOnly) return this._MyClassPropPrivateReadOnly } } __New(vDummy1:="", vDummy2:="") { myprint("constructor") MyClass._MyClassPropPrivate := "MyClassPropPrivate" this._MyPropPrivate := "MyPropPrivate" } MyMethod(vDummy1:="", vDummy2:="") { myprint("MyMethod") myprint("MyClassPropPrivate:" A_Space MyClass._MyClassPropPrivate) myprint("MyPropPrivate:" A_Space this._MyPropPrivate) } MyProp { get { myprint("MyProp_Get") return this._MyPropPrivate } set { ;note: special variable: 'value' myprint("MyProp_Set") return this._MyPropPrivate := value } } MyPropShort { get => (myprint("MyPropShort_Get"), this._MyPropPrivate) set => (myprint("MyPropShort_Set"), this._MyPropPrivate := value) } MyPropReadOnly { get { myprint("MyPropReadOnly_Get") return this._MyPropPrivateReadOnly } } MyPropReadOnlyShort => (myprint("MyPropReadOnlyShort_Get"), this._MyPropPrivateReadOnly) __Delete() { myprint("destructor") } } vRet := MsgBox("warning: will clear clipboard, then print to clipboard,`r`n" "continue?",, "YNC") if (vRet != "Yes") { MsgBox("early exit") return } A_Clipboard := "" MyClass.MyClassMethod() myprint() vProp := MyClass.MyClassProp myprint("ret:" A_Space vProp) myprint() oObj := MyClass() myprint() oObj.MyMethod() myprint() vProp := oObj.MyPropReadOnly myprint("ret:" A_Space vProp) vProp := oObj.MyProp myprint("ret:" A_Space vProp) oObj.MyProp := "MyPropPrivateNew" vProp := oObj.MyProp myprint("ret:" A_Space vProp) myprint() vProp := oObj.MyPropSimple myprint("ret:" A_Space vProp) oObj.MyPropSimple := "MyPropSimpleNew" vProp := oObj.MyPropSimple myprint("ret:" A_Space vProp) myprint() oObj := "" myprint("the end") MsgBox("the end") ================================================== [C++] #include <iostream> #include <string> template<typename T=char[], typename U> void myprint(T const& vVar1, U const& vVar2) {std::cout << (vVar1) << " " << (vVar2) << "\n";} template<typename T=char[]> void myprint(T const& vVar="") {std::cout << (vVar) << "\n";} class MyClass { //note: get error: 'static data member 'MyClassPropPrivate' not allowed in local class 'MyClass'' //and similar errors for other class (static) variables //if class is defined within a function private: inline static std::string MyClassPropPrivate = "MyClassPropPrivate"; inline static std::string MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly"; std::string MyPropPrivate = "MyPropPrivate"; std::string MyPropPrivateReadOnly = "MyPropPrivateReadOnly"; public: inline static std::string MyClassPropSimple = "MyClassPropSimple"; inline const static std::string MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple"; std::string MyPropSimple = "MyPropSimple"; const std::string MyPropReadOnlySimple = "MyPropReadOnlySimple"; static void MyClassMethod() { myprint("MyClassMethod"); } //WARNING: C++ lacks computed properties e.g. oObj.MyProp, //however, you can create ordinary oObj.MyProp properties: static std::string getMyClassProp() { myprint("MyClassProp_Get"); myprint("MyClassPropPrivate:", MyClass::MyClassPropPrivate); return MyClass::MyClassPropPrivate; } static std::string setMyClassProp(std::string vValue) { myprint("MyClassProp_Set"); return MyClass::MyClassPropPrivate = vValue; } static std::string getMyClassPropReadOnly() { myprint("MyClassPropReadOnly_Get"); myprint("MyClassPropPrivateReadOnly:", MyClass::MyClassPropPrivateReadOnly); return MyClass::MyClassPropPrivateReadOnly; } MyClass(std::string vDummy1="", std::string vDummy2="") { myprint("constructor"); MyClass::MyClassPropPrivate = "MyClassPropPrivate"; this->MyPropPrivate = "MyPropPrivate"; } void MyMethod(std::string vDummy1="", std::string vDummy2="") { myprint("MyMethod"); myprint("MyClassPropPrivate:", MyClass::MyClassPropPrivate); myprint("MyPropPrivate:", this->MyPropPrivate); } std::string getMyProp() { myprint("MyProp_Get"); return this->MyPropPrivate; } std::string setMyProp(std::string vValue) { myprint("MyProp_Set"); return this->MyPropPrivate = vValue; } std::string getMyPropReadOnly() { myprint("MyPropReadOnly_Get"); return this->MyPropPrivateReadOnly; } ~MyClass() { myprint("destructor"); } }; int main() { MyClass::MyClassMethod(); myprint(); auto vProp = MyClass::getMyClassProp(); myprint("ret:", vProp); myprint(); //auto oObj = MyClass(); //also works //MyClass oObj = MyClass(); //also works MyClass oObj; myprint(); oObj.MyMethod(); myprint(); vProp = oObj.getMyPropReadOnly(); myprint("ret:", vProp); vProp = oObj.getMyProp(); myprint("ret:", vProp); oObj.setMyProp("MyPropPrivateNew"); vProp = oObj.getMyProp(); myprint("ret:", vProp); myprint(); vProp = oObj.MyPropSimple; myprint("ret:", vProp); oObj.MyPropSimple = "MyPropSimpleNew"; vProp = oObj.MyPropSimple; myprint("ret:", vProp); myprint(); //delete oObj; //note: object created without using 'new', so we don't use 'delete', //it is a 'non-new' object, a 'non-pointer' object, we can't forcibly delete it, //the destructor is called when the function, in which the object was created, finishes executing myprint("the end"); //============================== myprint(); myprint("2nd object:"); MyClass::MyClassMethod(); myprint(); auto vProp2 = MyClass::getMyClassProp(); myprint("ret:", vProp2); myprint(); //WARNING: using 'new' requires using the '->' (arrow) operator, //whereas omitting 'new' requires using the preferable '.' (dot) operator: //auto oObj2 = new MyClass(); //also works MyClass* oObj2 = new MyClass(); myprint(); oObj2->MyMethod(); myprint(); vProp2 = oObj2->getMyPropReadOnly(); myprint("ret:", vProp2); vProp2 = oObj2->getMyProp(); myprint("ret:", vProp2); oObj2->setMyProp("MyPropPrivateNew"); vProp2 = oObj2->getMyProp(); myprint("ret:", vProp2); myprint(); vProp = oObj2->MyPropSimple; myprint("ret:", vProp); oObj2->MyPropSimple = "MyPropSimpleNew"; vProp = oObj2->MyPropSimple; myprint("ret:", vProp); myprint(); delete oObj2; myprint("the end"); } ================================================== [C#] using System; public class Program { public class MyClass { static string MyClassPropPrivate = "MyClassPropPrivate"; static string MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly"; public static string MyClassPropSimple = "MyClassPropSimple"; //WARNING: no 'public static const': //'public const' is a read-only class variable, //no way to do a read-only instance variable (workaround: a computed property) public const string MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple"; string MyPropPrivate = "MyPropPrivate"; string MyPropPrivateReadOnly = "MyPropPrivateReadOnly"; public string MyPropSimple = "MyPropSimple"; static public void MyClassMethod() { Console.WriteLine("MyClassMethod"); } static public string MyClassProp { get { Console.WriteLine("MyClassProp_Get"); Console.WriteLine(("MyClassPropPrivate:", MyClass.MyClassPropPrivate)); return MyClass.MyClassPropPrivate; } set { //note: special variable: 'value' Console.WriteLine("MyClassProp_Set"); MyClass.MyClassPropPrivate = value; //return MyClass.MyClassPropPrivate = value; //C# setters return void } } static public string MyClassPropReadOnly { get { Console.WriteLine("MyClassPropReadOnly_Get"); Console.WriteLine(("MyClassPropPrivateReadOnly:", MyClass.MyClassPropPrivateReadOnly)); return MyClass.MyClassPropPrivateReadOnly; } } public MyClass(string vDummy1="", string vDummy2="") { Console.WriteLine("constructor"); MyClass.MyClassPropPrivate = "MyClassPropPrivate"; this.MyPropPrivate = "MyPropPrivate"; } public void MyMethod(string vDummy1="", string vDummy2="") { Console.WriteLine("MyMethod"); Console.WriteLine(("MyClassPropPrivate:", MyClass.MyClassPropPrivate)); Console.WriteLine(("MyPropPrivate:", this.MyPropPrivate)); } public string MyProp { get { Console.WriteLine("MyProp_Get"); return this.MyPropPrivate; } set { //note: special variable: 'value' Console.WriteLine("MyProp_Set"); this.MyPropPrivate = value; //return this.MyPropPrivate = value; //C# setters return void } } public string MyPropReadOnly { get { Console.WriteLine("MyPropReadOnly_Get"); return this.MyPropPrivateReadOnly; } } ~MyClass() { Console.WriteLine("destructor"); } } public static void Main() { MyClass.MyClassMethod(); Console.WriteLine(); var vProp = MyClass.MyClassProp; Console.WriteLine(("ret:", vProp)); Console.WriteLine(); var oObj = new MyClass(); Console.WriteLine(); oObj.MyMethod(); Console.WriteLine(); vProp = oObj.MyPropReadOnly; Console.WriteLine(("ret:", vProp)); vProp = oObj.MyProp; Console.WriteLine(("ret:", vProp)); oObj.MyProp = "MyPropPrivateNew"; vProp = oObj.MyProp; Console.WriteLine(("ret:", vProp)); Console.WriteLine(); vProp = oObj.MyPropSimple; Console.WriteLine(("ret:", vProp)); oObj.MyPropSimple = "MyPropSimpleNew"; vProp = oObj.MyPropSimple; Console.WriteLine(("ret:", vProp)); Console.WriteLine(); //works: //vProp = MyClass.MyClassPropSimple; //Console.WriteLine(("ret:", vProp)); //MyClass.MyClassPropSimple = "MyClassPropSimpleNew"; //vProp = MyClass.MyClassPropSimple; //Console.WriteLine(("ret:", vProp)); //Console.WriteLine(); oObj = null; System.GC.Collect(); //failed attempt to force garbage collection, to test the destructor Console.WriteLine("the end"); } } ================================================== [Crystal] class MyClass @@myClassPropPrivate = "myClassPropPrivate" @@myClassPropPrivateReadOnly = "myClassPropPrivateReadOnly" @myPropPrivate = "myPropPrivate" @myPropPrivateReadOnly = "myPropPrivateReadOnly" property myPropSimple = "myProp" getter myPropReadOnlySimple = "myPropReadOnlySimple" def self.myClassMethod() p "myClassMethod" end def self.myClassProp #getter (note: a standard method) p "myClassProp_Get" p ["myClassPropPrivate:", @@myClassPropPrivate] return @@myClassPropPrivate end def self.myClassProp=(vValue) #setter p "myClassProp_Set" return @@myClassPropPrivate = vValue end def self.myClassPropReadOnly #getter (actually a standard method) p "myClassPropReadOnly_Get" p ["myClassPropPrivateReadOnly:", @@myClassPropPrivateReadOnly] return @@myClassPropPrivateReadOnly end def initialize(vDummy1 : String="", vDummy2 : String="") p "constructor" @@myClassPropPrivate = "myClassPropPrivate" @myPropPrivate = "myPropPrivate" end def myMethod(vDummy1 : String="", vDummy2 : String="") p "myMethod" p ["myClassPropPrivate:", @@myClassPropPrivate] p ["myPropPrivate:", @myPropPrivate] end def myProp #getter (note: a standard method) p "myProp_Get" return @myPropPrivate end def myProp=(vValue) #setter p "myProp_Set" return @myPropPrivate = vValue end def myPropReadOnly #getter (actually a standard method) p "myPropReadOnly_Get" return @myPropPrivateReadOnly end def finalize p "destructor" end end MyClass.myClassMethod() puts "\n" vProp = MyClass.myClassProp p ["ret:", vProp] puts "\n" oObj = MyClass.new() puts "\n" oObj.myMethod() puts "\n" vProp = oObj.myPropReadOnly p ["ret:", vProp] vProp = oObj.myProp p ["ret:", vProp] oObj.myProp = "myPropPrivateNew" vProp = oObj.myProp p ["ret:", vProp] puts "\n" vProp = oObj.myPropSimple p ["ret:", vProp] oObj.myPropSimple = "myPropSimpleNew" vProp = oObj.myPropSimple p ["ret:", vProp] puts "\n" #additional test code: #p MyClass::MyClassPropReadOnlySimple oObj = nil GC.collect p "the end" ================================================== [Excel] ================================================== [Excel VBA] ''MyClass' class module: 'to create a class module: 'Insert, Class Module 'this creates a module, e.g. 'Class1' 'click the treeview item, e.g. 'Class1' 'next to '(Name)', click the label, e.g. 'Class1', to rename the module 'e.g. 'MyClass' Private MyClassPropPrivate 'note: intended as a 'class'/'static' variable Private MyClassPropPrivateReadOnly 'note: intended as a 'class'/'static' variable 'Public MyClassPropSimple 'note: intended as a 'class'/'static' variable Private MyPropPrivate Private MyPropPrivateReadOnly Public MyPropSimple Public Static Function MyClassMethod() Debug.Print ("MyClassMethod") End Function Static Property Get MyClassProp() Debug.Print ("MyClassProp_Get") Debug.Print ("MyClassPropPrivate:" & " " & MyClassPropPrivate) MyClassProp = MyClassPropPrivate End Property 'WARNING: 2 kinds of setter: 'Property Let' and 'Property Set' Static Property Let MyClassProp(vValue) Debug.Print ("MyClassProp_Set") MyClassPropPrivate = vValue 'MyClassProp = vValue 'WARNING: would cause an infinite loop End Property Static Property Get MyClassPropReadOnly() Debug.Print ("MyClassPropReadOnly_Get") Debug.Print ("MyClassPropPrivateReadOnly:" & " " & MyClassPropPrivateReadOnly) MyClassPropReadOnly = MyClassPropPrivateReadOnly End Property Private Sub Class_Initialize() Debug.Print ("initializer (initialiser)") MyPropPrivate = "MyPropPrivate" MyPropPrivateReadOnly = "MyPropPrivateReadOnly" MyPropSimple = "MyPropSimple" MyClassPropPrivate = "MyClassPropPrivate" MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" MyClassPropSimple = "MyClassPropSimple" End Sub 'note: a custom 'constructor' method that must be called explicitly: Sub InitValues(Optional vDummy1 = "", Optional vDummy2 = "") Debug.Print ("constructor") MyClassPropPrivate = "MyClassPropPrivate" MyPropPrivate = "MyPropPrivate" End Sub Public Function MyMethod(Optional vDummy1 = "", Optional vDummy2 = "") Debug.Print ("MyMethod") Debug.Print ("MyClassPropPrivate:" & " " & MyClassPropPrivate) 'note: a way to access a 'class'/'static' variable via a 'class'/'static' property: 'Dim MyClass As MyClass 'Set MyClass = New MyClass 'Debug.Print ("MyClassPropPrivate:" & " " & MyClass.MyClassProp) Debug.Print ("MyPropPrivate:" & " " & MyPropPrivate) End Function Property Get MyProp() As String Debug.Print ("MyProp_Get") MyProp = MyPropPrivate End Property 'WARNING: 2 kinds of setter: 'Property Let' and 'Property Set' Property Let MyProp(vValue As String) 'setter Debug.Print ("MyProp_Set") MyPropPrivate = vValue 'MyProp = vValue 'WARNING: would cause an infinite loop End Property Property Get MyPropReadOnly() Debug.Print ("MyPropReadOnly_Get") MyPropReadOnly = MyPropPrivateReadOnly End Property Private Sub Class_Terminate() Debug.Print ("destructor") End Sub '============================== 'e.g. code to put in a macro: Dim MyClass As MyClass Set MyClass = New MyClass MyClass.MyClassMethod Debug.Print vProp = MyClass.MyClassProp Debug.Print ("ret:" & " " & vProp) Debug.Print Dim oObj As New MyClass Set oObj = New MyClass Call oObj.InitValues Debug.Print oObj.MyMethod Debug.Print vProp = oObj.MyPropReadOnly Debug.Print ("ret:" & " " & vProp) vProp = oObj.MyProp Debug.Print ("ret:" & " " & vProp) oObj.MyProp = "MyPropPrivateNew" vProp = oObj.MyProp Debug.Print ("ret:" & " " & vProp) Debug.Print vProp = oObj.MyPropSimple Debug.Print ("ret:" & " " & vProp) oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple Debug.Print ("ret:" & " " & vProp) Debug.Print Set oObj = Nothing Debug.Print ("the end") ================================================== [Go] //WARNING: no class ('static') properties/methods or workarounds in this demo: package main import ( "fmt" "runtime" "runtime/debug" ) func main() { vProp := "" oObj := MyClassNew() fmt.Println() oObj.MyMethod() fmt.Println() vProp = oObj.getMyPropReadOnly() fmt.Println("ret:", vProp) vProp = oObj.getMyProp() fmt.Println("ret:", vProp) oObj.setMyProp("MyPropPrivateNew") vProp = oObj.getMyProp() fmt.Println("ret:", vProp) fmt.Println() vProp = oObj.MyPropSimple fmt.Println("ret:", vProp) oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple fmt.Println("ret:", vProp) fmt.Println() //oObj = nil //error: 'cannot use nil as MyClass value in assignment' fmt.Println("the end") //============================== oObj2 := MyClassInterface(&MyClass{}) oObj2.MyMethod() fmt.Println() //fmt.Println(oObj.MyPropSimple) //can access all struct variables //fmt.Println(oObj2.MyPropSimple) //error: can only access struct variables listed in interface //note: attempt to force garbage collection to test that destructor is working: //runtime.GC() debug.FreeOSMemory() } // note: no 'static'/'class' variables: // note: no 'classes', use a struct as a workaround: type MyClass struct { //note: the 'private' variables, //are private for the 'MyClassInterface' interface, //but public for the 'MyClass' struct: MyPropPrivate string MyPropPrivateReadOnly string MyPropSimple string } // note: Go structs do not support private variables, // workaround: use an interface // (note: alternative workaround: use a package, // where private fields start lower case, public fields start upper case) type MyClassInterface interface { MyMethod() getMyProp() string setMyProp(string) getMyPropReadOnly() string } // WARNING: use '*' for the receiver to modify the object (by reference), omit it to clone the object (by value): func (oObj *MyClass) MyMethod() { fmt.Println("MyMethod") fmt.Println("MyPropPrivate:", oObj.MyPropPrivate) } // WARNING: Go lacks computed properties e.g. oObj.MyProp, // however, you can create ordinary oObj.MyProp properties: func (oObj *MyClass) getMyProp() string { fmt.Println("MyProp_Get") return oObj.MyPropPrivate } func (oObj *MyClass) setMyProp(vValue string) { fmt.Println("MyProp_Set") oObj.MyPropPrivate = vValue } func (oObj *MyClass) getMyPropReadOnly() string { fmt.Println("MyPropReadOnly_Get") return oObj.MyPropPrivateReadOnly } // note: custom constructor: func MyClassNew() MyClass { fmt.Println("constructor") oObj := MyClass{ MyPropPrivate: "MyPropPrivate", MyPropPrivateReadOnly: "MyPropPrivateReadOnly", MyPropSimple: "MyPropSimple", } runtime.SetFinalizer(&oObj, func(oObj *MyClass) { fmt.Println("destructor") }) return oObj } ================================================== [Java] import java.util.*; //for List.of class HelloWorld { static class MyClass { static String MyClassPropPrivate = "MyClassPropPrivate"; static String MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly"; static String MyClassPropSimple = "MyClassPropSimple"; static final String MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple"; String MyPropPrivate = "MyPropPrivate"; String MyPropPrivateReadOnly = "MyPropPrivateReadOnly"; public String MyPropSimple = "MyPropSimple"; public final String MyPropReadOnlySimple = "MyPropReadOnlySimple"; static public void MyClassMethod() { System.out.println("MyClassMethod"); } static public String getMyClassProp() { System.out.println("MyClassProp_Get"); System.out.println(List.of("MyClassPropPrivate:", MyClass.MyClassPropPrivate)); return MyClass.MyClassPropPrivate; } static public String setMyClassProp(String vValue) { System.out.println("MyClassProp_Set"); return MyClass.MyClassPropPrivate = vValue; } static public String getMyClassPropReadOnly() { System.out.println("MyClassPropReadOnly_Get"); System.out.println(List.of("MyClassPropPrivateReadOnly:", MyClass.MyClassPropPrivateReadOnly)); return MyClass.MyClassPropPrivateReadOnly; } //WARNING: Java lacks default values, use overloading as a workaround: //public MyClass(String vDummy1, String vDummy2) public MyClass() { System.out.println("constructor"); MyClass.MyClassPropPrivate = "MyClassPropPrivate"; this.MyPropPrivate = "MyPropPrivate"; } //public MyMethod(String vDummy1, String vDummy2) public void MyMethod() { System.out.println("MyMethod"); System.out.println(List.of("MyClassPropPrivate:", MyClass.MyClassPropPrivate)); System.out.println(List.of("MyPropPrivate:", this.MyPropPrivate)); } //WARNING: Java lacks computed properties e.g. oObj.MyProp, //the convention is getXXX() and setXXX() methods, //however, you can create ordinary oObj.MyProp properties: //e.g. public String MyPropSimple = "MyPropSimple"; public String getMyProp() { System.out.println("MyProp_Get"); return this.MyPropPrivate; } public String setMyProp(String vValue) { System.out.println("MyProp_Set"); return this.MyPropPrivate = vValue; } public String getMyPropReadOnly() { System.out.println("MyPropReadOnly_Get"); return this.MyPropPrivateReadOnly; } protected void finalize() throws Throwable { try { System.out.println("destructor"); } catch (Throwable e) { } finally { super.finalize(); } } } public static void main(String[] args) { MyClass.MyClassMethod(); System.out.println(); var vProp = MyClass.getMyClassProp(); System.out.println(List.of("ret:", vProp)); System.out.println(); var oObj = new MyClass(); System.out.println(); oObj.MyMethod(); System.out.println(); vProp = oObj.getMyPropReadOnly(); System.out.println(List.of("ret:", vProp)); vProp = oObj.getMyProp(); System.out.println(List.of("ret:", vProp)); oObj.setMyProp("MyPropPrivateNew"); vProp = oObj.getMyProp(); System.out.println(List.of("ret:", vProp)); System.out.println(); vProp = oObj.MyPropSimple; System.out.println(List.of("ret:", vProp)); oObj.MyPropSimple = "MyPropPrivateNew"; vProp = oObj.MyPropSimple; System.out.println(List.of("ret:", vProp)); System.out.println(); //note: ordinary way to delete instance reference: //oObj = null; //note: explicitly calling finalize to demonstrate it: try { oObj.finalize(); } catch (Throwable e) { } System.out.println("the end"); } } ================================================== [JavaScript] oRegistry = new FinalizationRegistry(oFunc=>oFunc()); class MyClass { static #MyClassPropPrivate = "MyClassPropPrivate"; static #MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly"; static MyClassPropSimple = "MyClassPropSimple"; #MyPropPrivate = "MyPropPrivate"; #MyPropPrivateReadOnly = "MyPropPrivateReadOnly"; MyPropSimple = "MyPropSimple"; static MyClassMethod() { console.log("MyClassMethod"); } static get MyClassProp() { console.log("MyClassProp_Get"); console.log(["MyClassPropPrivate:", this.#MyClassPropPrivate]); return this.#MyClassPropPrivate; } static set MyClassProp(vValue) { console.log("MyClassProp_Set"); return this.#MyClassPropPrivate = vValue; } static get MyClassPropReadOnly() { console.log("MyClassPropReadOnly_Get"); console.log(["MyClassPropPrivateReadOnly:", this.#MyClassPropPrivateReadOnly]); return this.#MyClassPropPrivateReadOnly; } constructor(vDummy1="", vDummy2="") { console.log("constructor"); MyClass.#MyClassPropPrivate = "MyClassPropPrivate"; //MyClass.MyClassProp = "MyClassPropPrivate"; this.#MyPropPrivate = "MyPropPrivate"; oRegistry.register(this, ()=>console.log("destructor")); } MyMethod(vDummy1="", vDummy2="") { console.log("MyMethod"); console.log(["MyClassPropPrivate:", MyClass.#MyClassPropPrivate]); //console.log(["MyClassPropPrivate:", MyClass.MyClassProp]); console.log(["MyPropPrivate:", this.#MyPropPrivate]); } get MyProp() { console.log("MyProp_Get"); return this.#MyPropPrivate; } set MyProp(vValue) { console.log("MyProp_Set"); return this.#MyPropPrivate = vValue; } get MyPropReadOnly() { console.log("MyPropReadOnly_Get"); return this.#MyPropPrivateReadOnly; } } MyClass.MyClassMethod(); console.log(); vProp = MyClass.MyClassProp; console.log(["ret:", vProp]); console.log(); oObj = new MyClass(); console.log(); oObj.MyMethod(); console.log(); vProp = oObj.MyPropReadOnly; console.log(["ret:", vProp]); vProp = oObj.MyProp; console.log(["ret:", vProp]); oObj.MyProp = "MyPropPrivateNew"; vProp = oObj.MyProp; console.log(["ret:", vProp]); console.log(); vProp = oObj.MyPropSimple; console.log(["ret:", vProp]); oObj.MyPropSimple = "MyPropSimpleNew"; vProp = oObj.MyPropSimple; console.log(["ret:", vProp]); console.log(); //works: //vProp = MyClass.MyClassPropSimple; //console.log(["ret:", vProp]); //MyClass.MyClassPropSimple = "MyClassPropSimpleNew"; //vProp = MyClass.MyClassPropSimple; //console.log(["ret:", vProp]); //console.log(); //garbage collector demo: //oArray = []; //oRegistry = new FinalizationRegistry(oFunc=>oFunc()); //oRegistry.register(oArray, ()=>console.log("my message")); //oArray = null; //Chrome: clear garbage collector: //click Console tab //paste code into console and run it //click Performance tab //click 'Collect garbage' button (it looks like a birthday cake) //click Console tab //the message should be printed oObj = null; console.log("the end"); ================================================== [Kotlin] class MyClass { private var MyPropPrivate: String = "MyPropPrivate" private var MyPropPrivateReadOnly: String = "MyPropPrivateReadOnly" var MyPropSimple: String = "MyPropSimple" val MyPropReadOnlySimple: String = "MyPropReadOnlySimple" //note: get error: Modifier 'companion' is not applicable inside 'local class' //if class is defined within a function companion object { private var MyClassPropPrivate = "MyClassPropPrivate" private var MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" var MyClassPropSimple = "MyClassPropSimple" val MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple" fun MyClassMethod() { println("MyClassMethod") } var MyClassProp: String get() { println("MyClassProp_Get") println(Pair("MyClassPropPrivate:", this.MyClassPropPrivate)) return this.MyClassPropPrivate } set(vValue) { println("MyClassProp_Set") //return this.MyClassPropPrivate = vValue //Kotlin setters return void this.MyClassPropPrivate = vValue } var MyClassPropReadOnly: String = "" get() { println("MyClassPropReadOnly_Get") println(Pair("MyClassPropPrivateReadOnly:", this.MyClassPropPrivateReadOnly)) return this.MyClassPropPrivateReadOnly } } constructor(vDummy1: String="", vDummy2: String="") { println("constructor") MyClass.MyClassPropPrivate = "MyClassPropPrivate" this.MyPropPrivate = "MyPropPrivate" } fun MyMethod(vDummy1: String="", vDummy2: String="") : Unit { println("MyMethod") println(Pair("MyClassPropPrivate:", MyClass.MyClassPropPrivate)) println(Pair("MyPropPrivate:", this.MyPropPrivate)) } var MyProp: String get() { println("MyProp_Get") return this.MyPropPrivate } set(vValue) { println("MyProp_Set") //return this.MyPropPrivate = vValue //Kotlin setters return void this.MyPropPrivate = vValue } var MyPropReadOnly: String = "" get() { println("MyPropReadOnly_Get") return this.MyPropPrivateReadOnly } fun finalize() { println("destructor") } } fun main() { MyClass.MyClassMethod() println() var vProp = MyClass.MyClassProp println(Pair("ret:", vProp)) println() //var oObj = MyClass() //note: can't be assigned null var oObj: MyClass? = MyClass() //note: can be assigned null, but requires using 'oObj!!' (for methods) instead of 'oObj' println() oObj!!.MyMethod() println() vProp = oObj.MyPropReadOnly println(Pair("ret:", vProp)) vProp = oObj.MyProp println(Pair("ret:", vProp)) oObj.MyProp = "MyPropPrivateNew" vProp = oObj.MyProp println(Pair("ret:", vProp)) println() vProp = oObj.MyPropSimple println(Pair("ret:", vProp)) oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple println(Pair("ret:", vProp)) println() //note: ordinary way to delete instance reference: //oObj = null //note: explicitly calling finalize to demonstrate it: oObj.finalize() println("the end") } ================================================== [PHP] <?php class MyClass { private static $MyClassPropPrivate = "MyClassPropPrivate"; private static $MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly"; public static $MyClassPropSimple = "MyClassPropSimple"; const MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple"; //WARNING: no '$' prefix //note: const variable names are typically written in upper case private $MyPropPrivate = "MyPropPrivate"; private $MyPropPrivateReadOnly = "MyPropPrivateReadOnly"; public $MyPropSimple = "MyPropSimple"; public static function MyClassMethod() { var_dump("MyClassMethod"); } //WARNING: __get/__set not available for static properties: //public static function __get($vProp) //for property getters //{ // if ($vProp == "MyClassPropSimple") // { // var_dump("MyClassProp_Get"); // var_dump(["MyClassPropPrivate:", $this->MyClassPropPrivate]); // return $this->MyClassPropPrivate; // } // else if ($vProp == "MyClassPropReadOnly") // { // var_dump("MyClassPropReadOnly_Get"); // var_dump(["MyClassPropPrivateReadOnly:", $this->MyClassPropPrivateReadOnly]); // return $this->MyClassPropPrivateReadOnly; // } //} //public static function __set($vProp, $vValue) //for property setters //{ // if ($vProp == "MyClassPropSimple") // { // var_dump("MyClassProp_Set"); // return $this->MyClassPropPrivate = $vValue; // } //} public function __construct($vDummy1="", $vDummy2="") { var_dump("constructor"); MyClass::$MyClassPropPrivate = "MyClassPropPrivate"; $this->MyPropPrivate = "MyPropPrivate"; } public function MyMethod($vDummy1="", $vDummy2="") { var_dump("MyMethod"); var_dump(["MyClassPropPrivate:", MyClass::$MyClassPropPrivate]); var_dump(["MyPropPrivate:", $this->MyPropPrivate]); } public function __get($vProp) //for property getters { if ($vProp == "MyProp") { var_dump("MyProp_Get"); return $this->MyPropPrivate; } else if ($vProp == "MyPropReadOnly") { var_dump("MyPropReadOnly_Get"); return $this->MyPropPrivateReadOnly; } } public function __set($vProp, $vValue) //for property setters { if ($vProp == "MyProp") { var_dump("MyProp_Set"); return $this->MyPropPrivate = $vValue; } } public function __destruct() { var_dump("destructor"); } } MyClass::MyClassMethod(); var_dump(""); //$vProp = MyClass::$MyClassProp; $vProp = "ERROR"; var_dump(["ret:", $vProp]); var_dump(""); $oObj = new MyClass(); var_dump(""); $oObj->MyMethod(); var_dump(""); $vProp = $oObj->MyPropReadOnly; var_dump(["ret:", $vProp]); $vProp = $oObj->MyProp; var_dump(["ret:", $vProp]); $oObj->MyProp = "MyPropPrivateNew"; $vProp = $oObj->MyProp; var_dump(["ret:", $vProp]); var_dump(""); $vProp = $oObj->MyPropSimple; var_dump(["ret:", $vProp]); $oObj->MyPropSimple = "MyPropSimpleNew"; $vProp = $oObj->MyPropSimple; var_dump(["ret:", $vProp]); var_dump(""); //additional test code: //var_dump(MyClass::MyClassPropReadOnlySimple); $oObj = null; var_dump("the end"); ================================================== [Python] class MyClass: #note: Python doesn't have private properties, #a convention is to use the prefix '_' for would-be private properties _MyClassPropPrivate = "MyClassPropPrivate" _MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" MyClassPropSimple = "MyClassPropSimple" #note: can't define instance variables at the top of the class, see '__init__()' @classmethod def MyClassMethod(self): print("MyClassMethod") #WARNING: Python lacks functionality for class property getters/setters #@classmethod #@property #def MyClassProp(self): # print("MyClassProp_Get") # print(["MyClassPropPrivate:", self._MyClassPropPrivate]) # return MyClass._MyClassPropPrivate #@classmethod #@MyClassProp.setter #def MyClassProp(self, vValue): # print("MyClassProp_Set") # MyClass._MyClassPropPrivate = vValue # return vValue #@classmethod #@property #def MyClassPropReadOnly(self): # print("MyClassPropReadOnly_Get") # print(["MyClassPropPrivateReadOnly:", self._MyClassPropPrivateReadOnly]) # return MyClass._MyClassPropPrivateReadOnly def __init__(self, vDummy1="", vDummy2=""): self._MyPropPrivate = "MyPropPrivate" self._MyPropPrivateReadOnly = "MyPropPrivateReadOnly" self.MyPropSimple = "MyPropSimple" print("constructor") MyClass._MyClassPropPrivate = "MyClassPropPrivate" self._MyPropPrivate = "MyPropPrivate" def MyMethod(self, vDummy1="", vDummy2=""): print("MyMethod") print(["MyClassPropPrivate:", MyClass._MyClassPropPrivate]) print(["MyPropPrivate:", self._MyPropPrivate]) @property def MyProp(self): print("MyProp_Get") return self._MyPropPrivate @MyProp.setter def MyProp(self, vValue): print("MyProp_Set") self._MyPropPrivate = vValue return vValue @property def MyPropReadOnly(self): print("MyPropReadOnly_Get") return self._MyPropPrivateReadOnly def __del__(self): print("destructor") #end of class MyClass.MyClassMethod() print() #vProp = MyClass.MyClassProp vProp = "ERROR" print(["ret:", vProp]) print() oObj = MyClass() print() oObj.MyMethod() print() vProp = oObj.MyPropReadOnly print(["ret:", vProp]) vProp = oObj.MyProp print(["ret:", vProp]) oObj.MyProp = "MyPropPrivateNew" vProp = oObj.MyProp print(["ret:", vProp]) print() vProp = oObj.MyPropSimple print(["ret:", vProp]) oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple print(["ret:", vProp]) print() oObj = None print("the end") ================================================== [R] #WARNING: no class ('static') properties/methods or workarounds in this demo: #note: a reference class (not an S3 or S4 class): MyClass = setRefClass( "MyClass", #note: using the custom prefix 'priv' to indicate 'private' fields: #note: type can be 'ANY': fields = list( privMyPropPrivate = "character", privMyPropPrivateReadOnly = "character", MyPropSimple = "character", MyPropReadOnlySimple = "character" ), #WARNING: R lacks computed properties e.g. oObj.MyProp, #however, you can create ordinary oObj.MyProp properties: methods = list( initialize = function(vDummy1="", vDummy2="") { #note: '<<-', non-local assignment operator: privMyPropPrivate <<- "MyPropPrivate" privMyPropPrivateReadOnly <<- "MyPropPrivateReadOnly" MyPropSimple <<- "MyPropSimple" MyPropReadOnlySimple <<- "MyPropReadOnlySimple" #note: we make this read-only by using 'lock' lower down print("constructor") privMyPropPrivate <<- "MyPropPrivate" }, MyMethod = function(vDummy1="", vDummy2="") { print("MyMethod") print(c("MyPropPrivate:", privMyPropPrivate)) }, getMyProp = function() { print("MyProp_Get") privMyPropPrivate }, setMyProp = function(vValue) { print("MyProp_Set") privMyPropPrivate <<- vValue }, getMyPropReadOnly = function() { print("MyPropReadOnly_Get") privMyPropPrivateReadOnly }, finalize = function() { print("destructor") } ) ) MyClass$lock("MyPropReadOnlySimple") #note: makes fields read-only vProp = "" oObj = MyClass$new() cat("\n") oObj$MyMethod() cat("\n") vProp = oObj$getMyPropReadOnly() print(c("ret:", vProp)) vProp = oObj$getMyProp() print(c("ret:", vProp)) oObj$setMyProp("MyPropPrivateNew") vProp = oObj$getMyProp() print(c("ret:", vProp)) cat("\n") vProp = oObj$MyPropSimple print(c("ret:", vProp)) oObj$MyPropSimple = "MyPropSimpleNew" vProp = oObj$MyPropSimple print(c("ret:", vProp)) cat("\n") rm(oObj) #oObj = NULL #also works gc() #invisible(gc()) #note: run gc() but hide the output print("the end") ================================================== [Ruby] class MyClass @@MyClassPropPrivate = "MyClassPropPrivate" @@MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple" #WARNING: this starts with a capital letter, so is a constant #WARNING: for property macros, initial values must be set elsewhere: #class << self # attr_accessor :MyClassPropAlt # attr_reader :MyClassPropReadOnlyAlt #end attr_accessor :MyPropSimple #value set in 'initialize' attr_reader :MyPropReadOnlySimple #value set in 'initialize' def self.MyClassMethod() p "MyClassMethod" end def self.MyClassProp #getter (note: a standard method) p "MyClassProp_Get" p ["MyClassPropPrivate:", @@MyClassPropPrivate] return @@MyClassPropPrivate end def self.MyClassProp=(vValue) #setter p "MyClassProp_Set" return @@MyClassPropPrivate = vValue end def self.MyClassPropReadOnly #getter (actually a standard method) p "MyClassPropReadOnly_Get" p ["MyClassPropPrivateReadOnly:", @@MyClassPropPrivateReadOnly] return @@MyClassPropPrivateReadOnly end def initialize(vDummy1="", vDummy2="") #setting values for properties: #WARNING: setting these values at the top of the class, would assign to the class object, not instance objects: @MyPropPrivate = "MyPropPrivate" @MyPropPrivateReadOnly = "MyPropPrivateReadOnly" @MyPropSimple = "MyPropSimple" @MyPropReadOnlySimple = "MyPropReadOnlySimple" @@MyClassPropSimple = "MyClassPropSimple" @@MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple" p "constructor" @@MyClassPropPrivate = "MyClassPropPrivate" @MyPropPrivate = "MyPropPrivate" ObjectSpace.define_finalizer(self, self.class.method(:finalize).to_proc) #specify destructor end def MyMethod(vDummy1="", vDummy2="") p "MyMethod" p ["MyClassPropPrivate:", @@MyClassPropPrivate] p ["MyPropPrivate:", @MyPropPrivate] end def MyProp #getter (note: a standard method) p "MyProp_Get" return @MyPropPrivate end def MyProp=(vValue) #setter p "MyProp_Set" return @MyPropPrivate = vValue end def MyPropReadOnly #getter (actually a standard method) p "MyPropReadOnly_Get" return @MyPropPrivateReadOnly end def MyClass.finalize(id) p "destructor" end end MyClass.MyClassMethod() puts "\n" vProp = MyClass.MyClassProp p ["ret:", vProp] puts "\n" oObj = MyClass.new() puts "\n" oObj.MyMethod() puts "\n" vProp = oObj.MyPropReadOnly p ["ret:", vProp] vProp = oObj.MyProp p ["ret:", vProp] oObj.MyProp = "MyPropPrivateNew" vProp = oObj.MyProp p ["ret:", vProp] puts "\n" vProp = oObj.MyPropSimple p ["ret:", vProp] oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple p ["ret:", vProp] puts "\n" #additional test code: #p MyClass::MyClassPropReadOnlySimple oObj = nil GC.start p "the end" ================================================== [Rust] //WARNING: no class ('static') properties/methods or workarounds in this demo (except MyClassPropReadOnlySimple): #![allow(non_snake_case)] mod MyModule { pub struct MyClassInModule { MyPropPrivate: String, MyPropPrivateReadOnly: String, pub MyPropSimple: String } impl MyClassInModule { pub fn new() -> MyClassInModule { MyClassInModule { MyPropPrivate: "MyPropPrivate".to_string(), MyPropPrivateReadOnly: "MyPropPrivateReadOnly".to_string(), MyPropSimple: "MyPropSimple".to_string() } } } } //note: fields are private by default (to other modules), use 'pub' to make them public struct MyClass { MyPropPrivate: String, MyPropPrivateReadOnly: String, MyPropSimple: String } impl MyClass { const MyClassPropReadOnlySimple: &str = "MyClassPropReadOnlySimple"; //note: const variable names are typically written in upper case //fn new(vDummy1: &str, vDummy2: &str) -> MyClass //note: Rust lacks default values and function overloading fn new() -> MyClass { println!("{}", "constructor"); MyClass { MyPropPrivate: "MyPropPrivate".to_string(), MyPropPrivateReadOnly: "MyPropPrivateReadOnly".to_string(), MyPropSimple: "MyPropSimple".to_string() } } //fn MyMethod(&self, vDummy1: &str, vDummy2: &str) //note: Rust lacks default values and function overloading fn MyMethod(&self) { println!("{}", "MyMethod"); println!("{:?}", ("MyPropPrivate:", self.MyPropPrivate.clone())); } //WARNING: Rust lacks computed properties e.g. oObj.MyProp, //however, you can create ordinary oObj.MyProp properties: fn getMyProp(&self) -> String { println!("{:?}", "MyProp_Get"); return self.MyPropPrivate.clone(); } fn setMyProp(&mut self, vValue: String) { println!("{:?}", "MyProp_Set"); self.MyPropPrivate = vValue; } fn getMyPropReadOnly(&self) -> String { println!("{:?}", "MyPropReadOnly_Get"); return self.MyPropPrivateReadOnly.clone(); } } impl Drop for MyClass { fn drop(&mut self) { println!("{}", "destructor"); } } fn main() { let mut vProp = "".to_string(); let mut oObj = MyClass::new(); println!(); //also works: //let oObj = MyClass //{ // MyPropPrivate: "MyPropPrivate".to_string(), // MyPropPrivateReadOnly: "MyPropPrivateReadOnly".to_string(), // MyPropSimple: "MyPropSimple".to_string() //}; oObj.MyMethod(); println!(); vProp = oObj.getMyPropReadOnly().clone(); println!("{:?}", ("ret:", vProp)); vProp = oObj.getMyProp().clone(); println!("{:?}", ("ret:", vProp)); oObj.setMyProp("MyPropPrivateNew".to_string()); vProp = oObj.getMyProp().clone(); println!("{:?}", ("ret:", vProp)); println!(); vProp = oObj.MyPropSimple.clone(); println!("{:?}", ("ret:", vProp)); oObj.MyPropSimple = "MyPropSimpleNew".to_string(); vProp = oObj.MyPropSimple.clone(); println!("{:?}", ("ret:", vProp)); println!(); //additional test code: //println!("{}", oObj.MyPropSimple); //println!("{}", oObj.MyPropPrivate); //println!("{}", MyClass::MyClassPropReadOnlySimple); //additional test code: //let oObj2 = MyModule::MyClassInModule::new(); //println!("{}", oObj2.MyPropSimple); //println!("{}", oObj2.MyPropPrivate); //error as expected: 'field `MyPropPrivate` of struct `MyClassInModule` is private' drop(oObj); println!("{:?}", "the end"); } ================================================== [Scala] //companion object for MyClass (for creating 'class' variables): object MyClass { private var MyClassPropPrivate = "MyClassPropPrivate" private var MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" var MyClassPropSimple = "MyClassPropSimple" val MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple" def MyClassMethod(): Unit = { println("MyClassMethod") } def MyClassProp: String = { println("MyClassProp_Get") println(("MyClassPropPrivate:", this.MyClassPropPrivate)) return this.MyClassPropPrivate } def MyClassProp_=(vValue: String): String = { println("MyClassProp_Set") this.MyClassPropPrivate = vValue return vValue } def MyClassPropReadOnly: String = { println("MyClassPropReadOnly_Get") println(("MyClassPropPrivateReadOnly:", this.MyClassPropPrivateReadOnly)) return this.MyClassPropPrivateReadOnly } } class MyClass(vDummy1: String="", vDummy2: String="") { //constructor start: private var MyPropPrivate = "MyPropPrivate" private var MyPropPrivateReadOnly = "MyPropPrivateReadOnly" var MyPropSimple = "MyPropSimple" val MyPropReadOnlySimple = "MyPropReadOnlySimple" println("constructor") MyClass.MyClassPropPrivate = "MyClassPropPrivate" this.MyPropPrivate = "MyPropPrivate" //constructor end def MyMethod(vDummy1: String="", vDummy2: String=""): Unit = { println("MyMethod") println(("MyClassPropPrivate:", MyClass.MyClassPropPrivate)) println(("MyPropPrivate:", this.MyPropPrivate)) } def MyProp: String = { println("MyProp_Get") return this.MyPropPrivate } def MyProp_=(vValue: String): String = { println("MyProp_Set") this.MyPropPrivate = vValue return vValue } def MyPropReadOnly: String = { println("MyPropReadOnly_Get") return this.MyPropPrivateReadOnly } //no destructor //{ // println("destructor") //} } MyClass.MyClassMethod() println() var vProp = MyClass.MyClassProp println(("ret:", vProp)) println() var oObj = MyClass() println() oObj.MyMethod() println() vProp = oObj.MyPropReadOnly println(("ret:", vProp)) vProp = oObj.MyProp println(("ret:", vProp)) oObj.MyProp = "MyPropPrivateNew" vProp = oObj.MyProp println(("ret:", vProp)) println() vProp = oObj.MyPropSimple println(("ret:", vProp)) oObj.MyPropSimple = "MyPropSimpleNew" vProp = oObj.MyPropSimple println(("ret:", vProp)) println() //additional test code: //println(MyClass.MyClassPropReadOnlySimple) //println(oObj.MyPropReadOnlySimple) oObj = null println("the end") ================================================== [Swift] class MyClass { //note: subclasses can override class variables //note: subclasses can't override static variables static private var MyClassPropPrivate = "MyClassPropPrivate" static private var MyClassPropPrivateReadOnly = "MyClassPropPrivateReadOnly" static var MyClassPropSimple = "MyClassPropSimple" static let MyClassPropReadOnlySimple = "MyClassPropReadOnlySimple" private var MyPropPrivate: String = "MyPropPrivate" private var MyPropPrivateReadOnly: String = "MyPropPrivateReadOnly" var MyPropSimple = "MyPropSimple" let MyPropReadOnlySimple = "MyPropReadOnlySimple" static func MyClassMethod() { print("MyClassMethod") } static var MyClassProp: String { get { print("MyClassProp_Get") print(["MyClassPropPrivate:", self.MyClassPropPrivate]) return self.MyClassPropPrivate } set(vValue) { print("MyClassProp_Set") return self.MyClassPropPrivate = vValue } } static var MyClassPropReadOnly: String { get { print("MyClassPropReadOnly_Get") print(["MyClassPropPrivateReadOnly:", self.MyClassPropPrivateReadOnly]) return self.MyClassPropPrivateReadOnly } } init(_ vDummy1: String = "", _ vDummy2: String = "") { print("constructor") MyClass.MyClassPropPrivate = "MyClassPropPrivate" self.MyPropPrivate = "MyPropPrivate" } func MyMethod(_ vDummy1: String = "", _ vDummy2: String = "") -> () { print("MyMethod") print(["MyClassPropPrivate:", MyClass.MyClassPropPrivate]) print(["MyPropPrivate:", self.MyPropPrivate]) } var MyProp: String { get { print("MyProp_Get") return self.MyPropPrivate } set(vValue) { print("MyProp_Set") return self.MyPropPrivate = vValue } } var MyPropReadOnly: String { get { print("MyPropReadOnly_Get") return self.MyPropPrivateReadOnly } } deinit { print("destructor") } } MyClass.MyClassMethod() print() var vProp = MyClass.MyClassProp print(["ret:", vProp]) print() //var oObj = MyClass() //note: can't be assigned nil var oObj: MyClass? = MyClass() //note: can be assigned nil, but requires using 'oObj!' instead of 'oObj' print() oObj!.MyMethod() print() vProp = oObj!.MyPropReadOnly print(["ret:", vProp]) vProp = oObj!.MyProp print(["ret:", vProp]) oObj!.MyProp = "MyPropPrivateNew" vProp = oObj!.MyProp print(["ret:", vProp]) print() vProp = oObj!.MyPropSimple print(["ret:", vProp]) oObj!.MyPropSimple = "MyPropSimpleNew" vProp = oObj!.MyPropSimple print(["ret:", vProp]) print() //additional test code: //print(MyClass.MyClassPropReadOnlySimple) //print(oObj!.MyPropReadOnlySimple) oObj = nil print("the end")
Class Examples (Rectangle) UFL: ClassRectangle [custom Rectangle class demonstrating methods/properties]
[AutoHotkey] class Rectangle { __New(vWidth, vHeight) { this.width := vWidth this.height := vHeight } ;read-only properties long-form: ;area ;{ ; get ; { ; return this.calcArea() ; } ;} ;perimeter ;{ ; get ; { ; return this.calcPerimeter() ; } ;} ;read-only properties short-form: area => this.calcArea() perimeter => this.calcPerimeter() calcArea() { return this.width * this.height } calcPerimeter() { return (this.width + this.height) * 2 } } oRect := Rectangle(10, 2) ;note: 'new' keyword needed in AutoHotkey v1 MsgBox(oRect.area) ;20 MsgBox(oRect.perimeter) ;24 MsgBox(oRect.calcArea()) ;20 MsgBox(oRect.calcPerimeter()) ;24 [C++] #include <iostream> template<typename T=char[]> void myprint(T const& vVar="") {std::cout << (vVar) << "\n";} int main() { class Rectangle { private: int width; int height; public: Rectangle(int vWidth, int vHeight) { this->width = vWidth; this->height = vHeight; } int getArea() { return this->calcArea(); } int getPerimeter() { return this->calcPerimeter(); } int calcArea() { return this->width * this->height; } int calcPerimeter() { return (this->width + this->height) * 2; } }; auto oRect = new Rectangle(10, 2); myprint(oRect->getArea()); //20 myprint(oRect->getPerimeter()); //24 myprint(oRect->calcArea()); //20 myprint(oRect->calcPerimeter()); //24 } [C#] public class Rectangle { int width; int height; public Rectangle(int vWidth, int vHeight) { this.width = vWidth; this.height = vHeight; } public int area { get {return this.calcArea();} } public int perimeter { get {return this.calcPerimeter();} } public int calcArea() { return this.width * this.height; } public int calcPerimeter() { return (this.width + this.height) * 2; } } var oRect = new Rectangle(10, 2); Console.WriteLine(oRect.area); //20 Console.WriteLine(oRect.perimeter); //24 Console.WriteLine(oRect.calcArea()); //20 Console.WriteLine(oRect.calcPerimeter()); //24 [Crystal] class Rectangle def initialize(vWidth : Int32, vHeight : Int32) @width = vWidth @height = vHeight end def area #getter (note: a standard method) return self.calcArea() end def area=(vValue : Int32) #setter return "ERROR" end def perimeter #getter (note: a standard method) return calcPerimeter() end def perimeter=(vValue : Int32) #setter return "ERROR" end def calcArea() return @width * @height end def calcPerimeter() return (@width + @height) * 2 end end oRect = Rectangle.new(10, 2) p oRect.area #20 p oRect.perimeter #24 p oRect.calcArea() #20 p oRect.calcPerimeter() #24 [Excel] [Excel VBA] '============================== 'class modules: 'note: comes with 2 trivial demo class modules: 'Rectangle [with properties: Area/Perimeter, and functions: calcArea/calcPerimeter] 'StringFuncsClass [with function: Lower] 'to create a class module: 'Insert, Class Module 'this creates a module, e.g. 'Class1' 'click the treeview item, e.g. 'Class1' 'next to '(Name)', click the label, e.g. 'Class1', to rename the module 'e.g. 'Rectangle'/'StringFuncsClass' '============================== 'code in StringFuncsClass class module: Public Static Function Lower(vText) Lower = LCase(vText) End Function '============================== 'code in Rectangle class module: Private m_Width Private m_Height Property Get Area() 'Area = calcArea() Area = m_Width * m_Height End Property Property Get Perimeter() 'Perimeter = calcPerimeter() Perimeter = (m_Width + m_Height) * 2 End Property Sub InitValues(vWidth, vHeight) m_Width = vWidth m_Height = vHeight End Sub Public Function calcArea() calcArea = m_Width * m_Height End Function Public Function calcPerimeter() calcPerimeter = (m_Width + m_Height) * 2 End Function '============================== 'note: see custom Rectangle class module 'e.g. code to put in a macro: Dim oRect As New Rectangle Call oRect.InitValues(10, 2) Debug.Print (oRect.Area) '20 Debug.Print (oRect.Perimeter) '24 Debug.Print (oRect.calcArea()) '20 Debug.Print (oRect.calcPerimeter()) '24 '============================== [Go] package main import "fmt" type Rectangle struct { width int height int } func (oObj *Rectangle) calcArea() int { return oObj.width * oObj.height } func (oObj *Rectangle) calcPerimeter() int { return (oObj.width + oObj.height) * 2 } func main() { oObj := Rectangle{10, 2} fmt.Println(oObj.calcArea()) //20 fmt.Println(oObj.calcPerimeter()) //24 } [Java] class HelloWorld { static class Rectangle { int width; int height; public Rectangle(int vWidth, int vHeight) { this.width = vWidth; this.height = vHeight; } //WARNING: Java lacks computed properties e.g. oObj.MyProp, //the convention is getXXX() and setXXX() methods, //however, you can create ordinary oObj.MyProp properties: //e.g. public String MyPropSimple = "MyProp"; public int getArea() { return this.calcArea(); } public int getPerimeter() { return this.calcPerimeter(); } public int calcArea() { return this.width * this.height; } public int calcPerimeter() { return (this.width + this.height) * 2; } } public static void main(String[] args) { var oRect = new Rectangle(10, 2); //System.out.println(oRect.area); //20 //System.out.println(oRect.perimeter); //24 System.out.println(oRect.getArea()); //20 System.out.println(oRect.getPerimeter()); //24 System.out.println(oRect.calcArea()); //20 System.out.println(oRect.calcPerimeter()); //24 } } [JavaScript] class Rectangle { constructor(vWidth, vHeight) { this.width = vWidth; this.height = vHeight; } get area() { return this.calcArea(); } get perimeter() { return this.calcPerimeter(); } calcArea() { return this.width * this.height; } calcPerimeter() { return (this.width + this.height) * 2; } } oRect = new Rectangle(10, 2); console.log(oRect.area); //20 console.log(oRect.perimeter); //24 console.log(oRect.calcArea()); //20 console.log(oRect.calcPerimeter()); //24 [Kotlin] class Rectangle { var width: Int var height: Int constructor(vWidth: Int, vHeight: Int) { this.width = vWidth this.height = vHeight } var area: Int = 0 get() { return this.calcArea() } private set var perimeter: Int = 0 get() { return this.calcPerimeter() } private set fun calcArea() : Int { return this.width * this.height } fun calcPerimeter() : Int { return (this.width + this.height) * 2 } } var oRect = Rectangle(10, 2) println(oRect.area) //20 println(oRect.perimeter) //24 println(oRect.calcArea()) //20 println(oRect.calcPerimeter()) //24 [PHP] class Rectangle { private $width; private $height; public function __construct($vWidth, $vHeight) { $this->width = $vWidth; $this->height = $vHeight; } public function __get($vProp) //for property getters { if ($vProp == "area") return $this->calcArea(); else if ($vProp == "perimeter") return $this->calcPerimeter(); } public function calcArea() { return $this->width * $this->height; } public function calcPerimeter() { return ($this->width + $this->height) * 2; } } $oRect = new Rectangle(10, 2); var_dump($oRect->area); //20 var_dump($oRect->perimeter); //24 var_dump($oRect->calcArea()); //20 var_dump($oRect->calcPerimeter()); //24 [Python] class Rectangle: def __init__(self, vWidth, vHeight): self.width = vWidth self.height = vHeight @property def area(self): return self.calcArea() @area.setter def area(self, value): return "ERROR" @property def perimeter(self): return self.calcPerimeter() @perimeter.setter def perimeter(self, value): return "ERROR" def calcArea(self): return self.width * self.height def calcPerimeter(self): return (self.width + self.height) * 2 oRect = Rectangle(10, 2) print(oRect.area) #20 print(oRect.perimeter) #24 print(oRect.calcArea()) #20 print(oRect.calcPerimeter()) #24 [R] #note: a reference class (not an S3 or S4 class): Rectangle = setRefClass( "Rectangle", fields = list( width = "numeric", height = "numeric" ), methods = list( initialize = function(width, height) { #note: '<<-': the global assignment operator: width <<- width height <<- height }, calcArea = function() { width * height }, calcPerimeter = function() { (width + height) * 2 } ) ) oRect = Rectangle$new(10, 2) print(oRect$calcArea()) #20 print(oRect$calcPerimeter()) #24 [Ruby] class Rectangle def initialize(vWidth, vHeight) @width = vWidth @height = vHeight end def area #getter (note: a standard method) return self.calcArea() end def area=(vValue) #setter return "ERROR" end def perimeter #getter (note: a standard method) return calcPerimeter() end def perimeter=(vValue) #setter return "ERROR" end def calcArea() return @width * @height end def calcPerimeter() return (@width + @height) * 2 end end oRect = Rectangle.new(10, 2) p oRect.area #20 p oRect.perimeter #24 p oRect.calcArea() #20 p oRect.calcPerimeter() #24 [Rust] struct Rectangle { width: i32, height: i32 } impl Rectangle { fn new(vWidth: i32, vHeight: i32) -> Rectangle { Rectangle { width: vWidth, height: vHeight } } fn calcArea(&self) -> i32 { return self.width * self.height; } fn calcPerimeter(&self) -> i32 { return (self.width + self.height) * 2; } } fn main() { let oRect = Rectangle::new(10, 2); println!("{}", oRect.calcArea()); //20 println!("{}", oRect.calcPerimeter()); //24 let oRect2 = Rectangle{width:10, height:2}; println!("{}", oRect2.calcArea()); //20 println!("{}", oRect2.calcPerimeter()); //24 } [Scala] class Rectangle(vWidth: Int, vHeight: Int) { //constructor start: private var width = vWidth private var height = vHeight //constructor end def area: Int = { return this.calcArea() } def perimeter: Int = { this.calcPerimeter() } def calcArea(): Int = { return this.width * this.height } def calcPerimeter(): Int = { return (this.width + this.height) * 2 } } var oRect = Rectangle(10, 2) println(oRect.area) //20 println(oRect.perimeter) //24 println(oRect.calcArea()) //20 println(oRect.calcPerimeter()) //24 [Swift] class Rectangle { private var width: Int private var height: Int init(_ vWidth: Int, _ vHeight: Int) { self.width = vWidth self.height = vHeight } var area: Int { get { return self.calcArea() } } var perimeter: Int { get { return self.calcPerimeter() } } func calcArea() -> Int { return self.width * self.height } func calcPerimeter() -> Int { return (self.width + self.height) * 2 } } var oRect = Rectangle(10, 2) print(oRect.area) //20 print(oRect.perimeter) //24 print(oRect.calcArea()) //20 print(oRect.calcPerimeter()) //24
The Universal Function Library Project: Details Note: UFL: Universal Function Library, a set of around 100-300 standard functions for all programming languages. UFL notes: Tern ['c ? x : y' is preferable since it's clear/concise/common, else use alternative symbols like Raku ('c ?? x !! y')][a ternary function is a lesser option since it can't do short-circuit Boolean evaluation] recommendation: 'c ? x : y' is extremely clear and concise and very well known, if you create a programming language, you should implement it just like that, and if your programming language for whatever failed to do that (and even if has some alternative syntax ...), choose alternative symbols (e.g. Raku: 'c ?? x !! y') author's note: unlike with 'c ? x : y', where either x or y are not executed, depending on c, for the function 'Tern(c, x, y)', x and y will both be executed) UFL notes: Swap [consider 'Swap(&a, &b)' or 'Swap(a, b)' or a swap statement 'swap a b'][destructuring assignment is unclear when longer variable names are used] author's note: (all of the options aside from 'Swap(&a, &b)' and 'swap a b' are poor) author's note: a function is a fine option, if the language supports functions with ByRef variables: 'Swap(&a, &b)' (or possibly 'Swap(a, b)') author's note: 'swap a b' is a good option for any programming language author's note: 't = a, a = b, b = t', is verbose (e.g. a 3-liner, plus blank lines, plus a comment to say 'swap', plus longer variable names), can easily result in bugs (especially when code is updated and variables are renamed), and it doesn't make the intention clear, a user can't simply search 'swap' to find any swaps in the code author's note: destructuring assignment only looks clear when the variable name ares are unusually short author's note: if all languages implemented 'swap a b' or 'Swap(&a, &b)', this would make it easy to migrate code between programming languages, otherwise simply swapping variables will be a recurring headache author's note: there are huge numbers of views on Stack Overflow etc, on threads re. swapping variables author's note: the only argument against a swap statement/function, is that swapping is relatively uncommon, however, it is relatively verbose, and relatively unsafe author's note: in general, well-known operations should be doable at the level the user thinks of them ('I'm swapping variables a and b', not, 'I'm creating a temporary variable and using it to swap a and b') author's note: in general, well-known operations should be doable in as safe a way as possible, having to name each variable twice, and name a temporary variable twice, and 3 assignments where something may be ordered incorrectly, are chances for more bugs, and reasonably often exactly these bugs do occur author's note: many approaches have the side effect of introducing a temporary variable author's note: oArray.Swap(Key1, Key2) and oMap.Swap(Key1, Key2) are also useful, add a swap statement/function, and then you can easily find variable and object key swaps by searching for 'swap' author's note: a swap statement/function is easier to find, understand and debug than the alternatives author's note: if it can be written badly, it will be written badly, a swap statement/function is very hard to get wrong UFL notes: Sleep [perhaps consider friendly format options e.g. 'm'/'s'] UFL notes: Noop recommendation: the string 'noop' is uncommon, so it is easier to search for than 'nop' and 'pass', which are relatively common substrings of words recommendation: either a noop statement or function is fine recommendation: a noop statement, that can't be overridden, may be useful as it enables the language to be sure it can replace the statement with a noop, no other checks are required recommendation: people use makeshift noops, a built-in noop, makes it easier to find any noops, for later removal recommendation: noops are useful as a line for adding breakpoints recommendation: some versions of noops can be confusing, e.g. '{}', because the programmer must remember not to add a trailing semicolon UFL notes: PrintLn [print string and a newline character] recommendation: a 'PrintLn' that always appends a line break, is more useful than a 'Print' that doesn't, if only one can be picked recommendation: since 'Print' with line break is wanted 99%+ of the time, it is fine for 'Print' to print a line break by default, with an option or separate function to print without a line break UFL notes: TickCount [get the current time in milliseconds (typically centisecond accuracy, or a higher resolution if possible), take the difference of 2 timestamps to get the time elapsed] UFL notes: SetTimer recommendation: it may be worth having 3 options: delay before first occurrence, gap in-between occurrences, number of occurrences UFL notes: Type UFL notes: Error UFL notes: Assert recommendation: it may be worth having variants of this function, e.g. assert that an error would occur if it were run, e.g. instead of 1 param that is a bool, perhaps 2 params (actual value, expected value), perhaps 3 params (function, parameters as an array, expected value)