About Monkey 2 › Forums › Monkey 2 Development › (vote) varargs / params feature
This topic contains 8 replies, has 3 voices, and was last updated by
nerobot 1 year, 6 months ago.
-
AuthorPosts
-
September 29, 2017 at 11:22 am #10884
Is it useful for you to have params feature in Monkey2?
Shortly: it’s an easy way to pass various number of parameters (with the same type).
Internally our params is an usaul array.
Monkey12345678910111213141516' example 1Function PrintSum( title:String,numbers:Int[] Params )Local sum:=0For Local n:=Eachin numberssum+=nNextPrint title+" : "+sumEndPrintSum( "total: ",3,7,0,12 )' example 2Function ArrayOf<T>:T[]( items:T[] Params )Return items ' maybe not so 'directly' wayEndLocal arr:=ArrayOf<String>( "one","two" )More info: https://msdn.microsoft.com/en-us/en-en/library/w5zay9db(v=vs.110).aspx
October 2, 2017 at 2:49 pm #10933From what I guess (based on similar threads) Mark wants to avoid syntactic sugar additions (unless they are mostly essential) in order to keep the parser thin and lightweight, perhaps I might be mistaken.
If the feature won’t make to the parser API hacks can be used for sure. This concept is based on factory patterns – to encapsulate the logic of creating stuff.
In the example below, the motivation is to pack variables into an array and return the array. The function overloading works like a dream here because it makes things very simple – a similar approach was used in the Tuples example I made earlier.
In this case if you dislike the API naming, perhaps other more meaningful names can be found, perhaps even extending the std Array class to form a smooth integration.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#Import "<std>"Using std..Class Params AbstractFunction Create<T>:T[](item1:T)Return New T[] (item1)EndFunction Create<T>:T[](item1:T, item2:T)Return New T[] (item1, item2)EndFunction Create<T>:T[](item1:T, item2:T, item3:T)Return New T[] (item1, item2, item3)EndFunction Create<T>:T[](item1:T, item2:T, item3:T, item4:T)Return New T[] (item1, item2, item3, item4)EndFunction Create<T>:T[](item1:T, item2:T, item3:T, item4:T, item5:T)Return New T[] (item1, item2, item3, item4, item5)EndEndFunction RunCommand(tokens:String[])Local command := ""For Local t := Eachin tokenscommand += t + " "NextPrint(command)EndFunction Main()' the default way' RunCommand(New String[] ("Start", "Level"))' RunCommand(New String[] ("Create", "Player", "RandomPosition"))' RunCommand(New String[] ("Create", "Enemy", "LotsOfThem", "100", "Robots"))' RunCommand(New String[] ("Timer", "Start"))' RunCommand(New String[] ("Begin", "Countdown", "GameLogic", "3"))' with paramsRunCommand(Params.Create("Start", "Level"))RunCommand(Params.Create("Create", "Player", "RandomPosition"))RunCommand(Params.Create("Create", "Enemy", "LotsOfThem", "100", "Robots"))RunCommand(Params.Create("Timer", "Start"))RunCommand(Params.Create("Begin", "Countdown", "GameLogic", "3"))EndOctober 2, 2017 at 3:11 pm #10935Idea is to hide Params.Create under the hood to simplify code.
From what I guess (based on similar threads) Mark wants to avoid syntactic sugar additions (unless they are mostly essential)
I created thit topic to ask if anybody like it and have usecases, looking at this Mark can decide to implement that or not.
October 3, 2017 at 7:48 pm #10957I would like it also as well.
October 5, 2017 at 1:29 am #10980Seems to me like a basic form of an initializer list as seen in C++.
Hmm, I feel like we should fuse this concept with the tuples so the type could vary from param to param.
Definitely useful in game programming for sure when initializing component based entities.October 5, 2017 at 2:58 am #10981Hmm, I feel like we should fuse this concept with the tuples so the type could vary from param to param.
Can you explain that concept?
For components you can declare function with base type Component and pass any derived types.
October 5, 2017 at 3:14 am #10982Well as it’s stated you can only pass a shared data type with “Params” but if you could pass a string and an int within the same line(like passing a tuple) you could state attributes for a character controller-like component that takes arguments like initializing the new RPG character or something similar.
Name:String
Description
Age:Int
JumpHeight:Double
AttackValue:DoubleYou could do:
Monkey123456789101112131415Class RPGCharacterController Extends ComponentField myParams:Params[]Method New<Params[]> (newParams:params[])Self.myParams = newParamsEnd MethodEnd ClassFunction Main()Local myCharacter:= New RPGCharacterController( ("name", 18, "description", 3.0, 5.0) )End MainOctober 5, 2017 at 3:27 am #10983You said about different things.
We discuss here special keyword Params (or Varargs), which is used as an addition to array type.
It has the only purpose – allow to pass varying params count, but all of them with the same type.
Would work with typles as well because they are an usual type (if will be added
):
Monkey12345Function ArrayOf<T>:T[]( items:T[] Params )Return itemsEnd' with tupleLocal arr:=ArrayOf<(Int,String)>( (1,"one"),(2,"two") )October 11, 2017 at 10:08 am #11066Usecase: c#-like string.Format() where {0},{1},… will be replaced with args[i] in order you pass params.
Monkey123456789101112' c#-like string.FormatMethod Format:String( str:String,args:String[] Params)For Local i:=0 Until args.Lengthstr=str.Replace( "{"+i+"}",args[i] )NextReturn strEnd' usageLocal template:="{0} is the best {1} for {3}!"Print Format( template,"Ted2Go","IDE","Monkey2" )Print Format( template,"Monkey2","tool","gemedev" ) -
AuthorPosts
You must be logged in to reply to this topic.