iOS Objective-C 的 Coffee Script -- eero. 让冗长的 objective-c 减肥一半并且更加易于阅读。

hlxwell · December 05, 2013 · Last by hlxwell replied at January 08, 2014 · 8645 hits

主页地址 http://eerolanguage.org/

而且支持 Code Completion.

喜欢的请赞一个。

Objective-C

#import <Foundation/Foundation.h>

@interface FileHelper : NSObject

@property (readonly) NSString* name;
@property (readonly) NSString* format;

-(NSFileHandle*) openFile: (NSString*) path;

-(NSFileHandle*) openFile: (NSString*) path
          withPermissions: (NSString*) permissions;

@end


@implementation FileHelper

-(NSString*) name {
  return @"Macintosh";
}

-(NSString*) format {
  return @"HFS+";
}

-(NSFileHandle*) openFile: (NSString*) path {
  return [NSFileHandle fileHandleForReadingAtPath: path];
}

-(NSFileHandle*) openFile: (NSString*) path
          withPermissions: (NSString*) permissions {

  NSFileHandle* handle = nil;

  if ([permissions isEqualTo: @"readonly"] || [permissions isEqualTo: @"r"]) {
    handle = [NSFileHandle fileHandleForReadingAtPath: path];

  } else if ([permissions isEqualTo: @"readwrite"] || [permissions isEqualTo: @"rw"]) {
    handle = [NSFileHandle fileHandleForUpdatingAtPath: path];
  }

  return handle;
}

@end

eero language:

#import <Foundation/Foundation.h>

interface FileHelper
  String name   {readonly}
  String format {readonly}

  openFile: String, [withPermissions: String], return FileHandle
end

implementation FileHelper
  name,   return String = 'Macintosh'
  format, return String = 'HFS+'

  openFile: String path, withPermissions: String = 'readonly', return FileHandle = nil
    if permissions == 'readonly' or permissions == 'r'
      return FileHandle.fileHandleForReadingAtPath: path
    else if permissions == 'readwrite' or permissions == 'rw'
      return FileHandle.fileHandleForUpdatingAtPath: path
end

貌似不错嘛

Readability matters!

Unknow user #3 December 05, 2013

看示例代码,没看出新语法的 pattern……

我对 coffeescript 不是很熟,不过似乎 eero 用的不是 coffee-like 的语法吧。 假如它能把语法改成尽量接近比较常见的如 ruby, coffee 之类的语法的话,可读性应该会比现在好很多。

看起来非常不错,语法表达能力提升了。

我想问的是:网站上没有解释这个名字怎么来的,eero 是什么意思?另外如果用这个东西,是否还能享受到 Xcode 带来的语法静态分析,智能提示等一系列高级 IDE 的功能?

初看这个标题,我还以为这个东西像是 CoffeeScript 增加了一个参数可以支持生成 Objective-C 代码呢。

这里有些 Limitations

  • Full indexing doesn't currently work, so syntax coloring is limited to lexical (keyword-based) parsing. Semantic recognition of non-built-in types, functions, etc., is not supported yet.
  • "Live issues" is not currently supported.
  • "Jump to definition" is not currently supported.
  • Autocompletion within header files is not currently supported.

#4 楼 @lgn21st

这里有你要的答案: Eero can be integrated with Xcode 5 on Mavericks.

The Eero Support for

  • Xcode plugin supports building
  • source-level debugging
  • static analysis
  • syntax coloring
  • autocompletion (even on prefix namespaces!).
  • All target types are supported, including OS X and iOS (simulator and device).

#4 楼 @lgn21st

不错,有 end

坐等 CoffeeJava, CoffeePHP

能运行在 JVM 上的语言那么多,已经不需要有 CoffeeJava 了

Sounds good

#8 楼 @MrPasserby 说实话非常期望有 coffeePHP,PHP 比很多语言更需要美化

意义不大,objc 已经相当易读了,坑也不多。我从来不认为把花括号和空行去掉就能提升可读性,这只不过是提升了代码的密度而已。

意义不大,如果只有自己用还行,跟别人合作时,带来更多麻烦,代码有问题,问别人时也不行

以 CoffeeScript 举例子,谁应该用 CoffeeScript?答案肯定不是新手,对于不熟悉 JavaScript 的新手而言,CoffeeScript 无异于一门新语言,当遇到问题需要到生成的目标 JavaScript 中寻找根源的时候,你会发现学习并深入理解 JavaScript 是躲不掉的。

同理,在熟悉并深入理解 Objective-C 的基础上,在考虑使用 Eero

#14 楼 @lgn21st 说的对。

#12 楼 @PrideChung #13 楼 @Peter 这个道理跟用 ruby 开发 iOS Mac 感觉很想,你也必须得了解 cocoa 和 objective-c 原来的写法。因为很多文档也是写成那样的。

#15 楼 @hlxwell

我的论点不是必须要了解 objc 才能学 eero,而是 eero 根本没意义。objc 的冗长不是来源于它的语法,它的语法没什么特别啰嗦的,真正冗长的是 Cocoa 和 UIKit 的命名风格,例如这个可能是 Cocoa 里最长的常量 NSPersistentStoreDidImportUbiquitousContentChangesNotification,62 个字符!对于那些“80 字符强迫症”患者根本不留活路。就算给 objc 换一套语法,这些常量和宏该怎么写还是怎么写,除非 eero 的作者有这个魄力给它们全部换一个名字。

#16 楼 @PrideChung

除非 eero 的作者有这个魄力给它们全部换一个名字。 作者去掉了所有的“NS”哈哈,虽然也没啥,但是说明作者也有这个意识。

我承认 objc 语法看习惯了,也不觉得麻烦了觉得也挺顺手的,加上 ide 的那些不全名字长也无所谓。

eero 的语法对于 ruby 开发者来说比较亲和,因为作者就是从 ruby python 这里参考来的。仅此而已。 有了 llvm 我相信未来开发 objective-c 可以像.net 一样,出现 c#.net vb.net 这样的东西。现在 eero 已经可以看到可以结合 Xcode 所有功能而利用一个新的语言。

在 Github 上搜到过另一个,基本没维护的感觉:https://github.com/holtwick/moccacino 有没有人真用过 eero... 效率如何?

#8 楼 @MrPasserby 有的,Java 版本有个 Loop, PHP 有个 SnowScript.. 似乎没人用.. Lua 是 MoonScript 倒真是实用的,, 有个热门项目 Lapis

https://github.com/runekaagaard/snowscript http://www.infoq.com/news/2012/07/loop-jvm-language https://github.com/dhanji/loop http://moonscript.org/ https://github.com/leafo/lapis

其实我觉得 objc 挺美的

这种混乱邪恶的行为。

Objective-C 不优美吗?活活改称 DSL 了,长怎么了。。。

反正 XCode 有代码补全,objective c 用起来很舒服的。 现代文、白话文不是比文言文多了很多冗余吗?但是说起来更方便了,表达起来更细腻了。 文言文为什么那么精炼?因为古时候写字成本很高,要用刀刻在竹简上,或者用毛笔写在丝绸上,又贵又花时间;所以文言文就像发电报一样,能少一些字就少一些字。 ruby 为什么那么精炼?因为 vi 缺省没有代码补全。

#22 楼 @kubisoft 你说的挺有道理的。 但是我相信大家最担心的,还是这种东西的兼容性什么的。

You need to Sign in before reply, if you don't have an account, please Sign up first.