is there anyway to declare a parameter as modifiab...
# announcements
s
is there anyway to declare a parameter as modifiable without needing to do
Copy code
fun a(b : Int) {
    var c = b
    c = 5
}
d
No, parameters are always
val
s
;-;
n
Can do shadowing, though not really recommended
Copy code
fun a(b: Int) {
    var b = b
    b = 5
}
d
In general if you want to have
var
semantics for a parameter that indicates a code-smell.
👍 6
s
so modifying a parameter passed value is undefined behaviour and it should not be done?
d
It's not undefined behavior. It is considered a code-smell so it is not allowed in Kotlin. What is your use case?
s
modifying a class
d
That is very vague and I can't really see the connection to modifying a parameter passed in
s
Copy code
process(a.path, a.extension, globalVariables.kppMacroList)
...
fun process(
    src: String,
    extension: String,
    macroTmp: ArrayList<Macro>
) {
    val destinationPreProcessed = File("$src${globalVariables.preprocessedExtension}.$extension")
    // macro needs to be a modifiable value thus cannot be declared as a paramater directly
    var macro = macroTmp
    var index = macro.size - 1
    if (macro[index].fileName != null) {
        index++
        println("reallocating to $index")
        macro = macro[0].realloc(macro, index + 1)
    }
    macro[index].fileName = src.substringAfterLast('/')
    println("registered macro definition for ${macro[index].fileName} at index $index")
    println("processing ${macro[index].fileName} -> ${destinationPreProcessed.name}")
    destinationPreProcessed.createNewFile()
    val lex = Lexer(fileToByteBuffer(File(src)), globalVariables.tokensNewLine)
    lex.lex()
    println("lex.currentLine is ${lex.currentLine}")
    while (lex.currentLine != null) {
        val out = parse(lex, macro)
        var input = lex.currentLine as String
        if (input[input.length - 1] == '\n') {
            input = input.dropLast(1)
        }
        println("\ninput = $input")
        println("output = $out\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        if (globalVariables.firstLine) {
            destinationPreProcessed.writeText(out + "\n")
            globalVariables.firstLine = false
        } else destinationPreProcessed.appendText(out + "\n")
        lex.lex()
    }
}
g
modifying a parameter passed value is undefined behaviour
It's so well defined, to the extent of being forbidden.
s
so what would be the correct way to accomplish this?
which realloc is just
Copy code
fun realloc(m: ArrayList<Macro>, NewSize: Int): ArrayList<Macro> {
        m.add(Macro())
        m[0].size = NewSize
        return m
    }
meaning to change it so it adds the correct number of new elements to the list, instead of only 1 even if more than 1 is requested, and delete if less than the current size is requested aswell
in
Copy code
macro = macro[0].realloc(macro, index + 1)
d
realloc
always returns the same object. There is no need to return it or to re-assign to
macro
.
s
have no idea if reassigning it is nessisary or not
ok
j
The intention behind the code is a little confusing - it looks like the first element in your list of macros has some special role (maintaining some kind of size for the whole list?) and the method to append a new macro to the list of macros is defined on the Macro object, and specifically called on the first macro in the list. But in any case, the reason you don't need to reassign is that:
Copy code
// here you pass in macro as the first argument, and you assign the result back to macro
macro = macro[0].realloc(macro, index + 1)
...
// here m is the original macro list
fun realloc(m: ArrayList<Macro>, NewSize: Int): ArrayList<Macro> {
        m.add(Macro())
        m[0].size = NewSize
// and it is returned here without having been modified -  it still refers to the original macros ArrayList
        return m
    }
// so it currently reassigns 'macro' to the value it already had
Hope that helps
s
ok