h a l f b a k e r yFunny peculiar.
add, search, annotate, link, view, overview, recent, by name, random
news, help, about, links, report a problem
browse anonymously,
or get an account
and write.
register,
|
|
|
This is one for the C/C++ geeks. It's a suggestion for a language feature to replace C's macros and C++'s templates.
Let's say I want to take the minimum of two numbers, whether int or float.
In C:
#define MIN(x,y) ((x < y) ? x : y)
In C++ :
template <class T>
T min(T x, T y)
{ return
(x < y) ? x : y; }
In 'weak C' you can return a type 'unknown', and you can leave out type specifiers on function parameter declarations.
I.e. :
unknown min(x, y)
{ return (x < y) ? x : y; }
when you do something like :
int i, j;
int k = min(i, j);
The compiler knows the result and input types, so it compiles the appropriate function
int min(int, int);
for you, and reports any errors that result.
This has two advantages :
(a) weak-C is simpler: no distinction between macros, functions and templates.
(b) can easily mix types, unlike C++ templates; but can be debugged properly, unlike C macros.
shoot holes in, please
Boost::Any
http://www.boost.org/doc/html/any.html A type quite like 'unknown' [ViperX, Apr 03 2006]
Please log in.
If you're not logged in,
you can see what this page
looks like, but you will
not be able to add anything.
Destination URL.
E.g., https://www.coffee.com/
Description (displayed with the short name and URL.)
|
|
I have to confess I'm not up on my C#. From the brief reading I just did on delegates, it seems they're effectively 'objects that represent function calls', a better approach than function pointers, which isn't really the same thing as what I outlined. |
|
|
Even if delegates do the same thing, note that we've still had to add a construct to the language, rather than relaxing and simplifying the syntax constraints, as 'weak-C' does. |
|
|
Not like an Excel formula that I'd type shyly because I'm not sure of the syntax? |
|
|
Python has a 'dynamic typing system' which is meant to bring similar advantages. |
|
|
The advantage of 'weak C' is that it actually remains strongly typed (i.e. you catch type errors at compile time rather than runtime), but with (some of) the advantages of weak typing. It also remains compiled, i.e. performance should be a little better than Python (if one cares about such things - in my work, I have to). |
|
|
Tut-tut //#define MIN(x,y) ((x < y) ? x : y)//
sp. "#define MIN(x,y) (((x) < (y)) ? (x) : (y))" |
|
|
I was waiting for someone to point that out. One of the many reasons that the C preprocessor needs getting rid of... |
|
|
Haskell is strongly-typed and has a "type inference" system that can usually figure out this kind of stuff. I'm not much good at haskell, but I think you could express this as: |
|
|
min :: (Ord a) => (a,a) -> a |
|
|
min (x,y) = if x < y then x else y |
|
|
It's much too late for me to read this too closely, so I'll get back to you -- however, it "feels" a lot like the days of VB when you had a "variant" data type. That one is a two edged sword. I think the bad outweighed the good. I'm worried that this might be in the same category. |
|
|
Check Boost. It has boost::any ;) |
|
|
What is being sought here is not unreasonable, though I'm not particularly fond of the implementation. What I'd rather see would be some extensions to make macros less kludgy. |
|
|
To achieve the desired behavior indicated here would require two things: (1) the ability to have a statement function as an rvalue, as it can in gcc but not in any standard C dialect I know of; (2) an ability to create an object within a temporary scope which has the same type as an existing object (reading that existing object precisely once). |
|
|
The problem with the #define approach to 'min' is that if p=="Hello" and q=="Tricky", min(*p++,*q++) will return 'e', with p=="llo" and q=="ricky" (rather than returning 'H' and leaving pointer p=="ello"). |
|
| |