cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos

Allow Set() to return value

Please allow the Set() function to return value of the variable that was set, ie:

 

 

Set( A, 5 ) ;
Set( B, "abc" );
Set( C, { Name: "Hi", Value: "There" } );

should return the value the respective values were set, ie:

 

 

  • Set( A, 5 ) would return 5 (as a number)
  • Set( B, "abc" ) would return "abc" (as a string)
  • Set( C, { Name: "Hi", Value: "There" } ); would return { Name: "Hi", Value: "There" } (as a record)

etc.


Purpose of this is that it will GREATLY help to simplify complex forumlas (due to the lack of functions or macros within PowerApps), and is similar to the same functionality in C and other languages.  

For example, consider the following code that is designed to look at two collections to determine whether a value is present (firstly choosing from "cAppUserSettings", if blank, then from "cAppDefaults", and finally if blank from the DEFAULT_UI_SCREEN_WIDTH).  

In this case the value we're after can be either "AppWidth", "DesignWidth", or a numeric value representing the actual width.  The code below:

Set( gSettings, 
    UI: {
        ScreenWidth:   If( IsNumeric( Coalesce( Lower( LookUp( cAppDefaults,     crac4_name = "UI.ScreenWidth",     crac4_value ) ),
                                                Lower( LookUp( cAppUserSettings, crac4_name = "UI.ScreenWidth",     crac4_value ) ),
                                                Lower( DEFAULT_UI_SCREEN_WIDTH ) ) ),
                           // Then
                           Switch( Coalesce( Lower( LookUp( cAppDefaults,     crac4_name = "UI.ScreenWidth",     crac4_value ) ),  // TODO: Convert these to enum or bool...
                                                Lower( LookUp( cAppUserSettings, crac4_name = "UI.ScreenWidth",     crac4_value ) ),
                                                Lower( DEFAULT_UI_SCREEN_WIDTH ) ),
"appwidth", App.Width,
"designwidth", App.DesignWidth,
App.Width ), // Else Value( Coalesce( Lower( LookUp( cAppDefaults, crac4_name = "UI.ScreenWidth", crac4_value ) ), // TODO: Convert these to enum or bool... Lower( LookUp( cAppUserSettings, crac4_name = "UI.ScreenWidth", crac4_value ) ), Lower( DEFAULT_UI_SCREEN_WIDTH ) ) ) ) } // UI section

requires that the Coalesce() section be pasted in THREE times, or else defined in separate variables or structure OUTSIDE of the Set( gSettings ) which is wasteful, and confusing, and more to have errors in.  Changing instead to:

 Set( gSettings, UI: {
ScreenWidth: If( IsNumeric( Set( SW, Coalesce( Lower( LookUp( cAppDefaults, crac4_name = "UI.ScreenWidth", crac4_value ) ), Lower( LookUp( cAppUserSettings, crac4_name = "UI.ScreenWidth", crac4_value ) ), Lower( DEFAULT_UI_SCREEN_WIDTH ) ) ) ), // Then Switch( SW, "appwidth", App.Width, "designwidth", App.DesignWidth, App.Width // default ), // Else Value( SW ) )
} // UI section

 

which is clearly SIMPLER, MORE CONSISE, and LESS PRONE TO ERROR (ie: I don't have three Coalesc() statements I need to keep EXACTLY in sync, etc).  

Setting the value of "SW" outside of the larger Set( gSettings ) statement (which in real life has many other members that need to do things like this) is messy, and clearly the above is fairly legible, in comparison to either.

 

(NOTE: this type of thing would not be as needed (though still useful) if PowerApps allowed for macros or functions - as it SHOULD - code reusability/legibility is terrible without these - especially for "utility" functions (that don't need to be implemented with controls).

 



 

Status: New