string Slice::Ruby::CodeVisitor::getInitializer(const TypePtr& p) { BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); if(builtin) { switch(builtin->kind()) { case Builtin::KindBool: { return "false"; } case Builtin::KindByte: case Builtin::KindShort: case Builtin::KindInt: case Builtin::KindLong: { return "0"; } case Builtin::KindFloat: case Builtin::KindDouble: { return "0.0"; } case Builtin::KindString: { return "''"; } case Builtin::KindObject: case Builtin::KindObjectProxy: case Builtin::KindLocalObject: { return "nil"; } } } EnumPtr en = EnumPtr::dynamicCast(p); if(en) { EnumeratorList enums = en->getEnumerators(); return getAbsolute(en, IdentToUpper) + "::" + fixIdent(enums.front()->name(), IdentToUpper); } StructPtr st = StructPtr::dynamicCast(p); if(st) { return getAbsolute(st, IdentToUpper) + ".new"; } return "nil"; }
void CodeVisitor::writeDefaultValue(const TypePtr& p) { BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); if(builtin) { switch(builtin->kind()) { case Builtin::KindBool: { _out << "false"; break; } case Builtin::KindByte: case Builtin::KindShort: case Builtin::KindInt: case Builtin::KindLong: { _out << "0"; break; } case Builtin::KindFloat: case Builtin::KindDouble: { _out << "0.0"; break; } case Builtin::KindString: { _out << "''"; break; } case Builtin::KindObject: case Builtin::KindObjectProxy: case Builtin::KindLocalObject: { _out << "null"; break; } } return; } EnumPtr en = EnumPtr::dynamicCast(p); if(en) { EnumeratorList enums = en->getEnumerators(); _out << getAbsolute(en, _ns) << "::" << fixIdent(enums.front()->name()); return; } // // PHP does not allow the following construct: // // function foo($theStruct=new MyStructType) // // Instead we use null as the default value and allocate an instance in // the constructor. // StructPtr st = StructPtr::dynamicCast(p); if(st) { _out << "null"; return; } _out << "null"; }