hacks for some examples added
tests added kotlin lib clean up
This commit is contained in:
Generated
+312
-548
File diff suppressed because it is too large
Load Diff
@@ -339,4 +339,8 @@ public class AstUtil {
|
|||||||
public static JsPrefixOperation not(JsExpression expression) {
|
public static JsPrefixOperation not(JsExpression expression) {
|
||||||
return new JsPrefixOperation(JsUnaryOperator.NOT, expression);
|
return new JsPrefixOperation(JsUnaryOperator.NOT, expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static JsBinaryOperation typeof(JsExpression expression, JsStringLiteral string) {
|
||||||
|
return equals(new JsPrefixOperation(JsUnaryOperator.TYPEOF, expression), string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,19 @@ public final class StandardClasses {
|
|||||||
declareArray(standardClasses, standardLibrary);
|
declareArray(standardClasses, standardLibrary);
|
||||||
declareIterator(standardClasses, standardLibrary);
|
declareIterator(standardClasses, standardLibrary);
|
||||||
declareRange(standardClasses);
|
declareRange(standardClasses);
|
||||||
|
declareString(standardClasses);
|
||||||
declareJavaArrayList(standardClasses);
|
declareJavaArrayList(standardClasses);
|
||||||
declareJavaSystem(standardClasses);
|
declareJavaSystem(standardClasses);
|
||||||
declareJavaInteger(standardClasses);
|
declareJavaInteger(standardClasses);
|
||||||
return standardClasses;
|
return standardClasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void declareString(@NotNull StandardClasses standardClasses) {
|
||||||
|
String stringFQName = "jet.String";
|
||||||
|
standardClasses.declareStandardTopLevelObject(stringFQName, "String");
|
||||||
|
standardClasses.declareStandardInnerDeclaration(stringFQName, "length", "length");
|
||||||
|
}
|
||||||
|
|
||||||
//TODO: duplication
|
//TODO: duplication
|
||||||
private static void declareRange(@NotNull StandardClasses standardClasses) {
|
private static void declareRange(@NotNull StandardClasses standardClasses) {
|
||||||
String intRangeFQName = "jet.IntRange";
|
String intRangeFQName = "jet.IntRange";
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.jetbrains.k2js.translate.expression;
|
|||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsInvocation;
|
import com.google.dart.compiler.backend.js.ast.JsInvocation;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsName;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsNameRef;
|
import com.google.dart.compiler.backend.js.ast.JsNameRef;
|
||||||
import com.google.dart.compiler.util.AstUtil;
|
import com.google.dart.compiler.util.AstUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -59,6 +60,14 @@ public final class PatternTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateTypePattern(@NotNull JsExpression expressionToMatch,
|
private JsExpression translateTypePattern(@NotNull JsExpression expressionToMatch,
|
||||||
@NotNull JetTypePattern pattern) {
|
@NotNull JetTypePattern pattern) {
|
||||||
|
//TODO: HACK to make some examples work
|
||||||
|
JsName className = getClassReference(pattern).getName();
|
||||||
|
if (className.getIdent().equals("String")) {
|
||||||
|
return AstUtil.typeof(expressionToMatch, program().getStringLiteral("string"));
|
||||||
|
}
|
||||||
|
if (className.getIdent().equals("Int")) {
|
||||||
|
return AstUtil.typeof(expressionToMatch, program().getStringLiteral("number"));
|
||||||
|
}
|
||||||
|
|
||||||
JsInvocation isCheck = AstUtil.newInvocation(context().namer().isOperationReference(),
|
JsInvocation isCheck = AstUtil.newInvocation(context().namer().isOperationReference(),
|
||||||
expressionToMatch, getClassReference(pattern));
|
expressionToMatch, getClassReference(pattern));
|
||||||
@@ -79,7 +88,7 @@ public final class PatternTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression getClassReference(@NotNull JetTypePattern pattern) {
|
private JsNameRef getClassReference(@NotNull JetTypePattern pattern) {
|
||||||
JetTypeReference typeReference = getTypeReference(pattern);
|
JetTypeReference typeReference = getTypeReference(pattern);
|
||||||
return getClassNameReferenceForTypeReference(typeReference);
|
return getClassNameReferenceForTypeReference(typeReference);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import org.jetbrains.jet.lang.descriptors.PropertySetterDescriptor;
|
|||||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||||
import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
|
import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
|
||||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
|
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
|
||||||
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
|
|
||||||
@@ -158,6 +159,13 @@ public final class PropertyAccessTranslator extends AccessTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression getterCall() {
|
private JsExpression getterCall() {
|
||||||
|
//TODO: HACK to make standard example work
|
||||||
|
if (DescriptorUtils.getFQName(propertyDescriptor).equals("jet.String.length")) {
|
||||||
|
JsNameRef lengthMethodReference = AstUtil.newQualifiedNameRef("length");
|
||||||
|
AstUtil.setQualifier(lengthMethodReference, translateQualifier());
|
||||||
|
return lengthMethodReference;
|
||||||
|
}
|
||||||
|
|
||||||
JsName getterName = getGetterName();
|
JsName getterName = getGetterName();
|
||||||
return qualifiedAccessorInvocation(getterName);
|
return qualifiedAccessorInvocation(getterName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,64 +17,11 @@ public final class SystemTest extends JavaClassesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void systemPrint() throws Exception {
|
public void systemPrint() throws Exception {
|
||||||
checkOutput("systemPrint.kt", "Hello, world!");
|
checkOutput("print.kt", "Hello, world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void printArg() throws Exception {
|
public void systemPrintln() throws Exception {
|
||||||
checkOutput("printArg.kt", "Hello, world!", "Hello, world!");
|
checkOutput("println.kt", "Hello, world!\n3\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whileLoop() throws Exception {
|
|
||||||
checkOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ifAsExpression() throws Exception {
|
|
||||||
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void objectOrientedHello() throws Exception {
|
|
||||||
checkOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void multiLanguageHello() throws Exception {
|
|
||||||
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void nullChecks() throws Exception {
|
|
||||||
checkOutput("nullChecks.kt", "No number supplied");
|
|
||||||
checkOutput("nullChecks.kt", "6", "2", "3");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void ranges() throws Exception {
|
|
||||||
checkOutput("ranges.kt", "OK\n" +
|
|
||||||
" 1 2 3 4 5\n" +
|
|
||||||
"Out: array has only 3 elements. x = 4\n" +
|
|
||||||
"Yes: array contains aaa\n" +
|
|
||||||
"No: array doesn't contains ddd\n", "4");
|
|
||||||
|
|
||||||
checkOutput("ranges.kt", " 1 2 3 4 5\n" +
|
|
||||||
"Out: array has only 3 elements. x = 10\n" +
|
|
||||||
"Yes: array contains aaa\n" +
|
|
||||||
"No: array doesn't contains ddd\n", "10");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void forLoop() throws Exception {
|
|
||||||
checkOutput("forLoop.kt", "a\n" +
|
|
||||||
"b\n" +
|
|
||||||
"c\n" +
|
|
||||||
"\n" +
|
|
||||||
"a\n" +
|
|
||||||
"b\n" +
|
|
||||||
"c\n", "a", "b", "c");
|
|
||||||
checkOutput("forLoop.kt", "123\n\n123\n", "123");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package org.jetbrains.k2js.test;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Talanov Pavel
|
||||||
|
*/
|
||||||
|
public final class WebDemoExamplesTest extends TranslationTest {
|
||||||
|
|
||||||
|
final private static String MAIN = "webDemoExamples/";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String mainDirectory() {
|
||||||
|
return MAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void printArg() throws Exception {
|
||||||
|
checkOutput("printArg.kt", "Hello, world!", "Hello, world!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whileLoop() throws Exception {
|
||||||
|
checkOutput("whileLoop.kt", "guest1\nguest2\nguest3\nguest4\n", "guest1", "guest2", "guest3", "guest4");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifAsExpression() throws Exception {
|
||||||
|
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectOrientedHello() throws Exception {
|
||||||
|
checkOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multiLanguageHello() throws Exception {
|
||||||
|
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nullChecks() throws Exception {
|
||||||
|
checkOutput("nullChecks.kt", "No number supplied");
|
||||||
|
checkOutput("nullChecks.kt", "6", "2", "3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ranges() throws Exception {
|
||||||
|
checkOutput("ranges.kt", "OK\n" +
|
||||||
|
" 1 2 3 4 5\n" +
|
||||||
|
"Out: array has only 3 elements. x = 4\n" +
|
||||||
|
"Yes: array contains aaa\n" +
|
||||||
|
"No: array doesn't contains ddd\n", "4");
|
||||||
|
|
||||||
|
checkOutput("ranges.kt", " 1 2 3 4 5\n" +
|
||||||
|
"Out: array has only 3 elements. x = 10\n" +
|
||||||
|
"Yes: array contains aaa\n" +
|
||||||
|
"No: array doesn't contains ddd\n", "10");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void forLoop() throws Exception {
|
||||||
|
checkOutput("forLoop.kt", "a\n" +
|
||||||
|
"b\n" +
|
||||||
|
"c\n" +
|
||||||
|
"\n" +
|
||||||
|
"a\n" +
|
||||||
|
"b\n" +
|
||||||
|
"c\n", "a", "b", "c");
|
||||||
|
checkOutput("forLoop.kt", "123\n\n123\n", "123");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isCheck() throws Exception {
|
||||||
|
checkOutput("isCheck.kt", "3\nnull\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void patternMatching() throws Exception {
|
||||||
|
checkOutput("patternMatching.kt", "Greeting\n" +
|
||||||
|
"One\n" +
|
||||||
|
"Not a string\n" +
|
||||||
|
"Unknown\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
System.out?.println("Hello, world!");
|
||||||
|
System.out?.println(3);
|
||||||
|
System.out?.println();
|
||||||
|
}
|
||||||
@@ -28,7 +28,7 @@ var Class = (function () {
|
|||||||
;
|
;
|
||||||
function create() {
|
function create() {
|
||||||
var parent = null, properties = $A(arguments);
|
var parent = null, properties = $A(arguments);
|
||||||
if (Object.isFunction(properties[0]))
|
if (typeof (properties[0]) == "function")
|
||||||
parent = properties.shift();
|
parent = properties.shift();
|
||||||
|
|
||||||
function klass() {
|
function klass() {
|
||||||
@@ -80,7 +80,7 @@ var Class = (function () {
|
|||||||
|
|
||||||
for (var i = 0, length = properties.length; i < length; i++) {
|
for (var i = 0, length = properties.length; i < length; i++) {
|
||||||
var property = properties[i], value = source[property];
|
var property = properties[i], value = source[property];
|
||||||
if (ancestor && Object.isFunction(value) &&
|
if (ancestor && (typeof (value) == "function") &&
|
||||||
value.argumentNames()[0] == "$super") {
|
value.argumentNames()[0] == "$super") {
|
||||||
var method = value;
|
var method = value;
|
||||||
value = (function (m) {
|
value = (function (m) {
|
||||||
@@ -144,38 +144,6 @@ var Namespace = (function () {
|
|||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
|
|
||||||
var _toString = Object.prototype.toString,
|
|
||||||
NULL_TYPE = 'Null',
|
|
||||||
UNDEFINED_TYPE = 'Undefined',
|
|
||||||
BOOLEAN_TYPE = 'Boolean',
|
|
||||||
NUMBER_TYPE = 'Number',
|
|
||||||
STRING_TYPE = 'String',
|
|
||||||
OBJECT_TYPE = 'Object',
|
|
||||||
FUNCTION_CLASS = '[object Function]',
|
|
||||||
BOOLEAN_CLASS = '[object Boolean]',
|
|
||||||
NUMBER_CLASS = '[object Number]',
|
|
||||||
STRING_CLASS = '[object String]',
|
|
||||||
ARRAY_CLASS = '[object Array]',
|
|
||||||
DATE_CLASS = '[object Date]';
|
|
||||||
|
|
||||||
function Type(o) {
|
|
||||||
switch (o) {
|
|
||||||
case null:
|
|
||||||
return NULL_TYPE;
|
|
||||||
case (void 0):
|
|
||||||
return UNDEFINED_TYPE;
|
|
||||||
}
|
|
||||||
var type = typeof o;
|
|
||||||
switch (type) {
|
|
||||||
case 'boolean':
|
|
||||||
return BOOLEAN_TYPE;
|
|
||||||
case 'number':
|
|
||||||
return NUMBER_TYPE;
|
|
||||||
case 'string':
|
|
||||||
return STRING_TYPE;
|
|
||||||
}
|
|
||||||
return OBJECT_TYPE;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extend(destination, source) {
|
function extend(destination, source) {
|
||||||
for (var property in source)
|
for (var property in source)
|
||||||
@@ -183,95 +151,7 @@ var Namespace = (function () {
|
|||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
function inspect(object) {
|
|
||||||
try {
|
|
||||||
if (isUndefined(object)) return 'undefined';
|
|
||||||
if (object === null) return 'null';
|
|
||||||
return object.inspect ? object.inspect() : String(object);
|
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof RangeError) return '...';
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function toJSON(value) {
|
|
||||||
return Str('', { '':value }, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
function Str(key, holder, stack) {
|
|
||||||
var value = holder[key],
|
|
||||||
type = typeof value;
|
|
||||||
|
|
||||||
if (Type(value) === OBJECT_TYPE && typeof value.toJSON === 'function') {
|
|
||||||
value = value.toJSON(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
var _class = _toString.call(value);
|
|
||||||
|
|
||||||
switch (_class) {
|
|
||||||
case NUMBER_CLASS:
|
|
||||||
case BOOLEAN_CLASS:
|
|
||||||
case STRING_CLASS:
|
|
||||||
value = value.valueOf();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (value) {
|
|
||||||
case null:
|
|
||||||
return 'null';
|
|
||||||
case true:
|
|
||||||
return 'true';
|
|
||||||
case false:
|
|
||||||
return 'false';
|
|
||||||
}
|
|
||||||
|
|
||||||
type = typeof value;
|
|
||||||
switch (type) {
|
|
||||||
case 'string':
|
|
||||||
return value.inspect(true);
|
|
||||||
case 'number':
|
|
||||||
return isFinite(value) ? String(value) : 'null';
|
|
||||||
case 'object':
|
|
||||||
|
|
||||||
for (var i = 0, length = stack.length; i < length; i++) {
|
|
||||||
if (stack[i] === value) {
|
|
||||||
throw new TypeError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stack.push(value);
|
|
||||||
|
|
||||||
var partial = [];
|
|
||||||
if (_class === ARRAY_CLASS) {
|
|
||||||
for (var i = 0, length = value.length; i < length; i++) {
|
|
||||||
var str = Str(i, value, stack);
|
|
||||||
partial.push(typeof str === 'undefined' ? 'null' : str);
|
|
||||||
}
|
|
||||||
partial = '[' + partial.join(',') + ']';
|
|
||||||
} else {
|
|
||||||
var keys = Object.keys(value);
|
|
||||||
for (var i = 0, length = keys.length; i < length; i++) {
|
|
||||||
var key = keys[i], str = Str(key, value, stack);
|
|
||||||
if (typeof str !== "undefined") {
|
|
||||||
partial.push(key.inspect(true) + ':' + str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
partial = '{' + partial.join(',') + '}';
|
|
||||||
}
|
|
||||||
stack.pop();
|
|
||||||
return partial;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringify(object) {
|
|
||||||
return JSON.stringify(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toQueryString(object) {
|
|
||||||
return $H(object).toQueryString();
|
|
||||||
}
|
|
||||||
|
|
||||||
function toHTML(object) {
|
|
||||||
return object && object.toHTML ? object.toHTML() : String.interpret(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
function keys(object) {
|
function keys(object) {
|
||||||
if (Type(object) !== OBJECT_TYPE) {
|
if (Type(object) !== OBJECT_TYPE) {
|
||||||
@@ -293,65 +173,10 @@ var Namespace = (function () {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
function clone(object) {
|
|
||||||
return extend({ }, object);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isElement(object) {
|
|
||||||
return !!(object && object.nodeType == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isArray(object) {
|
|
||||||
return _toString.call(object) === ARRAY_CLASS;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hasNativeIsArray = (typeof Array.isArray == 'function')
|
|
||||||
&& Array.isArray([]) && !Array.isArray({});
|
|
||||||
|
|
||||||
if (hasNativeIsArray) {
|
|
||||||
isArray = Array.isArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isHash(object) {
|
|
||||||
return object instanceof Hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFunction(object) {
|
|
||||||
return _toString.call(object) === FUNCTION_CLASS;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isString(object) {
|
|
||||||
return _toString.call(object) === STRING_CLASS;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isNumber(object) {
|
|
||||||
return _toString.call(object) === NUMBER_CLASS;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isDate(object) {
|
|
||||||
return _toString.call(object) === DATE_CLASS;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isUndefined(object) {
|
|
||||||
return typeof object === "undefined";
|
|
||||||
}
|
|
||||||
|
|
||||||
extend(Object, {
|
extend(Object, {
|
||||||
extend:extend,
|
extend:extend,
|
||||||
inspect:inspect,
|
|
||||||
toQueryString:toQueryString,
|
|
||||||
toHTML:toHTML,
|
|
||||||
keys:Object.keys || keys,
|
keys:Object.keys || keys,
|
||||||
values:values,
|
values:values,
|
||||||
clone:clone,
|
|
||||||
isElement:isElement,
|
|
||||||
isArray:isArray,
|
|
||||||
isHash:isHash,
|
|
||||||
isFunction:isFunction,
|
|
||||||
isString:isString,
|
|
||||||
isNumber:isNumber,
|
|
||||||
isDate:isDate,
|
|
||||||
isUndefined:isUndefined
|
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
System.out?.println(getStringLength("aaa"))
|
||||||
|
System.out?.println(getStringLength(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getStringLength(obj : Any) : Int? {
|
||||||
|
if (obj is String)
|
||||||
|
return obj.length // no cast to String is needed
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
cases("Hello")
|
||||||
|
cases(1)
|
||||||
|
cases(MyClass())
|
||||||
|
cases("hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cases(obj : Any) {
|
||||||
|
when(obj) {
|
||||||
|
1 => System.out?.println("One")
|
||||||
|
"Hello" => System.out?.println("Greeting")
|
||||||
|
!is String => System.out?.println("Not a string")
|
||||||
|
else => System.out?.println("Unknown")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyClass() {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user