THRIFT-1020. ocaml: OCaml compiler generates invalid OCaml
authorBryan Duxbury <bryanduxbury@apache.org>
Mon, 13 Dec 2010 19:17:22 +0000 (19:17 +0000)
committerBryan Duxbury <bryanduxbury@apache.org>
Mon, 13 Dec 2010 19:17:22 +0000 (19:17 +0000)
This patch resolves problems with double constants and adds a copy method to generated structs.

Patch: Richard Low

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1045320 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_ocaml_generator.cc

index 18a8072..b163b53 100644 (file)
@@ -366,6 +366,8 @@ void t_ocaml_generator::generate_const(t_const* tconst) {
 string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value) {
   type = get_true_type(type);
   std::ostringstream out;
+  // OCaml requires all floating point numbers contain a decimal point
+  out.setf(ios::showpoint);
   if (type->is_base_type()) {
     t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
     switch (tbase) {
@@ -385,9 +387,7 @@ string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value)
       break;
     case t_base_type::TYPE_DOUBLE:
       if (value->get_type() == t_const_value::CV_INTEGER) {
-        out << value->get_integer();
-      } else if(value->get_double() == 0.0) {
-        out << "0.0";   // OCaml can't bear '0' in place of double.
+        out << value->get_integer() << ".0";
       } else {
         out << value->get_double();
       }
@@ -733,7 +733,7 @@ void t_ocaml_generator::generate_ocaml_struct_sig(ofstream& out,
   vector<t_field*>::const_iterator m_iter;
   string tname = type_name(tstruct);
   indent(out) << "class " << tname << " :" << endl;
-  indent(out) << "object" << endl;
+  indent(out) << "object ('a)" << endl;
 
   indent_up();
 
@@ -750,7 +750,7 @@ void t_ocaml_generator::generate_ocaml_struct_sig(ofstream& out,
       indent(out) << "method reset_" << mname << " : unit" << endl;
     }
   }
-  indent(out) << "method copy : " << tname << endl;
+  indent(out) << "method copy : 'a" << endl;
   indent(out) << "method write : Protocol.t -> unit" << endl;
   indent_down();
   indent(out) << "end" << endl;