Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Retreiving union type var value.

Retreiving union type var value. 14 Jul 2016 11:14 #7862

  • Dominik
  • Dominik's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 4
  • Karma: 0
Hello,

I have a question connected with ttcn-3 - is it possible to retreive type of value from unin type var or retreive union value itself without explicit invocation?

My situation:

I have a list of union type values

type charstirng typeA with {extension "something"}
type charstirng typeB with {extension "somehing1"}

type union U
{
typeA a,
typeB b,
typeC c
}

type record of U R_of_U;

var R_of_U v_list := {c:={}, a:={...}, b:={...}, b:={...}};

I want to verify if my v_list is of typeA or typeB.

I did it with:

if (match(v_list, (a:={?}, b:={?})))
{
//here is my problem! If union value is of expected type - typeA or typeB then I want to retreive this value. Am I able to do that without checking

if (ischosen(v_list.a))
{
var charstring v_c := v_list.a;
...
}

//I can determine expected range of my union value, but is there
// any possibility to retreive this particular value implicitly without v_list.a or v_list.b?
// if not then I would have to repeat if (ischosen(...)) { char_var := v_list.a } in every case
// and such a code is not quite well readable

}


Could you plaese help me with this case?

Best regards,
Dominik
The administrator has disabled public write access.

Retreiving union type var value. 14 Jul 2016 12:15 #7863

Hi Dominik,

union type is something that can store values of different types. I.e. it can store a string value or a boolean value etc. But this also means that you cannot pick e.g. a boolean value from a union that is actually stores a string. But it is not just about the types, the different alternatives typically have different semantical meanings, i.e. in
type union U {
integer cost,
integer noOfItemsAvailable
}
both fields are integers, but most probably they refer to completely different properties.

In other words, if you are not sure which alternative is the actually chosen one in a union value, you shall check it, to avoid runtime error.

Probably a more elegant way to do this, is using select union:
select union (v_list) {
case (a) {v_c := v_list.a}
case (b) {v_c := v_list.b}
}
//pls. note that all v_list -s in the above example should be v_list LEFT SQUARE BRACKET i RIGHT SQUARE BRACKET,
//but the forum's engine is stoling the indexes for some reason.
Last Edit: 14 Jul 2016 12:37 by György Réthy.
The administrator has disabled public write access.
The following user(s) said Thank You: Dominik

Retreiving union type var value. 14 Jul 2016 12:40 #7864

  • Dominik
  • Dominik's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 4
  • Karma: 0
Hello Gyorgy,

thank you for your answer. I think I understand what you have written.

My goal is to verify if union type value matches one types listed in range

my union is

lets assume typeX are integer subtypes

type union U
{
typeA a,
typeB b,
typeC c,
...,
typeZ z
}

I want my union var value to be one of type range -> e.g. typeA or typeB or typeC

var U v_u := {a:=1}
check -> if (match(v_u, range-> ({a:={?}}, {b:={?}}, {c::={?}})

but then I face problem as I want to implicitly retreive v_u value as I know it is one of expected type.

I know that explicitly it can be retreived with if (ischosen(v_u.a)) {v_i := v_u.a} or with select case as you suggested.

My question is: is this possible to get this value implicitly without ischosen + v-u. operator?

Dominik
The administrator has disabled public write access.

Retreiving union type var value. 14 Jul 2016 12:49 #7865

Hi Dominik,

it is not possible to retrieve "any chosen alternative" implicitly; when referring a union value on the RHS, you always shall identify a union field and always exactly one field (unless assigning the union value to another union variable/parameter of the same type).
The administrator has disabled public write access.
  • Page:
  • 1

FacebookTwitterGoogle BookmarksRedditNewsvineTechnoratiLinkedin