https://kotlinlang.org logo
Title
s

Smallville7123

04/08/2019, 11:19 AM
is there anyway to declare a parameter as modifiable without needing to do
fun a(b : Int) {
    var c = b
    c = 5
}
d

diesieben07

04/08/2019, 11:19 AM
No, parameters are always
val
s

Smallville7123

04/08/2019, 11:20 AM
;-;
n

neil.armstrong

04/08/2019, 11:20 AM
Can do shadowing, though not really recommended
fun a(b: Int) {
    var b = b
    b = 5
}
d

diesieben07

04/08/2019, 11:21 AM
In general if you want to have
var
semantics for a parameter that indicates a code-smell.
👍 6
s

Smallville7123

04/08/2019, 11:43 AM
so modifying a parameter passed value is undefined behaviour and it should not be done?
d

diesieben07

04/08/2019, 11:43 AM
It's not undefined behavior. It is considered a code-smell so it is not allowed in Kotlin. What is your use case?
s

Smallville7123

04/08/2019, 11:43 AM
modifying a class
d

diesieben07

04/08/2019, 11:44 AM
That is very vague and I can't really see the connection to modifying a parameter passed in
s

Smallville7123

04/08/2019, 11:46 AM
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

ghedeon

04/08/2019, 11:47 AM
modifying a parameter passed value is undefined behaviour
It's so well defined, to the extent of being forbidden.
s

Smallville7123

04/08/2019, 11:48 AM
so what would be the correct way to accomplish this?
which realloc is just
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
macro = macro[0].realloc(macro, index + 1)
d

diesieben07

04/08/2019, 11:54 AM
realloc
always returns the same object. There is no need to return it or to re-assign to
macro
.
s

Smallville7123

04/08/2019, 11:54 AM
have no idea if reassigning it is nessisary or not
ok
j

Jonathan Mew

04/08/2019, 3:07 PM
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:
// 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

Smallville7123

04/09/2019, 8:40 AM
ok