diff --git a/src/expression.jsx b/src/expression.jsx index e334d09a..1ed12212 100644 --- a/src/expression.jsx +++ b/src/expression.jsx @@ -994,6 +994,9 @@ class AsExpression extends UnaryExpression { // ok success = true; } + } else if (this._type.equals(Type.stringType)) { + // ok: any-but-null => string + success = true; } else if (exprType instanceof PrimitiveType) { if (this._type instanceof PrimitiveType) { // ok: primitive => primitive diff --git a/src/jsemitter.jsx b/src/jsemitter.jsx index 8099c935..f5af8ea7 100644 --- a/src/jsemitter.jsx +++ b/src/jsemitter.jsx @@ -1648,6 +1648,11 @@ class _AsExpressionEmitter extends _ExpressionEmitter { new _AsNoConvertExpressionEmitter(this._emitter, new AsNoConvertExpression(this._expr.getToken(), this._expr.getExpr(), this._expr.getType())).emit(outerOpPrecedence); return; } + if (destType.equals(Type.stringType)) { + var prec = _AdditiveExpressionEmitter._operatorPrecedence; + this._emitWithParens(outerOpPrecedence, prec, prec, null, " + \"\""); + return; + } } if (srcType.resolveIfNullable().equals(Type.booleanType)) { // from boolean diff --git a/t/run/346.object-as-string.jsx b/t/run/346.object-as-string.jsx new file mode 100644 index 00000000..c3804d3f --- /dev/null +++ b/t/run/346.object-as-string.jsx @@ -0,0 +1,18 @@ +/*EXPECTED +foo +null +*/ + +class _Main { + override function toString() : string { + return "foo"; + } + + static function main(args : string[]) : void { + var object = new _Main; + log object as string; + object = null; + log object as string; + } +} +// vim: set expandtab tabstop=2 shiftwidth=2 ft=jsx: diff --git a/t/run/347.generic-as-string.jsx b/t/run/347.generic-as-string.jsx new file mode 100644 index 00000000..d882777f --- /dev/null +++ b/t/run/347.generic-as-string.jsx @@ -0,0 +1,29 @@ +/*EXPECTED +C: foo +C: _Main +C: null +*/ + +class C. { + var value : T; + function constructor(value : T) { + this.value = value; + } + + function debugLog() : void { + log "C: " + this.value as string; + } +} + +class _Main { + override function toString() : string { + return "_Main"; + } + + static function main(args : string[]) : void { + new C.("foo").debugLog(); + new C.<_Main>(new _Main).debugLog(); + new C.<_Main>(null).debugLog(); + } +} +// vim: set expandtab tabstop=2 shiftwidth=2 ft=jsx: