Skip to content

Instantly share code, notes, and snippets.

@AlexCouch
Created June 29, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexCouch/c5f70a26c1da7e47486a5f70b45727ed to your computer and use it in GitHub Desktop.
Save AlexCouch/c5f70a26c1da7e47486a5f70b45727ed to your computer and use it in GitHub Desktop.
#memauto //Garbage collection turned on; no pointers/references allowed!!
//Compile-time constants
#define PHASE_ONE
#define PHASE_TWO
#define PHASE_THREE
//Injection site, indicated by a * at the end of the name of the function; only one!!!
fun test*(args: Array<String>){
println("Starting test...")
begin(PHASE_ONE)
begin(PHASE_TWO)
begin(PHASE_THREE)
}
//Passing some kind of #define constant; use this if it's a) constants only and b) types don't matter; better than using an enum
fun begin(def phase){
when(phase){
PHASE_ONE -> {
println("Constructing objects for phase one...")
catchif(constructFirstObjects(), it.size == 2) //More concise form of a try-catch
println("Finished!")
}
PHASE_TWO -> {
println("Constructing objects for phase two...")
catchif(constructSecondObjects(), it.size == 3)
println("Finished!")
}
PHASE_THREE -> {
println("constructing objects for phase three...")
//Local object that is constructed upon declaration; creates a local singleton; can be made into a variable
obj localObj{
val someInt = 12
var someString = "This is some string that I created!!"
init{
println(someInt)
println(someString)
}
}()
println("Finished!")
}
}
}
fun constructFirstObjects(): Array<Type>{
val obj1 = ObjectOne()
returnif({obj1, ObjectTwo}, (obj1.ok && ObjectTwo.ok)) //A conditioned return, same as returning an if in kotlin
}
fun constructSecondObjects(): Array<Type>{
val obj3 = ObjectThree()
val obj4 = ObjectFour()
val obj5 = ObjectFive(25, "Some Other String")
returnif({obj3, obj4, obj5}, obj3.ok && obj4.ok && obj5.ok && (obj4.someInt != obj5.someInt) && (obj4.someString != obj5.someString))
}
//A new type; basic
#type ObjectOne{
//Before construction; if you would like to ensure that certain things have taken place before init, do it now
preinit{
println("First object preparing...")
}
//Constructor body
init{
println("First object constructing!")
}
//Anything you would like to do after construction before moving on to the next line in the executing scope
postinit{
println("First object constructed!")
}
}
//A singleton object
#type [ObjectTwo]{
init{
println("This is a singleton object!")
}
}
//An open and overridable type
#type {ObjectThree}{
val someInt = 5
var someString = "Some String"
preinit{
val someFloat = float(someInt)
println(someFloat / 2)
println("Current 'someString' val: $someString")
}
init{
someString = "A different string"
}
postinit{
println(someString)
}
}
#type ObjectFour : ObjectThree{
//Open properties
val {someInt}: Int
var {someString}: String
preinit{
println(someInt)
println(someString)
}
init{
//Calling a function with no parameters
makeBuddy
}
//Private function
fun >makeCompanion<(){
this._buddy_() //Calling buddy init
}
//A buddy object (companion object in kotlin, inner static class in java) that is inherited by all subclasses of the current class
!buddy{
val someString = "This is a string in ObjectFour:_buddy_"
init{
println("Buddy object constructing...")
}
postinit{
println("Buddy object created!")
}
}
}
//A type that override parent properties as primary constructor parameters
#type ObjectFive(val ^someInt: Int, var ^someString: String) : ObjectFour{
preinit{
println(someInt)
println(someString)
println(this._buddy_.someString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment