Re: macros (was: Seeking computer-programming job (Sunnyvale, CA))
["Followup-To:" header set to comp.lang.lisp.]
On 2009-05-15, Lew <lew@lewscanon.com> wrote:
Stefan Ram wrote:
? The same coded in Java without macros /is/ more verbose,
? but also more readable to someone who knows Java:
public static String getMultiValue( Object valueObject )
{ String result = null;
? if( valueObject == null )result = null;
? else if( valueObject instanceof StringValue )
? { StringValue stringValue =( StringValue )valueObject;
? ? result = getStringValue( stringValue ); }
? else if( valueObject instanceof SprayValue )
? { SprayValue setValue =( SprayValue )valueObject;
? ? result = getSetValue( setValue ); }
? return result; }
An experienced Java programmer will see this series of 'instanceof'
operators and look for a way to code the thing polymorphically.
interface Multi<T>
{
public String getValue( T arg );
}
This is cheating; you are redefining the object's type to suit the code.
Now valueObject must be a Multi<something> .
Can you make this gadget Just Work on the type Object?
Common Lisp:
(defmethod get-value ((obj string-value))
(get-string-value obj))
(defmethod get-value ((obj spray-value))
(get-spray-value obj))
Done; now we can juse use get-value to pull out the value of ither a
string-value or a spray-value.
class StringVersion implements Multi<String>
{
public String getValue( String arg )
{
return getStringValue( arg );
}
}
Even worse, you are wrapping objects now, yuck.
God for job security in tough economic times, though.