# Type checking with local variable and a value type

**URL:** https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896
**Category:** Oxygene
**Created:** [January 6, 2021, 4:10pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896 "2021-01-06T16:10:37Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 6, 2021, 4:10pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/1 "2021-01-06T16:10:37Z")

</div>

Hello,  
This has been tested with .NET 5.

In C#, there exists the following:

```auto
if (Value is String s)
{
  // can use the s variable
}

```

In Oxygene, we can use (and it works):

```auto
with matching s := String (Value) do begin
  // can use the s variable
end;

```

**The problem is when trying to use a value type.**

The following runs in C#:

```auto
if (Value is Int32 s)
{
  // can use the s variable
}

```

The same construct generates an `InvalidCastException` in Oxygene:

```auto
with matching s := Int32 (Value) do begin
  // can use the s variable
end;

```

**EDIT** : corrected the Oxygene example, as per the third message.

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 12:29pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/2 "2021-01-08T12:29:53Z")

</div>

UP: no answer!

---

<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: [January 8, 2021, 2:46pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/3 "2021-01-08T14:46:40Z")

</div>

Hmm, I think this is as designed. since value types cant be null, I believe the `()` cast does the same as `as` — raise an exception, since it’s can’t really return nil…

```auto
with s := nullable Int32(Value) do begin

```

might work?

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 2:56pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/4 "2021-01-08T14:56:39Z")

</div>

`nullable Int32` works but it makes a conversion from `Int32` to `nullable Int32`.

I’ve made a little error in my first post: the equivalent in `Oxygene` is:

```auto
with matching s := String (Value) do begin
  // can use the s variable
end;

```

but it still generates an `InvalidCastException` with values types.

What would be nice is if the compiler can generate the following.

**Reference type**

```auto
var s := String (value);
if assigned (s) then
  ...

```

**Value type**

```auto
if Value is Int32 then begin
  var s := Int32 (Value;
  ...
end;
```

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 3:00pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/5 "2021-01-08T15:00:09Z")

</div>

A nice addition would be also for the `case type` construct, as in C#:

```auto
case Value type of
  var s : String : ; // can use local variable s as a String
  var i : Int32 : ; // can use local variable i as an Int32
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: [January 8, 2021, 3:14pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/6 "2021-01-08T15:14:48Z")

</div>

> [@Patrick](#):
>
> `nullable Int32` works but it makes a conversion from `Int32` to `nullable Int32`.

Well, you cant have your cake and eat it too :).

What is `Int32(Value)` _supposed_ to give you? if the result is supposed to be an integer, all it can to is fail (or return 0, with would be worse/really wrong)… If yu want to get back a potential null value, you have to use nullable.

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 3:16pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/7 "2021-01-08T15:16:10Z")

</div>

> [@mh](#):
>
> hat is `Int32(Value)` _supposed_ to give you?

That’s why I corrected the example with `matching`, so it should make sense then, no?

---

<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: [January 8, 2021, 3:19pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/8 "2021-01-08T15:19:51Z")

</div>

> [@Patrick](#):
>
> That’s why I corrected the example with `matching`, so it should make sense then, no?

well, those a re two entirely separate languages constructs

`with matching NEW_VARIABLE := ANY_EXPRESSION do `

will run the statement if `NEW_VARIABLE` is non-null. the `with` statement has no understanding of ANY\_EXPRESSION, nor can (or should) int affect how that expression works

```oxygene
with matching s := Int32(Value) do begin
  // can use the s variable
end;

```

is literally there exact same thing as

```oxygene
var s := Int32(Value);
if assigned(s) then begin
  // can use the s variable
end;

```

and it cannot change what “Int32(Value)” does.

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 3:22pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/9 "2021-01-08T15:22:42Z")

</div>

So, if you prefer, provide a new syntax that let us check the type of a value and assign it to a variable of the right type…

As `with matching` doesn’t work for nullable types, I thought it was a good solution for the syntax.

---

<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: [January 8, 2021, 3:29pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/10 "2021-01-08T15:29:06Z")

</div>

> [@Patrick](#):
>
> So, if you prefer, provide a new syntax that let us check the type of a value and assign it to a variable of the right type…

Yes, we’d have to port over the mess that is C#'s patterns. Not sure if we want to do that to Oxygene, but I’ll bring it up for internal discussion.

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 8, 2021, 3:33pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/11 "2021-01-08T15:33:50Z")

</div>

> [@mh](#):
>
> Not sure if we want to do that to Oxygene

What is more readable?

```auto
if Value is Int32 then begin
  var i := Int32 (Value);
  ...
end;

```

or

```auto
with matching i := Int32 (Value) do begin
end;

```

And for the `case type`:

```auto
case Value type of
  typeOf (Int32) : begin
    var i := Int32 (Value);
    ...
  end;

```

or

```auto
case Value type of
  var i : Int32 : begin // or "i as Int32"
  end;
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: [January 8, 2021, 4:13pm UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/12 "2021-01-08T16:13:25Z")

</div>

> [@Patrick](#):
>
> What is more readable?

not his, for sure, IMHO.

> [@Patrick](#):
>
> ```auto
> case Value type of
> var i : Int32 : begin // or "i as Int32"
> end;
> end;
> 
> ```

---

<div class="post-metadata">

### Author: ![Patrick](https://talk.remobjects.com/user_avatar/talk.remobjects.com/patrick/32/14_2.png) [@Patrick](https://talk.remobjects.com/u/Patrick)
#### Post date: [January 9, 2021, 6:23am UTC](https://talk.remobjects.com/t/type-checking-with-local-variable-and-a-value-type/23896/13 "2021-01-09T06:23:40Z")

</div>

Perhaps with lambda expressions:

```auto
case Value type of
  typeOf (Int32) : i -> ... // with begin...end for multi statements
end;

```
