0

I am creating a library to emulate C's stdio library. Is working but the functions (2/3 of which are actually Function objects at the moment) look terrible. I am having a really difficult time figuring out Metadata, Symbols and Mirrors. From what I can tell, they are very useful and they might do what I need. So 3 questions:

  • Can InstanceMirrors be used like pointers?
  • Can either of them be used in a similar fashion to macros like those in C (#define MAC / #define F_MAC(a, b))?
  • Is there a clean way to implement var_args* and polymorphic functions**? am hoping for something which the editor will actually recognize as a function.

Please include explanations and examples (I am pretty dense so if you don't include examples, you will likely be wasting your time).

*I already know about this: Creating function with variable number of arguments or parameters in Dart, I am hoping for something the editor will recognize as a function rather than an object.

**I am currently just using dynamic parameters and generating the behavior based on the types passed in at run-time, but that is, while effective, horrifying to look at and document.

Ideally I would like my documentation output to resemble this

// var_args hack
int printf   (String format, ...)  //C

// actually looks good
int vprintf  (String format, List<dynamic> args) //C

// var_args hack
int fprintf  (dynamic stream, String format, ...) //C

// var_args hack
int fnprintf  (dynamic stream, int maxsize, String format, ...) //C

// actually looks good
int vfprintf (dynamic stream, String format, List<dynamic> args) //C

// actually looks good
int vfnprintf (dynamic stream, int maxsize, String format, List<dynamic> args) //C

// --1st first param optional so polymorphic is necessary, + var_args--
int sprintf (StringBuffer stream, String format, ...) //C
String sprintf (String format, ...)

// --1st first param optional so polymorphic is necessary, + var_args--
int snprintf (StringBuffer stream, int maxsize, String format, ...)//C
String snprintf (String format, int maxsize, ...)

// --1st first param optional so polymorphic is necessary--
int vsprintf (StringBuffer stream, String format, List<dynamic> args)//C
String vsprintf (String format, List<dynamic> args)

// --1st first param optional so polymorphic is necessary--
int vsnprintf(StringBuffer stream, int maxsize, String format, List<dynamic> args) //C
String vsnprintf(String format, int maxsize, List<dynamic> args)

// --1st first param optional so polymorphic is necessary--
int strftime(StringBuffer buffer, int maxsize, String format, DateTime date) //C
String strftime(int maxsize, String format, DateTime date)
int strftime(StringBuffer buffer, String format, DateTime date)
String strftime(String format, DateTime date)

// will need something like a pointer or reference
String vsprintf(String buffer, String format, List<dynamic> args) //C

Edit:

sorry, I was called away and didn't have time to my question. In order:

  • what I meant was something like this:
String f(String *arg0, int *arg1, double *arg2, bool *arg3) 

currently I am doing:

String arg0 = ...; 
int arg1 = ...;
double arg2 = ...;
bool arg3 = ...;

List<dynamic> args = [arg0, arg1, arg2, arg3];

f1(args);

arg0 = args[0];
arg1 = args[1];
arg2 = args[2];
arg3 = args[3];



List<dynamic> args0 = [arg0];
List<dynamic> args1 = [arg1];
List<dynamic> args2 = [arg2];
List<dynamic> args3 = [arg3];

f2(args0, args1, args2, args3);

arg0 = args0[0];
arg1 = args1[0];
arg2 = args2[0];
arg3 = args3[0];

I am hoping to find a better way

  • I would like to be able to do the equivalent of this:
    #define private: 
    #define public:
    #define enum class

so that I could:

enum E
{
  static int VALUE_0 = 0;
  static int VALUE_1 = 1;
  static int VALUE_2 = 2;
  static int VALUE_3 = 3;

  int value;
}


class C
{
  private:
    int _private0;
    int _private1;
    int _private2;
    int _private3;


  public:
    int _public0;
    int _public1;
    int _public2;
    int _public3;

}

It essentially for commenting purposes (just curious)

  • yea, I know it works, I was just wondering if it was a better way.

P.S. Any good tutorials on mirrors, or Symbols?

Community
  • 1
  • 1
chrisgotter
  • 383
  • 1
  • 3
  • 13

1 Answers1

0
  • Can InstanceMirrors be used like pointers?
import 'dart:math' as math;
import 'dart:mirrors' as mirr;

void main() {
  math.Rectangle r = new math.Rectangle(10, 10, 20, 20);
  mirr.InstanceMirror im = mirr.reflect(r);
  var r2 = im.reflectee;
  print('r top-left: ${r.topLeft}, r2 top-left: ${r2.topLeft}');
}
  • Can either of them be used in a similar fashion to macros like those in C (#define MAC / #define F_MAC(a, b))?

can you elaborate a bit more what you want to accomplish?

  • Is there a clean way to implement var_args* and polymorphic functions**? am hoping for something which the editor will actually recognize as a function.

I think the question/answer you linked to has all information about this topic

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567