Pass Operators to Constructors

BlitzMax Forums/BlitzMax Programming/Pass Operators to Constructors

Leiden(Posted 2005) [#1]
Is there a way for a constructor to receive data. IE, when using something like:

Test:TMyType = New TMyType

Would there be a way to say pass an operator into the constructor so the Method New() would look something like:
Method New(X:Float, Y:float)
End Method


Is this possible?


Grey Alien(Posted 2005) [#2]
You can't do it with new but can make your own constructor called Create for example.


Leiden(Posted 2005) [#3]
Hey thanks for the reply. How would I do that? Do you mean just a Method called Create that I call once I construct my new type?


Azathoth(Posted 2005) [#4]
You'd use a function in the type as methods only work on the current instance.

Function Create:TMyType(X:Float, Y:float)
  Local mt:TMyType=New TMyType
<stuff>
  Return mt
End Function



Leiden(Posted 2005) [#5]
Ahh that works thanks, I tried with a Method but got unpredictable results. I've since read up more on Types and understand em a bit better.


taxlerendiosk(Posted 2005) [#6]
There's also this way
Type TMyType

  Method Create:TMyType(X:Float, Y:Float)
    <stuff>
    Return self
  End Method

End Type

mt:TMyType = New TMyType.Create()

...which I noticed was used in the new docs somewhere. Matter of taste, I suppose.


Leiden(Posted 2005) [#7]
Arg!? I am 99% sure I tried that and It didnt work. But it works, Thanks!


Grey Alien(Posted 2005) [#8]
"mt:TMyType = New TMyType.Create()" this method is messy as it combines new and .create. It would be better to just do "mt:TMyType = TMyType.Create()" and call a function not a method.

Hwoever, I sometimes have an Init method and call this once a type has been created (or later if I don't want to destroy and remake the type).


scooter43(Posted 2005) [#9]
Also you are allocating memory for 2 objects... yes you are throwing the first away, but there is no need to use a method.

I totally agree with Grey, have a function to create and an init method.

The only issue with calling the function Create() is that if you extend the type and plan on making another Create() function, then the child type must have the same arguments as the parent type.


Type A

  Function Create:A()
    Local _a:A = New A
   _a.Init()
    Return _a
  End Function

  Method Init()
   ' Do initialization here
  End Method
End Type

Type B Extends A
     Field s:String

    '  YOU CAN'T DO THIS... Argument types differ
     Function Create:B(_s:String)
       Local _b:B = New B
       _b.Init(_s)
       Return _b
     End Function
    
    '  YOU CAN'T DO THIS... Argument types differ
    Method Init(_s:String)
      s = _s
    End Method
End Type


Because BlitzMax does not support overloading, I suggest having a Create<Type Name> function and an Init<Type Name> method. Then you can pass what ever arguments you want.


Type A

  Function CreateA:A()
    Local _a:A = New A
   _a.Init()
    Return _a
  End Function

  Method InitA()
   ' Do initialization here
  End Method
End Type

Type B Extends A
     Field s:String

    '  YOU CAN DO THIS!!!
     Function CreateB:B(_s:String)
       Local _b:B = New B
       _b.InitB(_s)
       Return _b
     End Function
    
    '  YOU CAN DO THIS!!!
    Method InitB(_s:String)
      InitA()  ' Don't forget to initialize your parent
      s = _s
    End Method
End Type



Leiden(Posted 2005) [#10]
If only BlitzMax had templates, typecasting, and pointers. If it did It would have to be the best programming language ever.


taxlerendiosk(Posted 2005) [#11]
Also you are allocating memory for 2 objects... yes you are throwing the first away, but there is no need to use a method.

No you're not, you're creating one, then running a method on it that initialises some stuff in itself and returns itself.

I agree Init's a better name for the method than Create, but I just mentioned it because I saw it in the docs and thought it was interesting you could do that. I prefer a function too.


Perturbatio(Posted 2005) [#12]
If only BlitzMax had templates, typecasting, and pointers. If it did It would have to be the best programming language ever.


Maybe I don't understand what you mean exactly, but BMax has pointers and (at least to a certain extent) typecasting.


Jay Kyburz(Posted 2005) [#13]
I like the look of the method create. I didn't know you could do that. I've always disliked my create functions.


skidracer(Posted 2005) [#14]
To avoid operator overloading Objective C has a style where the Init name is used followed by a "With" and then a description of the parameters so in BlitzMax you would have something like:
Type TMyType
	Method InitWithString:TMyType(a$)
		..dostuff
		Return Self
	End Method

	Method InitWithCoords:TMyType(x,y)
		..dostuff
		Return Self
	End Method
End Type

a:TMyType=New TMyType.InitWithString("home")

b:TMyType=New TMyType.InitWithCoords(22,34)



Dreamora(Posted 2005) [#15]
Isn't that function overloading?
This has nothing to do with operators at all ... operator overloading would be

Type Vector
   field x:float, y:float

   method infix "+":Vector (other:Vector)
      return new Vector.create(self.x + other.x, self.y + other.y)
   end method

   method prefix "-":Vector ()
      return new Vector.create(self.x * -1, self.y * -1)
   end method
end Type

wouldn't it?

(used Eiffel coding style for operator overloading with infix and prefix to expose the following as unary or binary operator)