# Class Contracts: ensure not working with Property gettor?

**URL:** https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928
**Category:** Elements
**Tags:** oxygene
**Created:** [July 15, 2019, 3:02pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928 "2019-07-15T15:02:37Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 3:02pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/1 "2019-07-15T15:02:37Z")

</div>

The following code, does **NOT pass compiler** - what am I missing?

```
property MyFunkyName: String 
  read begin
    result := GetMyFunkyName;
  ensure
    result.Contains(' ');
  end;
```

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [July 15, 2019, 3:09pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/2 "2019-07-15T15:09:49Z")

</div>

> [@wuping](#):
>
> The following code, does **NOT pass compiler** - what am I missing?

Define “not pass compiler”?

---

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 3:26pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/3 "2019-07-15T15:26:29Z")

</div>

@mh marc - sorry I should have been more clear.

The compiler indicated that the code is wrong, as seen in the figure below.

A sample project is attached for reproducing the issue (honestly, I am not sure if it is an issue, or it is just that I didn’t use the “ensure” feature correctly).

 ![2019-07-15_11-19-45](https://talk.remobjects.com/uploads/default/original/2X/b/be23efb79754c8f1abfcbb6d833b0b32fd07ca2b.png)[ConsoleApplication.7z](https://talk.remobjects.com/uploads/short-url/8i5evmCXsy7zP43A9xmhGBBTzKr.7z) (2.7 KB)

---

<div class="post-metadata">

### Author: ![FritzW](https://talk.remobjects.com/user_avatar/talk.remobjects.com/fritzw/32/6428_2.png) [@FritzW](https://talk.remobjects.com/u/FritzW)
#### Post date: [July 15, 2019, 3:58pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/4 "2019-07-15T15:58:13Z")

</div>

That is a block. I don’t think a. block can have an ensure and require part.  
The idea is nice but I think not so easy to manage

---

<div class="post-metadata">

### Author: ![Theo69](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@Theo69](https://talk.remobjects.com/u/Theo69)
#### Post date: [July 15, 2019, 4:01pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/5 "2019-07-15T16:01:02Z")

</div>

I think you are correct.  
This will work:

```auto
property MyFunkyName: String read GetMyFunckyName;
  private method GetMyFunckyName: String begin
    result := GetMyFunkyName;
  ensure
    result.Contains(' ');
  end;

```

---

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 4:07pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/6 "2019-07-15T16:07:44Z")

</div>

@Theo69  
In the solution you outlined, would the private method GetMyFunckyName be only accessible (i.e., local) to property MyFunkyName? Or it can be used by other class members internally as well?

---

<div class="post-metadata">

### Author: ![FritzW](https://talk.remobjects.com/user_avatar/talk.remobjects.com/fritzw/32/6428_2.png) [@FritzW](https://talk.remobjects.com/u/FritzW)
#### Post date: [July 15, 2019, 4:10pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/7 "2019-07-15T16:10:20Z")

</div>

it is a normal private method.

---

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 4:13pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/8 "2019-07-15T16:13:37Z")

</div>

> [@FritzW](#):
>
> it is a normal private method.

Got it. Also thank you for pointing out the “block” nature of that gettor. I think I still would like it to be a block which is " **local" to the property it applies.**

---

<div class="post-metadata">

### Author: ![Theo69](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@Theo69](https://talk.remobjects.com/u/Theo69)
#### Post date: [July 15, 2019, 4:23pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/9 "2019-07-15T16:23:52Z")

</div>

You can do it in a block too, but not with ensure:

```auto
property MyFunkyName: String read begin
    result := GetMyFunkyName;
  {$Ifdef debug}
    assert(result.Contains(' '),"Result does not contain ' '");
  {$endif}
  end;

```

---

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 4:25pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/10 "2019-07-15T16:25:46Z")

</div>

@Theo69 - Yes, I used the “conventional” assert.

---

<div class="post-metadata">

### Author: ![FritzW](https://talk.remobjects.com/user_avatar/talk.remobjects.com/fritzw/32/6428_2.png) [@FritzW](https://talk.remobjects.com/u/FritzW)
#### Post date: [July 15, 2019, 4:36pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/11 "2019-07-15T16:36:28Z")

</div>

Why not simply the ensure in the GetMyFunkyName; ?  
Not sure but I think a property block has also a runtime overhead.

If this is not what you want because of using the GetMyFunkyName at other places  
the resolution from Theo69 but as inline?  
like:

```
 private method GetMyFunckyName: String; inline; 
begin
    result := GetMyFunkyName;
  ensure
    result.Contains(' ');
end;
```

---

<div class="post-metadata">

### Author: ![Theo69](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@Theo69](https://talk.remobjects.com/u/Theo69)
#### Post date: [July 15, 2019, 4:38pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/12 "2019-07-15T16:38:51Z")

</div>

To prevent it to be used from other places:

```auto
[EditorBrowsable(never)]
private method Get_MyFunckyName: String; inline; 
begin
    result := GetMyFunkyName;
  ensure
    result.Contains(' ');
end;

```

---

<div class="post-metadata">

### Author: ![wuping](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/w/5f8ce5/32.png) [@wuping](https://talk.remobjects.com/u/wuping)
#### Post date: [July 15, 2019, 4:38pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/13 "2019-07-15T16:38:54Z")

</div>

@FritzW

The reason (good, or bad) is - GetMyFunkyName is declared as a pure abstract method, to be overridden by a subclass. And I want the parent class to take care of the “post-condition check”.

> [@FritzW](#):
>
> Why not simply the ensure in the GetMyFunkyName; ?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [July 15, 2019, 6:56pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/14 "2019-07-15T18:56:44Z")

</div>

@ck, do we wanna support require/ensure in property getter/setters as shown in the OP?

---

<div class="post-metadata">

### Author: ![FritzW](https://talk.remobjects.com/user_avatar/talk.remobjects.com/fritzw/32/6428_2.png) [@FritzW](https://talk.remobjects.com/u/FritzW)
#### Post date: [July 15, 2019, 7:18pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/15 "2019-07-15T19:18:16Z")

</div>

Out of Interest , are this Kind of setter/ geter compiler inline without runtime costs?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [July 15, 2019, 7:35pm UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/16 "2019-07-15T19:35:40Z")

</div>

They’ll generate the same code as using a getter/setter method, as that’s what required on the runtime level, anyways. You could mark a property inline, but then any underlying field(s) to accesses would have to be public, as well…

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [July 16, 2019, 4:46am UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/17 "2019-07-16T04:46:56Z")

</div>

Do note that on .net and java, the runtime will inline any property access when running optimized (compiled for release, no debugger). On Island the optimizer will do the same kind of thing.

---

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [July 16, 2019, 4:51am UTC](https://talk.remobjects.com/t/class-contracts-ensure-not-working-with-property-gettor/19928/18 "2019-07-16T04:51:24Z")

</div>

Thanks, logged as [bugs://82950](https://www.remobjects.com/portal/bugs/?id=82950)
