Ian Mackenzie
2015-07-18 05:12:44 UTC
I'm looking at creating native Dart bindings to a C++ library that I've
been developing, and one of the things I keep having to consider is how to
deal with wrapping functions that in C++ are overloaded. In some cases I've
been able to refactor the C++ library to replace overloaded functions with
separately-named functions instead (in many cases increasing readability
and simplifying the code!), but there are a few holdouts. For instance, in
C++ I have code like
// C++
class Point3d
{
public:
Point3d projectedOnto(const Axis3d& axis) const;
Point3d projectedOnto(const Plane3d& plane) const;
};
I can think of a few options for making this work in Dart. First, just use
an untyped argument:
// Dart
class Point3d {
Point3d projectedOnto(datum) {...}
}
which is simplest but not type safe and doesn't indicate well to the caller
what is required (maybe call the argument 'axisOrPlane'?). Alternately,
have the Dart versions of Axis3d and Plane3d inherit from Datum3d or
something, then have
// Dart
class Point3d {
Point3d projectedOnto(Datum3d datum) {...}
}
which is more safe but still not clear to the user what is required (they
have to figure out that Datum3d is extended/implemented by Axis3d and
Plane3d). The simplest version is to probably just add suffixes, at least
in Dart (maybe in C++ as well for consistency):
// Dart
class Point3d {
Point3d projectedOntoAxis(Axis3d axis) {...}
Point3d projectedOntoPlane(Plane3d plane) {...}
}
but that's getting pretty verbose and prevents the code from reading like a
nice phrase. One more I thought of would be to use typed keyword arguments
to a single method where it would hopefully be obvious that only one should
be specified:
// Dart
class Point3d {
Point3d projectedOnto({Axis3d axis, Plane3d plane}) {...}
}
although that is just as verbose to use as the suffixed version and a bit
weirder (and I'm guessing less efficient).
Thoughts? Which version do you think is the cleanest/most readable/most
idiomatic?
been developing, and one of the things I keep having to consider is how to
deal with wrapping functions that in C++ are overloaded. In some cases I've
been able to refactor the C++ library to replace overloaded functions with
separately-named functions instead (in many cases increasing readability
and simplifying the code!), but there are a few holdouts. For instance, in
C++ I have code like
// C++
class Point3d
{
public:
Point3d projectedOnto(const Axis3d& axis) const;
Point3d projectedOnto(const Plane3d& plane) const;
};
I can think of a few options for making this work in Dart. First, just use
an untyped argument:
// Dart
class Point3d {
Point3d projectedOnto(datum) {...}
}
which is simplest but not type safe and doesn't indicate well to the caller
what is required (maybe call the argument 'axisOrPlane'?). Alternately,
have the Dart versions of Axis3d and Plane3d inherit from Datum3d or
something, then have
// Dart
class Point3d {
Point3d projectedOnto(Datum3d datum) {...}
}
which is more safe but still not clear to the user what is required (they
have to figure out that Datum3d is extended/implemented by Axis3d and
Plane3d). The simplest version is to probably just add suffixes, at least
in Dart (maybe in C++ as well for consistency):
// Dart
class Point3d {
Point3d projectedOntoAxis(Axis3d axis) {...}
Point3d projectedOntoPlane(Plane3d plane) {...}
}
but that's getting pretty verbose and prevents the code from reading like a
nice phrase. One more I thought of would be to use typed keyword arguments
to a single method where it would hopefully be obvious that only one should
be specified:
// Dart
class Point3d {
Point3d projectedOnto({Axis3d axis, Plane3d plane}) {...}
}
although that is just as verbose to use as the suffixed version and a bit
weirder (and I'm guessing less efficient).
Thoughts? Which version do you think is the cleanest/most readable/most
idiomatic?
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.