Last active
May 7, 2020 17:35
-
-
Save mojo2012/fdd2ee532573bef6fd0cf2af47bc58fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition | |
# Structure | |
``` | |
import <FullyQualifiedTypeName>[.<StaticMethod>] [as <Alias>] | |
package <Name> { | |
[class, interface, enum, struct] | |
} | |
[private] enum <Name> { | |
<name>[()] | |
} | |
[private] struct <Name> { | |
val <name> as <Type> | |
} | |
[private] [final] class <ClassName> [extends <SuperClass>, ...] [implements <Interface>, ...] { | |
[private | protected] [static] [final] <Type>[?] <name> | |
[private | protected] [static] [synchronized] [async] <ReturnType>[?] <MethodName>([<Type>[?] <ParamName>, ...]) [throws Exception, ...] { | |
[var | val] <VariableName> [= <Assignment>] [as <Type>] | |
[final] <Type> <VariableName> [= <Assignment>] [as <Type>] | |
someMethod(); | |
} | |
} | |
[private] interface <InterfaceName> [extends <SuperInterface>, ...] { | |
[default] [async] <ReturnType>[?] <MethodName>([<Type>[?] <ParamName>, ...]) [throws Exception, ...] | |
} | |
``` | |
# Data types | |
## Primitive types | |
``` | |
byte | |
short | |
int | |
long | |
float | |
double | |
boolean | |
char | |
string // shorthand for char[] | |
``` | |
> These types cannot be null. | |
## Complex types | |
``` | |
Byte | |
Short | |
Integer | |
Long | |
Float | |
Double | |
Boolean | |
Character | |
String | |
``` | |
> These types are nullable using `<Type>?`. | |
# Program flow | |
``` | |
let name = "Peter" | |
name = "Markus" // fails, variable is defined as final | |
var message1 = 'Hello ${name}' // char array, | |
var message2 = "Hello ${name.upperCase()}" // String | |
message += "!!!!" | |
let idGetter = UserData::getId | |
``` | |
# Example | |
package Test { | |
class TypeName extends BaseClass implements IInterface { | |
let id as int // read only property | |
var name as String // read-write property | |
new(String name) { // constrcutor | |
this.id = 1 // field access through this. | |
this.name = name; | |
} | |
setName(String name) { | |
this.name = name | |
} | |
shout(String message) { | |
... | |
} | |
} | |
} | |
let finalVariable1 // not possible, as no value assigned | |
let finalVariable2 = 1 // type int inferred | |
var variable1 as String // no type can be inferred, therefore casting to type necessary | |
var name = "Peter" | |
name += "!!!!" | |
// supports named params | |
// casting through "as" keyword | |
let instance = TypeName.new(name = "Hallo ${Peter.upperCase()}") as BaseClass | |
// calls setter, instead of field access | |
instance.id = 2 // not possible, read only/no setter | |
instance.name = "Peeta" | |
// get unbound method handle | |
var shouldMethod = TypeName::shout | |
// invoke on instance | |
shoutMethod(instance, "Test Test Test") | |
// get bound method handle | |
shouldMethod = instance::shout | |
shouldMethod("Test 2 Test 2") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment