# Tuple with unpack alternative

**URL:** <https://community.haxe.org/t/tuple-with-unpack-alternative/4578>\
**Category:** Haxe\
**Created:** [March 19, 2025, 1:59pm UTC](https://community.haxe.org/t/tuple-with-unpack-alternative/4578 "2025-03-19T13:59:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neimanpinchas](https://community.haxe.org/user_avatar/community.haxe.org/neimanpinchas/32/2348_2.png) [@neimanpinchas](https://community.haxe.org/u/neimanpinchas)\
**Post date:** [March 19, 2025, 1:59pm UTC](https://community.haxe.org/t/tuple-with-unpack-alternative/4578/1 "2025-03-19T13:59:45Z")

</div>

In rust everything is an expression goes in harmony with tuple and multple assign for example.

```haxe
let (location,timezone)=if is_us {
 ("NEW YORK","EST")
} else {
("PARIS","CET")
}

```

combine that with switch…

compared to the following javascript.

```haxe
if (is_us){
//var leaks out of scope in javascript
var location="NEW YORK";
var tz="EST"
} else {
var location="PARIS";
var tz="CET";
}

```

This is very importent because the rust checker will make you sick and give you no other way of getting the variables in the global scope (I do not remeber exactly the details, I dropped rust a while ago, but decalring upwards and modifying in the inner scope made some limitations.)

In haxe var will not leak, so we cannot directly use the js code, but we could declare the variables earlier, but in turn we lose branch protecting.

```haxe
var location="";
var tz="";
if (is_us){
location="NEW YORK";
tz="EST"
} else {
location="PARIS";
tz="CET";
}

```

I am constantly finding myself with this inconveniance, I made once a macro that takes the if statement as parameter, the list of params, and it does the code generation and merges the scopes, but its not pretty, no intellisense and allocates a temporary array in the resulting code.

What is the correct approach here?

Thanks for any advice

---

<div class="post-metadata">

**Author:** ![alex\_dja](https://community.haxe.org/user_avatar/community.haxe.org/alex_dja/32/294_2.png) [@alex\_dja](https://community.haxe.org/u/alex_dja)\
**Post date:** [March 19, 2025, 3:18pm UTC](https://community.haxe.org/t/tuple-with-unpack-alternative/4578/2 "2025-03-19T15:18:32Z")

</div>

> [@neimanpinchas](#):
>
> `"CET"`

Maybe you should try abstract here like this [Try Haxe!](https://try.haxe.org/#f1EfbadD)

---

<div class="post-metadata">

**Author:** ![acarioni](https://avatars.discourse-cdn.com/v2/letter/a/65b543/32.png) [@acarioni](https://community.haxe.org/u/acarioni)\
**Post date:** [March 19, 2025, 3:39pm UTC](https://community.haxe.org/t/tuple-with-unpack-alternative/4578/3 "2025-03-19T15:39:09Z")

</div>

What’s wrong with this?

```haxe
typedef TS = {location: String, tz: String};

class Test {
  static function main() {
    var ts: TS;
    if (is_us) {
      ts = {location: "a", tz: "b"};
    } else {
      ts = {location: "a", tz: "b"};
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Igazine](https://community.haxe.org/user_avatar/community.haxe.org/igazine/32/1745_2.png) [@Igazine](https://community.haxe.org/u/Igazine)\
**Post date:** [March 19, 2025, 7:17pm UTC](https://community.haxe.org/t/tuple-with-unpack-alternative/4578/4 "2025-03-19T19:17:54Z")

</div>

```haxe
enum Tuple {
	Timezone(location:String, tz:String);
}

class Test {
	static function main() {
		var is_us = false;
		var mytz1:Tuple = Tuple.Timezone(
			(is_us) ? 'NEW YORK' : 'PARIS',
			(is_us) ? 'EST' : 'CET'
		);
		var mytz2:Tuple = (is_us) ? Tuple.Timezone('NEW YORK', 'EST') : Tuple.Timezone('PARIS', 'CET');
		switch mytz1 {
			case Timezone(location, tz):
				trace('Location: ${location}, Timezone: ${tz}');
			default:
		}
		switch mytz2 {
			case Timezone(location, tz):
				trace('Location: ${location}, Timezone: ${tz}');
			default:
		}
	}
}

```

[Try Haxe!](https://try.haxe.org/#8997c36c)
