Ask Your Question
0

Using class parameter in other parameter

asked 2015-09-03 04:02:19 -0600

Tomasz Olszewski gravatar image

updated 2015-09-03 04:03:08 -0600

Hello,

Could you tell me what is the difference between 2 below examples. Is it bad practice, or is it bug? Because it doesn't work as expected in both situations:

class test(
  $arg1 = 'dsa',
  $arg2 = "My arg is ${arg1}"
) {
  notify { $arg2: }
}

class { 'test': }

And:

class test(
  $arg1 = 'dsa',
  $arg2 = "My arg is ${arg1}"
) { 
  notify { $arg2: }  
}

class { 'test':
  arg1 => 'dsa',
}

In both examples I expected to see "My arg is dsa". In first one it's empty, in second one it works.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-09-03 04:54:36 -0600

i believe this is expected behaviour - the parameterized variables arent instantiated in the test(...) bit - this just sets defaults - so arg1 would be undefined when setting $arg2.

to get what you want try:

class test (
  $arg1 = 'dsa',
  $arg2 = 'my arg is',
) {
   notify { $arg2 $arg1: }
}

class { 'test': }

Cheers Stuart

edit flag offensive delete link more

Comments

As always, order doesn't matter :-/

Tomasz Olszewski gravatar imageTomasz Olszewski ( 2015-09-03 07:02:48 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-03 04:02:19 -0600

Seen: 57 times

Last updated: Sep 03 '15