It seems I have now reached that point with Objective-Smalltalk. Now it was pretty much always the case that I preferred the Objective-Smalltalk code to equivalent Objective-C code, but since it was mostly scripts and other "final" code, the comparison never really came up. With a somewhat workable if incomplete class and scheme (and filter) definition syntax in place, and therefore Objective-Smalltalk now at least theoretically capable of delivering reusable code, that is no longer the case.
Specifically, I have a little Smalltalk-inspired method browser that I use for ObjST-based live coding environments (AppLive for Apps and SiteBuilder for websites).
Yes: "Programmer UI". I'll clean that up later. What I am currently cleaning up is the interface between the browser and the "method store", which is horrendous. It is also tied to a specific, property-list based implementation of the method store.
The idea there is to allow external editing of the classes/methods. A browser for hierarchically nested structures...could this be a job for scheme handlers? Why yes, glad you asked!
#!/usr/local/bin/stsh
#-methodbrowser:classdef
scheme ClassBrowser {
var dictionary.
-initWithDictionary:aDict {
self setDictionary:aDict.
self.
}
-classDefs {
self dictionary at:'methodDict'.
}
/. {
|= {
self classDefs allKeys.
}
}
/:className/:which/:methodName {
|= {
self classDefs at:className | at:which | at:methodName.
}
=| {
self classDefs at:className | at:which | at:methodName put:newValue.
}
}
/:className/:which {
|= {
self classDefs at:className | at:which | allKeys.
}
}
}
scheme:browser := ClassBrowser alloc initWithDictionary: classdef value propertyList.
stdout do println: browser:. each.
shell runInteractiveLoop.
Despite the fact that there are still some issues to resolve, for example this code makes it clear that something needs to be done about navigation vs. final access, it still strikes me as a clear and succinct expression of what is going on. Now I sort of need to rewrite it in Objective-C, because both AppLive and SiteBuilder are Objective-C projects.
And I really don't want to.
I really, really don't want to. The idea of recasting this logic just strikes me as abhorrent, so much that the somewhat daunting prospect of significantly improving my Objective-Smalltalk native compilation facilities looks much more attractive.
That's the tipping point.
For reference, here's the original Objective-C code. What, you didn't believe me that it's horrendous? It also does a few
things that the Objective-Smalltalk code doesn't do yet, but those aren't that significant.
//
// MethodDict.h
// MPWTalk
//
// Created by Marcel Weiher on 10/16/11.
// Copyright (c) 2012 Marcel Weiher. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol MethodDict
-(NSArray*)instanceMethodsForClass:(NSString*)className;
-(NSArray*)classMethodsForClass:(NSString*)className;
-(NSString*)fullNameForMethodName:(NSString*)shortName ofClass:(NSString*)className;
-(NSString*)methodForClass:(NSString*)className methodName:(NSString*)methodName;
-(void)setClassMethod:(NSString*)methodBody name:(NSString*)methodName forClass:(NSString*)className;
-(void)setInstanceMethod:(NSString*)methodBody name:(NSString*)methodName forClass:(NSString*)className;
-(void)deleteInstanceMethodName:(NSString*)methodName forClass:(NSString*)className;
-(void)deleteClassMethodName:(NSString*)methodName forClass:(NSString*)className;
-(NSMutableDictionary*)addClassWithName:(NSString*)newClassName;
-(void)deleteClass:(NSString*)className;
-(NSString*)instanceMethodForClass:(NSString*)className methodName:(NSString*)methodName;
-(NSString*)classMethodForClass:(NSString*)className methodName:(NSString*)methodName;
@end
@interface MethodDict : NSObject
{
NSMutableDictionary *dict;
}
- (NSDictionary *)dict;
-initWithDict:(NSDictionary*)newDict;
-(NSArray*)classes;
@end
//
// MethodDict.m
// MPWTalk
//
// Created by Marcel Weiher on 10/16/11.
// Copyright (c) 2012 Marcel Weiher. All rights reserved.
//
#import "MethodDict.h"
#import <MPWFoundation/MPWFoundation.h>
#import <ObjectiveSmalltalk/MethodHeader.h>
@implementation NSString(methodName)
-methodName
{
MPWMethodHeader *header=[MPWMethodHeader methodHeaderWithString:self];
return [header methodName];
}
@end
@implementation MethodDict
objectAccessor(NSMutableDictionary, dict, setDict)
-initWithDict:(NSDictionary*)newDict
{
self = [super init];
[self setDict:[[newDict mutableCopy] autorelease]];
return self;
}
-(NSData*)asXml
{
NSData *data=[NSPropertyListSerialization dataFromPropertyList:[self dict] format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
return data;
}
-(NSArray*)classes
{
return [[[self dict] allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
-(NSMutableDictionary*)classDictForName:(NSString*)className
{
return [[self dict] objectForKey:className];
}
-(void)deleteClass:(NSString*)className
{
return [[self dict] removeObjectForKey:className];
}
-(NSMutableDictionary*)methodDictForClass:(NSString*)className classMethods:(BOOL)isClassMethod
{
NSString *key=isClassMethod ? @"classMethods" : @"instanceMethods";
return [[[self dict] objectForKey:className] objectForKey:key];
}
-(NSArray*)methdodsForClass:(NSString*)className getClassMethods:(BOOL)classMethods
{
NSDictionary *methodDict=[self methodDictForClass:className classMethods:classMethods];
NSArray* methodKeys = [methodDict allKeys];
if ( [methodKeys count]) {
return [(NSArray*)[[methodKeys collect] methodName] sortedArrayUsingSelector:@selector(compare:)];
}
return [NSArray array];
}
-(NSArray*)instanceMethodsForClass:(NSString*)className
{
return [self methdodsForClass:className getClassMethods:NO];
}
-(NSArray*)classMethodsForClass:(NSString*)className
{
return [self methdodsForClass:className getClassMethods:YES];
}
-(NSString*)fullNameForMethodName:(NSString*)shortName ofClass:(NSString*)className
{
NSArray *fullNames = [[self methodDictForClass:className classMethods:NO] allKeys];
fullNames=[fullNames arrayByAddingObjectsFromArray:[[self methodDictForClass:className classMethods:YES] allKeys]];
for ( NSString *fullName in fullNames ) {
if ( [[fullName methodName] isEqual:shortName] ) {
return fullName;
}
}
return nil;
}
-(NSString*)instanceMethodForClass:(NSString*)className methodName:(NSString*)methodName
{
return [[self methodDictForClass:className classMethods:NO] objectForKey:[self fullNameForMethodName:methodName ofClass:className]];
}
-(NSString*)classMethodForClass:(NSString*)className methodName:(NSString*)methodName
{
return [[self methodDictForClass:className classMethods:YES] objectForKey:[self fullNameForMethodName:methodName ofClass:className]];
}
-(NSString*)methodForClass:(NSString*)className methodName:(NSString*)methodName
{
return [self instanceMethodForClass:className methodName:methodName];
}
-(void)setMethod:(NSString*)methodBody name:(NSString*)methodName forClass:(NSString*)className isClassMethod:(BOOL)isClassMethod
{
NSMutableDictionary *methodDict = [self methodDictForClass:className classMethods:isClassMethod];
if ( !methodDict ) {
[self addClassWithName:className];
methodDict = [self methodDictForClass:className classMethods:isClassMethod];
}
[methodDict setObject:methodBody forKey:methodName];
}
-(void)setClassMethod:(NSString*)methodBody name:(NSString*)methodName forClass:(NSString*)className
{
[self setMethod:methodBody name:methodName forClass:className isClassMethod:YES];
}
-(void)setInstanceMethod:(NSString*)methodBody name:(NSString*)methodName forClass:(NSString*)className
{
[self setMethod:methodBody name:methodName forClass:className isClassMethod:NO];
}
-(NSMutableDictionary*)addClassWithName:(NSString*)newClassName
{
NSMutableDictionary *classDict=[self classDictForName:newClassName];
if ( !classDict) {
classDict=[NSMutableDictionary dictionary];
classDict[@"instanceMethods"]=[NSMutableDictionary dictionary];
classDict[@"classMethods"]=[NSMutableDictionary dictionary];
dict[newClassName]=classDict;
}
return classDict;
}
-(void)deleteMethodName:(NSString*)methodName forClass:(NSString*)className isClassMethod:(BOOL)isClassMethod
{
NSMutableDictionary *methodDict = [self methodDictForClass:className classMethods:isClassMethod];
[methodDict removeObjectForKey:[self fullNameForMethodName:methodName ofClass:className]];
}
-(void)deleteInstanceMethodName:(NSString*)methodName forClass:(NSString*)className
{
[[self methodDictForClass:className classMethods:NO] removeObjectForKey:[self fullNameForMethodName:methodName ofClass:className]];
}
-(void)deleteClassMethodName:(NSString*)methodName forClass:(NSString*)className
{
[[self methodDictForClass:className classMethods:YES] removeObjectForKey:[self fullNameForMethodName:methodName ofClass:className]];
}
-(NSString*)description
{
return [NSString stringWithFormat:@"<%@:%p: %@>",[self class],self,dict];
}
@end
Can you open source the sample app in Objective Smalltalk so we can take a look? I've only started coding again but I am very compelled by what you write :)
ReplyDelete