I'm calling a method of a class dynamically like this, and it works:
CALL METHOD (gc_zcl_mapping_methods)=>(<ls_import_params>-attr_logic_method)
EXPORTING
iv_dats = <lv_value>
RECEIVING
rv_timestamp = <ls_import_params>-attr_value.
The problem is that I'm naming the two parameters iv_dats and rv_timestamp.
Not every method has these parameter names so I'm looking for a way to call them without naming the parameters.
I tried like this, but I get many syntax errors.
<ls_import_params>-attr_value = (gc_zcl_mapping_methods)=>(<ls_import_params>-attr_logic_method)(<lv_value>).
Is there a correct syntax for my goal to omit the parameter names?
Unfortunately, you have to name the returning parameter and other parameters too.
The full dynamic calling is explained here in the ABAP documentation
For your example code, that will give this code :
DATA(ptab) = VALUE abap_parmbind_tab(
( name = 'IV_DATS'
kind = cl_abap_objectdescr=>exporting
value = REF #( <lv_value> ) )
( name = 'RV_TIMESTAMP'
kind = cl_abap_objectdescr=>receiving
value = REF #( <ls_import_params>-attr_value ) ) ).
DATA(etab) = VALUE abap_excpbind_tab( ).
CALL METHOD (gc_zcl_mapping_methods)=>(<ls_import_params>-attr_logic_method)
PARAMETER-TABLE ptab
EXCEPTION-TABLE etab.
You may get the name of the returning parameter by using RTTI (here, I suppose that the class is global), you may also adapt it to get other parameter names:
DATA(rtti_class) = CAST cl_abap_objectdescr(
cl_abap_typedescr=>describe_by_name( '\CLASS=' && gc_zcl_mapping_methods ) ).
DATA(method) = rtti_class->methods[ name = <ls_import_params>-attr_logic_method ].
DATA(returning_parameter) = method-parameters[ parm_kind = cl_abap_objectdescr=>returning ].
DATA(returning_parameter_name) = returning_parameter-name.
I let you add the handling of exceptions.
ADDENDUM : Minimal, Complete, and Verifiable Example :
Let's say this static call is to be done dynamically :
DATA(result) = VALUE sychar01( ).
DATA(ltxttab) = VALUE rslinltab( ( kind = 'E' numb = 1 param = 'MSGID' value = 'MESSAGEGY9' )
( kind = 'E' numb = 1 param = 'PRAGMA' value = '##DUMMY' ) ).
cl_abap_syntax_message=>long_text_exists(
EXPORTING
p_kind = 'E'
p_number = 1
p_ltxttab = ltxttab
RECEIVING
p_result = result
EXCEPTIONS
message_not_found = 1 ).
Dynamic version (also, the RECEIVING parameter is being determined dynamically at runtime, but usually it's not needed because its name is known at compile time) :
DATA(rtti_class) = CAST cl_abap_objectdescr(
cl_abap_typedescr=>describe_by_name( '\CLASS=' && 'CL_ABAP_SYNTAX_MESSAGE' ) ).
DATA(method) = rtti_class->methods[ name = 'LONG_TEXT_EXISTS' ].
DATA(returning_parameter) = method-parameters[ parm_kind = cl_abap_objectdescr=>returning ].
DATA(returning_parameter_name) = returning_parameter-name.
ASSERT returning_parameter_name = 'P_RESULT'. " <=== just for the demo
DATA(ptab) = VALUE abap_parmbind_tab(
( name = 'P_KIND'
kind = cl_abap_objectdescr=>exporting
value = REF #( 'E' ) )
( name = 'P_NUMBER'
kind = cl_abap_objectdescr=>exporting
value = REF #( 1 ) )
( name = 'P_LTXTTAB'
kind = cl_abap_objectdescr=>exporting
value = REF #( ltxttab ) )
( name = returning_parameter_name
kind = cl_abap_objectdescr=>receiving
value = REF #( result ) ) ).
DATA(etab) = VALUE abap_excpbind_tab(
( name = 'MESSAGE_NOT_FOUND' value = 1 ) ).
CALL METHOD ('CL_ABAP_SYNTAX_MESSAGE')=>('LONG_TEXT_EXISTS')
PARAMETER-TABLE ptab
EXCEPTION-TABLE etab.
Related
I have created a SALV tree, using the CL_SALV_TREE class, the output is shown in the following image:
Now for the export in Excel that works correctly for me, I have used the following code, but the problem is that it does not export the subtotals, so how could I add the subtotals or how could I solve it? since apparently the aggregation functions can't be visible in the ALV tree.
CLASS lcl_tree IMPLEMENTATION.
METHOD export_tree.
DATA: lr_data TYPE REF TO data,
lt_spfli TYPE STANDARD TABLE OF spfli,
levels TYPE TABLE OF rsplf_srv_p.
DATA: lr_zip TYPE REF TO cl_abap_zip,
lr_xlnode TYPE REF TO if_ixml_node,
lr_xldimension TYPE REF TO if_ixml_node,
lr_file TYPE REF TO cl_xml_document,
lr_xlrows TYPE REF TO if_ixml_node_list,
lr_xlrow TYPE REF TO if_ixml_element,
lr_xlformat TYPE REF TO if_ixml_element,
lr_xlworksheet TYPE REF TO if_ixml_element.
FIELD-SYMBOLS: <spfli> TYPE spfli.
DATA(lt_nodes) = go_alv_tree->get_nodes( )->get_all_nodes( ).
LOOP AT lt_nodes INTO DATA(ls_node).
DATA(lr_node) = ls_node-node.
DATA(lv_level) = 0.
DO.
TRY.
lr_node = lr_node->get_parent( ).
lv_level = lv_level + 1.
CATCH cx_salv_msg.
EXIT.
ENDTRY.
ENDDO.
APPEND VALUE rsplf_srv_p( indx = sy-tabix value = lv_level ) TO levels.
lr_data = ls_node-node->get_data_row( ).
ASSIGN lr_data->* TO <spfli>.
APPEND <spfli> TO lt_spfli.
ENDLOOP.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lr_table)
CHANGING
t_table = lt_spfli ).
DATA(lv_xlsx) = lr_table->to_xml( if_salv_bs_xml=>c_type_xlsx ).
CREATE OBJECT lr_zip.
lr_zip->load( lv_xlsx ).
lr_zip->get( EXPORTING name = 'xl/worksheets/sheet1.xml' IMPORTING
content = DATA(lv_file) ).
CREATE OBJECT lr_file.
lr_file->parse_xstring( lv_file ).
* Row elements are under SheetData
lr_xlnode = lr_file->find_node( 'sheetData' ).
lr_xlrows = lr_xlnode->get_children( ).
DO lr_xlrows->get_length( ) TIMES.
lr_xlrow ?= lr_xlrows->get_item( sy-index - 1 ).
READ TABLE lt_nodes INTO ls_node INDEX sy-index - 1. "find this row
in tree
IF sy-subrc = 0.
READ TABLE levels ASSIGNING FIELD-SYMBOL(<line_level>) INDEX sy-index.
* Find the level of the node
CHECK <line_level>-value - 1 NE 0.
* Assign the level to row
lr_xlrow->set_attribute( name = 'outlineLevel' value = condense( CONV string( <line_level>-value - 1 ) ) ).
lr_xlrow->set_attribute( name = 'hidden' value = 'true' ).
ENDIF.
ENDDO.
* Create new element in the XML file
lr_xlworksheet ?= lr_file->find_node( 'worksheet' ).
DATA(lr_xlsheetpr) = cl_ixml=>create( )->create_document( )->create_element( name = 'sheetPr' ).
DATA(lr_xloutlinepr) = cl_ixml=>create( )->create_document( )->create_element( name = 'outlinePr' ).
lr_xlsheetpr->if_ixml_node~append_child( lr_xloutlinepr ).
lr_xloutlinepr->set_attribute( name = 'summaryBelow' value = 'false' ).
lr_xldimension ?= lr_file->find_node( 'dimension' ).
lr_xlworksheet->if_ixml_node~insert_child( new_child = lr_xlsheetpr ref_child = lr_xldimension ).
* Create xstring and move it to XLSX
lr_file->render_2_xstring( IMPORTING stream = lv_file ).
lr_zip->delete( EXPORTING name = 'xl/worksheets/sheet1.xml' ).
lr_zip->add( EXPORTING name = 'xl/worksheets/sheet1.xml' content = lv_file ).
lv_xlsx = lr_zip->save( ).
DATA lv_size TYPE i.
DATA lt_bintab TYPE solix_tab.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xlsx
IMPORTING
output_length = lv_size
TABLES
binary_tab = lt_bintab.
CHECK lt_bintab IS NOT INITIAL.
DATA(p_file) = cl_openxml_helper=>browse_local_file_open( iv_title = 'Save to XLSX File' iv_filename = '' iv_extpattern = 'All files(*.*)|*.*' ).
cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_size
filename = p_file && `.xlsx`
filetype = 'BIN'
CHANGING data_tab = lt_bintab ).
ENDMETHOD.
ENDCLASS.
I tried to replicate the code from this link: https://blogs.sap.com/2015/07/24/salv-tree-to-excel-xlsx/comment-page-1/#comment-658453
There, what it does is add this add-corresponding statement before adding it to the ALV, but that doesn't work on classes which I'm using in my program:
"ADD-CORRESPONDING" is not supported in the OO context
If your question is simplified to what is the code equivalent to add-corresponding in the "OO context", this is one possible answer, I propose the method add_corresponding below, and a test code to demonstrate how it works - this code compiles in 7.40 SP08:
CLASS lcx_add_corresp_not_all_struct DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS add_corresponding IMPORTING from_struct TYPE any
CHANGING to_struct TYPE any
RAISING lcx_add_corresp_not_all_struct
cx_sy_conversion_overflow.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD add_corresponding.
TYPES: ty_names TYPE HASHED TABLE OF abap_compname WITH UNIQUE KEY table_line,
ty_names_in_structs TYPE STANDARD TABLE OF ty_names WITH EMPTY KEY,
ty_table_rtti TYPE STANDARD TABLE OF REF TO cl_abap_typedescr WITH EMPTY KEY.
DATA(rtti_from_struct) = cl_abap_typedescr=>describe_by_data( from_struct ).
DATA(rtti_to_struct) = cl_abap_typedescr=>describe_by_data( to_struct ).
IF rtti_from_struct->kind <> rtti_from_struct->kind_struct
OR rtti_to_struct->kind <> rtti_to_struct->kind_struct.
RAISE EXCEPTION NEW lcx_add_corresp_not_all_struct( ).
ENDIF.
DATA(names_in_structs) = VALUE ty_names_in_structs(
FOR rtti IN VALUE ty_table_rtti( ( rtti_from_struct ) ( rtti_to_struct ) )
( VALUE #( FOR <comp> IN CAST cl_abap_structdescr( rtti )->components
WHERE ( type_kind CA '8abeFIPs' ) " all numeric types
( <comp>-name ) ) ) ).
DATA(same_names) = FILTER ty_names( names_in_structs[ 1 ] IN names_in_structs[ 2 ] WHERE table_line = table_line ).
LOOP AT same_names REFERENCE INTO DATA(same_name).
ASSIGN COMPONENT same_name->* OF STRUCTURE from_struct TO FIELD-SYMBOL(<from_number>).
ASSERT sy-subrc = 0.
ASSIGN COMPONENT same_name->* OF STRUCTURE to_struct TO FIELD-SYMBOL(<to_number>).
ASSERT sy-subrc = 0.
<to_number> = <to_number> + <from_number>.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
CLASS ltc_app DEFINITION
FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS test FOR TESTING RAISING cx_static_check.
METHODS overflow FOR TESTING RAISING cx_static_check.
ENDCLASS.
CLASS ltc_app IMPLEMENTATION.
METHOD test.
TYPES: ty_output LIKE ls_output.
ls_output = VALUE #( clabs = 100 ceinm = 500 ).
DATA(ls_output2) = ls_output.
lcl_app=>add_corresponding( EXPORTING from_struct = ls_output2 CHANGING to_struct = ls_output ).
cl_abap_unit_assert=>assert_equals( act = ls_output exp = VALUE ty_output( clabs = 200 ceinm = 1000 ) ).
ENDMETHOD.
METHOD overflow.
TYPES: BEGIN OF ty_struct,
int1 TYPE int1,
END OF ty_struct.
DATA(from_struct) = VALUE ty_struct( int1 = 200 ).
DATA(to_struct) = from_struct.
TRY.
lcl_app=>add_corresponding( EXPORTING from_struct = from_struct CHANGING to_struct = to_struct ).
CATCH cx_sy_conversion_overflow INTO DATA(arithmetic_overflow).
ENDTRY.
cl_abap_unit_assert=>assert_bound( act = arithmetic_overflow msg = |Actual: { to_struct-int1 } ; expected: arithmetic overflow| ).
ENDMETHOD.
ENDCLASS.
NB: instead of ADD-CORRESPONDING, you may simply use ls_output-clabs = ls_output-clabs + ls_mchb-clabs and repeat for all numeric components.
NB: ADD-CORRESPONDING and other arithmetic "corresponding" statements were made obsolete because they are considered error-prone:
"These statements are error-prone because, particularly in complex structures, it is not easy to check that identically named components have the data type and content necessary for a numeric operation."
I have the following scenario:
I am using BAPI BAPI_BUPR_RELATIONSHIP_CHANGE to change validuntildate. (BUT050-DATE_TO / BUT051-DATE_TO).
But I also need to update field BUT051-PAFKT and a custom field in BUT050 (lets call it ZZFIELD).
I do this by updating BUT050 / BUT051 from internal tables.
At the end, if I write COMMIT WORK or call FM BAPI_TRANSACTION_COMMIT, only the fields from BAPI_BUPR_RELATIONSHIP_CHANGE will be updated. If I do not write anything at the end, only the field from UPDATE FROM TABLE will be updated.
How can I update all my fields?
Are there any BAPI that can allow me to modify BUT051-PAFKT and custom fields from BUT050 ?
Sample code for testing:
DATA: lt_return TYPE bapiret2_t,
lv_kunnr TYPE kunnr VALUE '111',
lv_partner TYPE bu_partner VALUE '222',
lv_rel_cat TYPE bu_reltyp VALUE 'BUR001',
lv_new_date TYPE sy-datum VALUE '20300101',
lt_but051 TYPE TABLE OF but051.
CALL FUNCTION 'BAPI_BUPR_RELATIONSHIP_CHANGE'
EXPORTING
businesspartner1 = lv_kunnr
businesspartner2 = lv_partner
relationshipcategory = lv_rel_cat
validfromdate = sy-datum
validuntildate = sy-datum
validuntildatenew = lv_new_date
datetox = abap_true
TABLES
RETURN = lt_return.
SELECT *
FROM but051
INTO TABLE lt_but051
WHERE partner1 = lv_kunnr
AND partner2 = lv_partner
AND reltyp = lv_rel_cat.
LOOP AT lt_but051 ASSIGNING FIELD-SYMBOL(<ls_but051>).
<ls_but051>-pafkt = '0003'.
ENDLOOP.
UPDATE but051 FROM TABLE lt_but051.
COMMIT WORK.
Try this (or its wrapper BAPI_BUPR_CONTP_CHANGE):
DATA: ls_person TYPE bapibus1006_central_person.
DATA: ls_person_x TYPE bapibus1006_central_person_x.
DATA: ls_central TYPE bapibus1006_central.
DATA: ls_central_x TYPE bapibus1006_central_x.
DATA: lt_return TYPE bapiret2_t.
is_data-function = '0001'. "<-- your PAFKT
is_data_x-function = abap_true.
* changes of the central data
CALL FUNCTION 'BUPR_CONTP_CHANGE'
EXPORTING
iv_partner = is_data-partner1
* IV_PARTNER_GUID =
iv_contactperson = is_data-partner2
* IV_CONTACTPERSON_GUID =
* IV_DATE_FROM =
* IV_DATE_TO =
* IV_DEFAULTRELATIONSHIP =
* IV_DEFAULTRELATIONSHIP_X =
is_data = is_data-data
is_data_x = is_data_x-central
* IV_TESTRUN = ' '
TABLES
et_return = lt_return.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = abap_true.
Also check my answer about updating relations and the corresponding note.
Regarding the custom field in BUT050, you can't just add a new field and update it, you need to change the BOL model.
I have a problem with adding rows of table to the transport request in programming way.
When i wrote down the transport request number i get the error:
You cannot use request EAMK913244
the code I use is
data lt_variable_changed type table of ztable_task2.
data: l_request type trkorr,
lt_e071 type tr_objects,
lt_e071k type tr_keys,
lv_tabkey type trobj_name,
ls_e071 type e071,
ls_e071k type e071k.
ls_e071-pgmid = 'R3TR'.
ls_e071-object = 'TABU'. "for table
ls_e071-obj_name = 'ZTABLE_TASK2'.
ls_e071-objfunc = 'K'.
append ls_e071 to lt_e071.
loop at lt_variable_changed into ls_variable.
lv_tabkey = ls_variable-num.
ls_e071k-pgmid = 'R3TR'.
ls_e071k-object = 'TABU'.
ls_e071k-objname = 'ZTABLE_TASK2'.
ls_e071k-mastertype = 'TABU'.
ls_e071k-mastername = 'ZTABLE_TASK2'.
ls_e071k-tabkey = lv_tabkey.
append ls_e071k to lt_e071k.
endloop.
call function 'TR_REQUEST_CHOICE'
exporting
iv_suppress_dialog = 'X'
iv_request = var_query
it_e071 = lt_e071
it_e071k = lt_e071k.
message 'Ok' type 'I'.
Screen from se01:
Thanks for help and good luck!
three function modules shall be used to transport changes
call function 'TR_ORDER_CHOICE_CORRECTION'
exporting
iv_category = 'CUST'
importing
ev_order = ev_request
ev_task = ev_task
exceptions
invalid_category = 1
no_correction_selected = 2
others = 3.
call function 'TR_OBJECTS_CHECK'
exporting
iv_no_show_option = abap_true
tables
wt_ko200 = lt_ko200_customizing
wt_e071k = lt_e071k_customizing
exceptions
cancel_edit_other_error = 1
show_only_other_error = 2
others = 3.
call function 'TR_OBJECTS_INSERT'
exporting
wi_order = lv_request
iv_no_show_option = abap_true
tables
wt_ko200 = lt_ko200_customizing
wt_e071k = lt_e071k_customizing
exceptions
cancel_edit_other_error = 1
show_only_other_error = 2
others = 3.
You can also use object-oriented approach for transporting tables.
This piece creates a customizing request and puts contents of a table in it:
DATA(instance) = cl_adt_cts_management=>create_instance( ).
TRY.
instance->insert_objects_in_wb_request( EXPORTING pgmid = 'R3TR'
object = 'TABU'
obj_name = CONV trobj_name( 'Z_TABLE' )
CHANGING trkorr = l_trkorr ).
CATCH cx_adt_cts_insert_error.
RETURN.
ENDTRY.
This piece uses ADT CTS classes and is very flexible. Despite the name wb_request this method is adaptive and will create workbench or customizing request depending on the objects passed.
I have managed to make the READ_TEXT FM work only for one cID at a time on multiple calls of function read_text(for example I found out how to access it for cID = 'GRUN' cObject = 'MATERIAL'. Can anyone advise how to connect read_text function so that inspection text(cID = 'GRUN' cObject = 'MATERIAL') will be dispalyed in my alv grid on the same line with material details?
FORM READTEXT.
data: it_MVKE type standard table of MVKE initial size 0.
data: lMVKE like MVKE, lMAKT like MAKT, lT002 like T002 ,
lTDNAME like THEAD-TDNAME,"text header
it_TLINE type standard table of TLINE,
wa_TLINE type TLINE.
data: cObject(10) type c, cID(4) type c.
select MATNR from MARA into corresponding fields of table it_MVKE
where MATNR in Material order by MATNR.
cID = 'GRUN'. cObject = 'MATERIAL'. "Text date principale "
loop at it_MVKE into lMVKE.
lTDNAME = lMVKE-MATNR.
select spras from T002 into lT002.
CALL FUNCTION 'READ_TEXT'
EXPORTING
CLIENT = SY-MANDT
ID = cID
LANGUAGE = lT002-SPRAS
NAME = lTDNAME
OBJECT = cObject
TABLES
LINES = it_TLINE
EXCEPTIONS
ID = 1
OTHERS = 8.
IF SY-SUBRC EQ 0.
select single * from MAKT into lMAKT where MATNR eq lMVKE-MATNR
and SPRAS eq lT002-SPRAS.
LOOP AT it_TLINE INTO wa_TLINE.
wa_join-TEXTPRI = wa_TLINE-TDLINE.
append wa_join to lt_join.
clear wa_join.
ENDLOOP.
ENDIF.
ENDSELECT.
ENDLOOP.
ENDFORM.
You cannot do like this. Function modules in SAP accept only single parameter at one time, unless this parameter is specified as a table type or in TABLES section.
However, here is workaround from my previous answer you can use to get rid of READ_TEXT at all.
As forgetaboutme said, put you cIDs into itab together with TDNAMEs:
wa_cids-cid = 'GRUN'.
wa_cids-cobject = 'MATERIAL'.
if cID = '0001'.
concatenate lMVKE-MATNR lMVKE-VKORG lMVKE-VTWEG into wa_cids-lTDNAME.
else.
lTDNAME = lMVKE-MATNR.
endif.
append wa_cids to it_cids.
Select texts from db table considering your itab.
SELECT l~tdname l~clustr l~clustd
INTO CORRESPONDING FIELDS OF TABLE t_stxl
FROM stxl AS l
JOIN stxh AS h
ON h~tdobject = l~tdobject
AND h~tdname = l~tdname
AND h~tdid = l~tdid
FOR ALL ENTRIES it_cids
WHERE l~relid = 'TX' "standard text
AND h~tdobject = it_cids-cobject
AND h~tdname = it_cids-lTDNAME
AND h~tdid = it_cids-cid
AND l~tdspras = sy-langu.
Convert them from raw form into readable form
CLEAR: t_stxl_raw[], t_tline[].
APPEND VALUE ty_stxl_raw( clustr = <stxl>-clustr clustd = <stxl>-clustd ) TO t_stxl_raw.
IMPORT tline = t_tline FROM INTERNAL TABLE t_stxl_raw.
Read them
LOOP AT t_tline ASSIGNING <tline>.
wa_Report-TEXT = <tline>-TDLINE.
append wa_Report to it_Report.
ENDLOOP.
You can create an internal table of cIDs & cObjects like this:
types: begin of cids,
cid(4) type c,
cobject(10) type c,
end of cids.
data: wa_cids type cids.
data: it_cids type standard table of cids.
Then you can simply append all the different types of cIDs/cObjects you have to your internal table:
wa_cids-cid = 'GRUN'.
wa_cids-cobject = 'MATERIAL'.
append wa_cids to it_cids.
Then loop over your internal table calling function 'READ_TEXT'
loop at it_cids into wa_cids.
call function 'READ_TEXT'
exporting
client = sy-mandt
id = wa_cids-cid
language = lt002-spras "p_SPRAS
name = ltdname
object = wa_cids-cobject
tables
lines = it_tline
exceptions
id = 1
others = 8.
* Do what you need to do with it_tline here.
endloop.
* Rest of code here
"first part" &&&& fun _ ->
let ident
"second part" &&&& fun _ ->
ident ....
I need to use variable "ident".
I just need to pass value of variable from first part of test to second one...
I want to ask you if there is any easy way how to define and use global variable or even if you have better (and easy) idea of doing that
Keep in mind, please, that I am a beginner, so I would prefer easier ones.
Global variables will often make your code difficult to work with - particularly if they are mutable.
Instead, consider returning the values you need to keep track of as composite values. An easy data type to start with would be a tuple:
let ``first part`` id =
let someOtherValue = "Foo"
someOtherValue, id + 1
This function takes an int (the current ID) as input, and returns string * int (a tuple where the first element is a string, and the second element and int) as output.
You can call it like this:
> let other, newId = ``first part`` 42;;
val other : string = "Foo"
val newId : int = 43
Notice that you can use pattern matching to immediately destructure the values into two named symbols: other and newId.
Your second function could also take an ID as input:
let ``second part`` id otherArgument =
// use id here, if you need it
"Bar"
You can call it like this, with the newId value from above:
> let result = ``second part`` newId "Baz";;
val result : string = "Bar"
If you find yourself doing this a lot, you can define a record for the purpose:
type Identifiable<'a> = { Id : int; Value : 'a }
Now you can begin to define higher-order functions to deal with such a type, such as e.g. a map function:
module Identifiable =
let map f x = { Id = x.Id; Value = f x.Value }
// Other functions go here...
This is a function that maps the Value of an Identifiable from one value to another, but preserves the identity.
Here's a simple example of using it:
> let original = { Id = 42; Value = "1337" };;
val original : Identifiable<string> = {Id = 42;
Value = "1337";}
> let result' = original |> Identifiable.map System.Int32.Parse;;
val result' : Identifiable<int> = {Id = 42;
Value = 1337;}
As you can see, it preserves the value 42, but changes the Value from a string to an int.
You can still change the ID explicitly, if you want to do that:
> let result'' = { result' with Id = 7 };;
val result'' : Identifiable<int> = {Id = 7;
Value = 1337;}
Since this was getting out of hand for comments this is how I would do it for an example
let mutable t = 0
let first =
t <- 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2
In some cases you have to use a ref:
let t = ref 0
let first =
t := 1 + 1
//other stuff
let second =
//can use t here and it will have a value of 2 -
// you use "!t" to get the value
If you define ident at the top of your file like this :
let ident = "foo"
// rest of your code using ident
ident are global and you can use in the next part of your file.
EDIT :
If ident wil change in the next part of your code, use this :
let ident = ref "foo"