# \[SOLVED\] Remove physical field

**URL:** https://community.haxe.org/t/solved-remove-physical-field/3874
**Category:** Haxe
**Created:** [February 25, 2023, 2:57pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874 "2023-02-25T14:57:03Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ClintFlames](https://community.haxe.org/user_avatar/community.haxe.org/clintflames/32/2009_2.png) [@ClintFlames](https://community.haxe.org/u/ClintFlames)
#### Post date: [February 25, 2023, 2:57pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/1 "2023-02-25T14:57:03Z")

</div>

I need to remove physical field without adding setter. Is it possible? (code for example)

```haxe
class Example {
	public var field(get, null):String;

	function get_field() {
		trace(field); // src/Example.hx:5: Useless
		return "Field";
	}

	public function new() {
		field = "Useless";
		trace(field); // src/Example.hx:11: Field
	}

	static function main() { new Example(); }
}

```

---

<div class="post-metadata">

### Author: ![RoBBoR](https://community.haxe.org/user_avatar/community.haxe.org/robbor/32/1881_2.png) [@RoBBoR](https://community.haxe.org/u/RoBBoR)
#### Post date: [February 25, 2023, 4:43pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/2 "2023-02-25T16:43:24Z")

</div>

Perhaps I’m misunderstanding your needs, but have you tried the `never` keyword?

```haxe
public var field(get, never):String;

```

---

<div class="post-metadata">

### Author: ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)
#### Post date: [February 25, 2023, 7:39pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/3 "2023-02-25T19:39:46Z")

</div>

Given the trace results you provided, it seems you want the `default` behavior?

> **[Try Haxe !](https://try.haxe.org/#b87Cd157)**
>
> Haxe is an amazing multiplatform programming language. Check it out !

---

<div class="post-metadata">

### Author: ![ClintFlames](https://community.haxe.org/user_avatar/community.haxe.org/clintflames/32/2009_2.png) [@ClintFlames](https://community.haxe.org/u/ClintFlames)
#### Post date: [February 26, 2023, 11:08am UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/4 "2023-02-26T11:08:07Z")

</div>

Thank you, this is what I needed. I did not even suspect that `never` exists since it is not on the [Rules for getter and setter](https://haxe.org/manual/class-field-property-rules.html) page.

---

<div class="post-metadata">

### Author: ![player\_03](https://community.haxe.org/user_avatar/community.haxe.org/player_03/32/821_2.png) [@player\_03](https://community.haxe.org/u/player_03)
#### Post date: [March 1, 2023, 9:10pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/5 "2023-03-01T21:10:15Z")

</div>

It’s listed on [the parent page](https://haxe.org/manual/class-field-property.html) and the [common access modifier page](https://haxe.org/manual/class-field-property-common-combinations.html) instead. 🙂

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [March 3, 2023, 8:12pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/6 "2023-03-03T20:12:31Z")

</div>

Can someone explain me what the question was? I still do t get it

---

<div class="post-metadata">

### Author: ![davidbruce](https://community.haxe.org/user_avatar/community.haxe.org/davidbruce/32/555_2.png) [@davidbruce](https://community.haxe.org/u/davidbruce)
#### Post date: [March 4, 2023, 6:13pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/7 "2023-03-04T18:13:20Z")

</div>

They did not want to have a setter method associated with their property.

---

<div class="post-metadata">

### Author: ![player\_03](https://community.haxe.org/user_avatar/community.haxe.org/player_03/32/821_2.png) [@player\_03](https://community.haxe.org/u/player_03)
#### Post date: [March 16, 2023, 12:16am UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/8 "2023-03-16T00:16:12Z")

</div>

They wanted to know how to make a non-[physical](https://haxe.org/manual/class-field-property-rules.html#define-physical-field) field.

> Define: Physical field
> 
> A field is considered to be **physical** if it is either
> 
> - a [variable](https://haxe.org/manual/class-field-variable.html)
> - a [property](https://haxe.org/manual/class-field-property.html) with the read-access or write-access identifier being `default` or `null`
> - a [property](https://haxe.org/manual/class-field-property.html) with `:isVar` [metadata](https://haxe.org/manual/lf-metadata.html)

Memory is allocated to store a physical field, while no memory is allocated to store a non-physical field.

In the OP’s example, `field` was physical, so Haxe would always allocate room for a string every time someone called `new Example()`. However, OP wanted to hard-code a value of `"Field"`, stored in the `get_field()` function, so there was no reason to store anything in `field`. Changing `(get, null)` to `(get, never)` made the field non-physical, meaning the compiler would no longer allocate extra memory for each instance.

From the outside, `field` still looks and acts like it stores a value, but the underlying memory is more efficient.

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [March 16, 2023, 12:24pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/9 "2023-03-16T12:24:09Z")

</div>

I was not aware of the existence of physical fields, thank you very much. Most of my targets are dynamic, and I think this applies more to compiled languages like c or c++, right?

---

<div class="post-metadata">

### Author: ![player\_03](https://community.haxe.org/user_avatar/community.haxe.org/player_03/32/821_2.png) [@player\_03](https://community.haxe.org/u/player_03)
#### Post date: [March 23, 2023, 12:56am UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/10 "2023-03-23T00:56:15Z")

</div>

No, it applies to any target that supports class instances. (Which is to say, all of them.)

For instance, consider [this code sample](https://try.haxe.org/#F306F3aa):

```haxe
class SampleClass {
  public var physicalField:Float = 0;
  public var nonPhysicalField(get, never):Float;
  
  public inline function new() {
  }
  
  private function get_nonPhysicalField():Float {
		return physicalField;
  }
}

```

When compiled to JS, it produces the following:

```js
class SampleClass {
	constructor() {
		this.physicalField = 0;
	}
	get_nonPhysicalField() {
		return this.physicalField;
	}
}

```

Haxe only sets `this.physicalField`, which means the browser only has to make space for one value, rather than two.

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [March 23, 2023, 8:59pm UTC](https://community.haxe.org/t/solved-remove-physical-field/3874/11 "2023-03-23T20:59:20Z")

</div>

Oh, I see. thank you very much for the explanation
