# Primary constructor

**URL:** <https://forum.modular.com/t/primary-constructor/3511>\
**Category:** General\
**Tags:** mojo-language-design\
**Created:** [September 19, 2026, 10:34pm UTC](https://forum.modular.com/t/primary-constructor/3511 "2026-09-19T22:34:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BoldCrane38](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@BoldCrane38](https://forum.modular.com/u/BoldCrane38)\
**Post date:** [September 19, 2026, 10:34pm UTC](https://forum.modular.com/t/primary-constructor/3511/1 "2026-09-19T22:34:08Z")

</div>

```auto
struct St[T: AnyType]{
# Declares both fields a and b.
  var a: T,
  var b: T
}
# Use here something like @ or params, idk
@(
# Doesn't declare a field. It are regular parameter to be used inside the struct block
  p1: Int,
  p2: Int
)(Copyable):
  var field_non_instantiable_initialized: Int = 4
  var c: Int = p1 + p2
  var d: Int = c + field_non_instantiable_initialized

# I don't know how exactly would be declared get and set in languages like Mojo and Python, so, please overlook it.
  var e: Int = block:
    get: c + field_non_instantiable_initialized
    set: c = newValue
    # or
    set(newC): c = newC

# accessible only inside the struct
  priv var f: p1 + 5

def main():
  var st: St = St[String]{a = "a", b = "b"}(p1 = 1, p2 = 2)

  print(st.p1) # error, p1 is not a field

  print(st.f) # error, f is private

  print(st.field_non_instantiable_initialized) # prints 4

  print(st.c) # access c, which is p1 + p2, in this case: 3

  print(st.d) # access d, which is c + field_non_instantiable_initialized
, in this case: 7

  print(st.e) # access e, which is c + field_non_instantiable_initialized, in this case: 7

  st.c = 6

  print(st.d) # continues being 7

  print(st.e) # now is 10

```

## References:

> **[Primary constructors](https://dart.dev/language/primary-constructors)**
>
> Create classes concisely by declaring parameters and fields in the class header.

> **[Primary constructor - Classes | Kotlin](https://kotlinlang.org/docs/classes.html#primary-constructor)**
>
> The primary constructor sets up the initial state of an instance when it's created.

> <https://github.com/igotfr/Ideal/blob/main/design/struct/regular_struct.md>

> <https://github.com/igotfr/Ideal/blob/main/design/struct/parameters_and_get_and_set_in_struct.md>

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [September 20, 2026, 1:40am UTC](https://forum.modular.com/t/primary-constructor/3511/2 "2026-09-20T01:40:40Z")

</div>

“Primary constructors” are a cool feature, but are just “syntactic sugar”. These aren’t things we’ve had time to fully consider and are not something that is a priority unless unambiguous. That said, we do have initializer lists which serve the same purpose, try them out!
