static func — examples
← all topics · 49 examples · page 1 of 1 · raw source ↓
ILEmitterTests_Coverage_Collections__RP_FullRoutePattern_TryMatchAndBuild
static func runnable verifiedRuns `lenArgs` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Collections.cs::RP_FullRoutePattern_TryMatchAndBuild topic: static-func status: verified
// verified behavior: Test.lenArgs(...) == false
namespace Test
pub static func RoutePattern {
let EmptyParams: IReadOnlyDictionary<string, string> = Dictionary<string, string>(0)
pub func TryMatch(pattern: string, path: string, out result: IReadOnlyDictionary<string, string>) -> bool {
let pSegs = pattern.TrimStart('/').Split('/', StringSplitOptions.RemoveEmptyEntries)
let hSegs = path.TrimStart('/').Split('/', StringSplitOptions.RemoveEmptyEntries)
if pSegs.Length != hSegs.Length {
result = EmptyParams
return false
}
var captured: Dictionary<string, string> = nil
var i = 0
while i < pSegs.Length {
let seg = pSegs[i]
if seg.Length >= 2 && seg[0] == '{' && seg[seg.Length - 1] == '}' {
if captured == nil { captured = Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) }
captured[seg.Substring(1, seg.Length - 2)] = hSegs[i]
} else {
if !string.Equals(seg, hSegs[i], StringComparison.OrdinalIgnoreCase) {
result = EmptyParams
return false
}
}
i += 1
}
if captured == nil { result = EmptyParams } else { result = captured }
return true
}
pub func Build(pattern: string, valueFor: Func<string, string?>) -> string? {
let pSegs = pattern.TrimStart('/').Split('/', StringSplitOptions.RemoveEmptyEntries)
var built = List<string>()
var i = 0
while i < pSegs.Length {
let seg = pSegs[i]
if seg.Length >= 2 && seg[0] == '{' && seg[seg.Length - 1] == '}' {
let v = valueFor(seg.Substring(1, seg.Length - 2))
if v == nil { return nil }
built.Add(v)
} else {
built.Add(seg)
}
i += 1
}
return "/" + string.Join('/', built)
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Data.cs::StaticFunc_Bag topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Password {
let MIN: int = 8
func strong(s: string) -> bool {
return s.Length > MIN
}
}
func go() -> bool {
return Password.strong("hunter2hunter2")
}Runs `go` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::NoReturnsClause_OnlyExplicitArrowsTakeEffect topic: static-func status: verified
// verified behavior: Test.go(...) == 1
namespace Test
static func S {
func a() { } // void
func b() -> int { return 1 }
}
func go() -> int {
S.a()
return S.b()
}ILEmitterTests_Returns__Returns_Clause_On_StaticFunc_All_Methods_Without_Arrow_Get_Type
static func runnable verifiedRuns `go` → "ab"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_StaticFunc_All_Methods_Without_Arrow_Get_Type topic: static-func status: verified
// verified behavior: Test.go(...) == "ab"
namespace Test
static func Strs {
returns string
func a() { return "a" }
func b() { return "b" }
}
func go() -> string { return Strs.a() + Strs.b() }ILEmitterTests_Returns__Returns_Clause_On_StaticFunc_Bound_Method_Has_Correct_ReturnType
static func unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_StaticFunc_Bound_Method_Has_Correct_ReturnType topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func S {
returns int
func go() { return 99 }
}ILEmitterTests_Returns__Returns_Clause_On_StaticFunc_Explicit_Arrow_Wins
static func runnable verifiedRuns `combine` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_StaticFunc_Explicit_Arrow_Wins topic: static-func status: verified
// verified behavior: Test.combine(...) == "hello"
namespace Test
static func S {
returns int
func num() { return 5 }
func tag() -> string { return "hello" }
}
func combine() -> string { return S.tag() }ILEmitterTests_Returns__Returns_Clause_On_StaticFunc_Internal_Cross_Method_Call_Sees_Type
static func runnable verifiedRuns `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_StaticFunc_Internal_Cross_Method_Call_Sees_Type topic: static-func status: verified
// verified behavior: Test.go(...) == 5
namespace Test
static func IntBag {
returns int
func two() { return 2 }
func three() { return 3 }
func sum() { return two() + three() }
}
func go() -> int { return IntBag.sum() }ILEmitterTests_Returns__Returns_Clause_On_StaticFunc_Sets_Default_Return_Type
static func runnable verifiedRuns `main` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_StaticFunc_Sets_Default_Return_Type topic: static-func status: verified
// verified behavior: Test.main(...) == 5
namespace Test
static func IntBag {
returns int
func two() { return 2 }
func three() { return 3 }
func sum() { return two() + three() }
}
func main() -> int { return IntBag.sum() }Runs `go` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_With_Primitive_Type topic: static-func status: verified
// verified behavior: Test.go(...) == false
namespace Test
static func B {
returns bool
func t() { return true }
func f() { return false }
func both() { return t() and f() }
}
func go() -> bool { return B.both() }Runs `go` → "hi"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_With_String_Type topic: static-func status: verified
// verified behavior: Test.go(...) == "hi"
namespace Test
static func S {
returns string
func hi() { return "hi" }
}
func go() -> string { return S.hi() }Runs `test` → 25
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_With_Two_Methods_Both_Inherit topic: static-func status: verified
// verified behavior: Test.test(...) == 25
namespace Test
static func Calc {
returns int
func double_it(n: int) = n * 2
func triple_it(n: int) = n * 3
}
func test() -> int = Calc.double_it(5) + Calc.triple_it(5)Runs `sum` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_With_User_Data_Type topic: static-func status: verified
// verified behavior: Test.sum(...) == 2
namespace Test
data Point {
x: int
y: int
}
static func PointBag {
returns Point
func origin() { return Point { x: 0, y: 0 } }
func one() { return Point { x: 1, y: 1 } }
}
func sum() -> int {
let p = PointBag.one()
return p.x + p.y
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::DottedNamespace_Data_EmitsIntoFullNamespace topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Keystone.Routing.Demo
pub data Point { x: int, y: int }ILEmitterTests_StaticFunc__DottedNamespace_FreeFunction_HostsOnLastSegmentModuleClass
static func unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::DottedNamespace_FreeFunction_HostsOnLastSegmentModuleClass topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Keystone.Routing.Demo
func add(a: int, b: int) -> int { return a + b }ILEmitterTests_StaticFunc__DottedNamespace_StaticFunc_EmitsIntoFullNamespace
static func runnable verifiedRuns `null` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::DottedNamespace_StaticFunc_EmitsIntoFullNamespace topic: static-func status: verified
// verified behavior: Test.null(...) == 7
namespace Keystone.Routing.Demo
static func RoutePattern {
func answer() -> int { return 7 }
}ILEmitterTests_StaticFunc__Static_Func_Const_Inlined_As_Literal_At_Use_Site
static func runnable verifiedRuns `test` → 99
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::Static_Func_Const_Inlined_As_Literal_At_Use_Site topic: static-func status: verified
// verified behavior: Test.test(...) == 99
namespace Test
static func Config {
let MAX: int = 99
}
func test() -> int = Config.MAXILEmitterTests_StaticFunc__Static_Func_Method_Called_Across_Two_Static_Funcs
static func runnable verifiedRuns `test` → 21
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::Static_Func_Method_Called_Across_Two_Static_Funcs topic: static-func status: verified
// verified behavior: Test.test(...) == 21
namespace Test
static func A {
func twice(n: int) -> int = n * 2
}
static func B {
func via_a(n: int) -> int = A.twice(n) + 1
}
func test() -> int = B.via_a(10)Runs `test` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::Static_Func_Two_Methods_Independent_State topic: static-func status: verified
// verified behavior: Test.test(...) == 6
namespace Test
static func Counter {
var n: int = 0
func bump() -> int {
n = n + 1
return n
}
}
func test() -> int {
let a = Counter.bump()
let b = Counter.bump()
let c = Counter.bump()
return a + b + c
}Runs `go` → 11
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::Static_Is_Still_Valid_Identifier_Elsewhere topic: static-func status: verified
// verified behavior: Test.go(...) == 11
namespace Test
func go(static: int) -> int { return static + 1 }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_BasicDeclaration_Parses topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Foo {
func go() -> int { return 1 }
}Runs `check` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Bool_Const topic: static-func status: verified
// verified behavior: Test.check(...) == true
namespace Test
static func Flags {
let DEBUG: bool = true
}
func check() -> bool { return Flags.DEBUG }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Const_Field_Loaded_Inline_In_IL topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func K {
let V: int = 42
}
func read() -> int { return K.V }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Default_Emits_Internal_Class topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func PrivSf {
func go() -> int { return 2 }
}ILEmitterTests_StaticFunc__StaticFunc_Emits_TopLevel_Static_Class_Shape
static func unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Emits_TopLevel_Static_Class_Shape topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Holder {
let X: int = 7
func make() -> int { return X }
}ILEmitterTests_StaticFunc__StaticFunc_Let_NonLiteral_Field_Emits_Readonly_Static
static func unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Let_NonLiteral_Field_Emits_Readonly_Static topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Holder {
let s: string = "hi"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Literal_Field_Emits_Const topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Holder {
let X: int = 7
}ILEmitterTests_StaticFunc__StaticFunc_Method_Call_From_Another_StaticFunc
static func runnable verifiedRuns `go` → 11
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Method_Call_From_Another_StaticFunc topic: static-func status: verified
// verified behavior: Test.go(...) == 11
namespace Test
static func A {
func produce() -> int { return 10 }
}
static func B {
func consume() -> int { return A.produce() + 1 }
}
func go() -> int { return B.consume() }Runs `main` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Method_Call_From_TopLevel topic: static-func status: verified
// verified behavior: Test.main(...) == true
namespace Test
static func Password {
func is_strong(s: string) -> bool { return s.Length > 8 }
}
func main() -> bool { return Password.is_strong("hunter2hunter2") }Runs `go` → "<ok>"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Method_Calls_BCL topic: static-func status: verified
// verified behavior: Test.go(...) == "<ok>"
namespace Test
static func Str {
func tag(s: string) -> string { return "<" + s + ">" }
}
func go() -> string { return Str.tag("ok") }Runs `go` → 12
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Method_With_Multiple_Parameters topic: static-func status: verified
// verified behavior: Test.go(...) == 12
namespace Test
static func Calc {
func add(a: int, b: int) -> int { return a + b }
func sub(a: int, b: int) -> int { return a - b }
}
func go() -> int { return Calc.add(3, 4) + Calc.sub(10, 5) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Methods_Are_Static topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Util {
func go() -> int { return 42 }
}Runs `combined` → 43
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Mixed_Fields_And_Methods topic: static-func status: verified
// verified behavior: Test.combined(...) == 43
namespace Test
static func Math2 {
let PI: int = 3
func double(x: int) -> int { return x * 2 }
func triple(x: int) -> int { return x * 3 }
}
func combined() -> int {
return Math2.double(5) + Math2.triple(10) + Math2.PI
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Pub_Emits_Public_Class topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
pub static func PubSf {
func go() -> int { return 1 }
}Runs `sum` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_References_User_Data_Type topic: static-func status: verified
// verified behavior: Test.sum(...) == 0
namespace Test
data Point {
x: int
y: int
}
static func Geom {
func origin() -> Point { return Point { x: 0, y: 0 } }
}
func sum() -> int {
let p = Geom.origin()
return p.x + p.y
}Runs `get_name` → "Esharp"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_String_Const topic: static-func status: verified
// verified behavior: Test.get_name(...) == "Esharp"
namespace Test
static func App {
let NAME: string = "Esharp"
}
func get_name() -> string { return App.NAME }ILEmitterTests_StaticFunc__StaticFunc_Type_Is_TopLevel_Not_Nested_In_Module_Class
static func unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Type_Is_TopLevel_Not_Nested_In_Module_Class topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Sibling {
func go() -> int { return 1 }
}
func host() -> int { return Sibling.go() }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_Var_Field_Emits_Mutable_Static topic: static-func status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
static func Counter {
var n: int = 0
}Runs `span` → 105
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_With_Only_Constants topic: static-func status: verified
// verified behavior: Test.span(...) == 105
namespace Test
static func Limits {
let MAX: int = 100
let MIN: int = -5
}
func span() -> int { return Limits.MAX - Limits.MIN }Runs `go` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_With_Only_Functions topic: static-func status: verified
// verified behavior: Test.go(...) == 3
namespace Test
static func Util {
func one() -> int { return 1 }
func two() -> int { return 2 }
}
func go() -> int { return Util.one() + Util.two() }Runs `test` → 1221
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Counter_Closure_Via_StaticFunc_Pin topic: static-func status: verified
// verified behavior: Test.test(...) == 1221
namespace Test
static func Counter {
var n: int = 0
func bump() -> int {
n = n + 1
return n
}
func peek() -> int = n
func reset() -> int {
n = 0
return n
}
}
func test() -> int {
let a = Counter.bump()
let b = Counter.bump()
let c = Counter.peek()
let _ = Counter.reset()
let d = Counter.bump()
return a * 1000 + b * 100 + c * 10 + d
}Runs `test` → 1201
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::State_Machine_With_Var_Field_Pin topic: static-func status: verified
// verified behavior: Test.test(...) == 1201
namespace Test
static func Light {
var state: int = 0
func next() -> int {
state = (state + 1) % 3
return state
}
func reset() -> int {
state = 0
return state
}
}
func test() -> int {
let a = Light.next()
let b = Light.next()
let c = Light.next()
let d = Light.next()
return a * 1000 + b * 100 + c * 10 + d
}Runs `test` → 3142
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::StaticFunc_Const_Field_Read topic: static-func status: verified
// verified behavior: Test.test(...) == 3142
namespace Test
static func Math {
let PI_TIMES_THOUSAND: int = 3142
}
func test() -> int = Math.PI_TIMES_THOUSANDRuns `test` → "anonymous"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::StaticFunc_Const_String_Field topic: static-func status: verified
// verified behavior: Test.test(...) == "anonymous"
namespace Test
static func Names {
let DEFAULT: string = "anonymous"
}
func test() -> string = Names.DEFAULTRuns `test` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::StaticFunc_Method_Call topic: static-func status: verified
// verified behavior: Test.test(...) == 42
namespace Test
static func Util {
func plus_one(n: int) -> int = n + 1
}
func test() -> int = Util.plus_one(41)Runs `test` → 3032
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::StaticFunc_Multiple_Const_Fields topic: static-func status: verified
// verified behavior: Test.test(...) == 3032
namespace Test
static func Config {
let WIDTH: int = 1920
let HEIGHT: int = 1080
let DEPTH: int = 32
}
func test() -> int = Config.WIDTH + Config.HEIGHT + Config.DEPTHRuns `test` → 17
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::StaticFunc_Multiple_Methods topic: static-func status: verified
// verified behavior: Test.test(...) == 17
namespace Test
static func Geo {
func square(n: int) -> int = n * n
func cube(n: int) -> int = n * n * n
}
func test() -> int = Geo.square(3) + Geo.cube(2)Showcase example
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: interfaces_and_static.es topic: static-func status: verified
// hand-authored, idiomatic E# — verified through the E# compiler
namespace Demo
// ═════════════════════════════════════════════════════════════════════════════
// E#'s object side: interfaces, the two kinds of type that satisfy them, methods that
// live inside a `ref data` body, and a `static func` static class.
//
// An `interface` is nominal: a type satisfies it only by NAMING it after `:` — there is
// no structural auto-matching. Both kinds of user type can conform:
//
// • a value `data` conforms by PROMOTION — a free `func` whose first parameter is the
// data type becomes the interface method. (Storing a value type behind an interface
// boxes it; the compiler warns. Use `ref data` when you'll pass it as the interface a
// lot.)
// • a `ref data` conforms with methods written INSIDE its body (they get an implicit
// `self`), and constructs via an `init` block.
//
// A `static func Name { ... }` is a static class: a bag of constants and stateless
// functions, called `Name.member(...)`.
// ═════════════════════════════════════════════════════════════════════════════
// The contract. By .NET convention interface names start with `I`.
interface IArea {
func area() -> int
}
// A value `data` conforming via promotion. `func area(r: Rect)` has `Rect` as its first
// parameter, so it IS `Rect.area()` — and that satisfies `IArea`. (Passing a `Rect` where
// an `IArea` is expected boxes the struct; fine here, the compiler just notes it.)
data Rect : IArea {
w: int
h: int
}
func area(r: Rect) -> int {
return r.w * r.h
}
// A `ref data` conforming with an in-body method. It has identity, an `init` constructor,
// and `self` inside its methods.
ref data Disk : IArea {
radius: int
init(r: int) {
self.radius = r
}
// Methods inside a `ref data` body read/write fields through `self`. This one
// satisfies `IArea.area()`. (Rough integer area, π ≈ 3.)
func area() -> int {
return 3 * self.radius * self.radius
}
}
// Dispatch through the interface: `describe` neither knows nor cares whether it holds a
// `Rect` or a `Disk` — it calls `area()` virtually. This is the one place E# reaches for
// the object world on purpose.
func describe(s: IArea) -> int {
return s.area()
}
// A `static func` static class: constants + stateless helpers, reached as `Geo.member`.
static func Geo {
const UNIT = 1
func unitSquare() -> Rect {
return Rect { w: UNIT, h: UNIT }
}
func biggest(a: int, b: int) -> int {
return a > b ? a : b
}
}
func main() -> int {
let r = Rect { w: 3, h: 4 } // value type
let d = Disk(5) // ref data via init
let viaInterface = describe(r) + describe(d) // 12 + 75 = 87 (both as IArea)
let unit = Geo.unitSquare().area() // 1 (static class → promoted method)
let pick = Geo.biggest(viaInterface, 100) // 100
return viaInterface + unit + pick // 87 + 1 + 100 = 188
}
Rejected at compile time: ES1010
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_RejectsBareReturn topic: static-func status: unverified
// verified behavior: reports diagnostic ES1010
namespace Test
static func Bad {
return 1
}Rejected at compile time: ES1010
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StaticFunc.cs::StaticFunc_RejectsTopLevelStatements topic: static-func status: unverified
// verified behavior: reports diagnostic ES1010
namespace Test
static func Bad {
let X: int = 1
if true { let y = 2 }
}