function defaults must be constant
BlitzMax Forums/BlitzMax Programming/function defaults must be constant
| ||
is there any way to define an initialized type as a default parameter or constant value? for example:Framework BRL.Blitz Type t1 Field val1:Int Field val2:Int Field val3:Int Function create:t1() Local retval:t1=New t1 retval.val1=5 retval.val2=10 retval.val3=15 Return retval EndFunction EndType Type t2 Method test(v:t1=t1.create()) DebugLog(v.val1) DebugLog(v.val2) DebugLog(v.val3) EndMethod EndType i want to either be able to pass in an intialized t1 to test() or just let it use a set default. since t1.create() was a static type function i thought for sure this would work :( now i could handle this inside of test with: Method test(v:t1=Null) If Not v Then v=t1.create() DebugLog(v.val1) DebugLog(v.val2) DebugLog(v.val3) EndMethod simple enough, but when i extend the class the "If" statement must be included in each subtyped override of test() since calling Super.test(v) wont return the initialized variable if v started off as Null. not very OOP friendly :( anyone else have some thoughts around this or have accomplished something along these lines? i have also tried Type globals and Type constants but all give the same "must be constant" compile error. thx. |
| ||
You could do this: Framework BRL.Blitz Import BRL.StandardIO Type t1 Field val1:Int Field val2:Int Field val3:Int Function create:t1() Local retval:t1=New t1 retval.val1=5 retval.val2=10 retval.val3=15 Return retval EndFunction EndType Type t2 Method foo(v:t1=Null) If (v=Null) v=t1.Create() Endif bar(v) End Method Method bar(v:t1) DebugLog(v.val1) DebugLog(v.val2) DebugLog(v.val3) EndMethod EndType Type t2ext Extends t2 Method bar(v:t1) Print "Overridden method! - " + v.val1 + " - " + v.val2 + " - " + v.val3 End Method End Type Local meep:t2=New t2 Local baz:t2=New t2ext meep.foo() baz.foo() |
| ||
No there is no way to do that at the moment. The return problem can be solved at the moment ... you could return the created instance for usage afterwards. |
| ||
thx for the quick replies :) i went ahead with Roberts wrapper method approach using a _impl suffix on the method name to denote the actual implementation of the method. just need to make sure to override the _impl method instead of the wrapper in the cases where its used...Framework BRL.Blitz Type t1 Field val1:Int Field val2:Int Field val3:Int Function create:t1() Local retval:t1=New t1 retval.val1=5 retval.val2=10 retval.val3=15 Return retval EndFunction EndType Type t2 Method test(v:t1=Null) If Not v Then v=t1.create() test_impl(v) EndMethod Method test_impl(v:t1) DebugLog(v.val1) DebugLog(v.val2) DebugLog(v.val3) EndMethod EndType i suppose another nice feature of doing it this way is that if i needed to, i could actually change what the default type instance is by overriding the wrapper method also. |