# defmethod
Add a method directly to a type without defining a protocol.
## Syntax
defmethod TypeName method-name { |params| body } defmethod TypeName method-name { |params| body } when guard
## Examples
deftype Point %{ x %{} y %{} } defmethod Point magnitude {|| [sqrt (($self~x * $self~x) + ($self~y * $self~y))] } defmethod Point scale-by {|factor^number| %Point{ x ($self~x * $factor) y ($self~y * $factor) } } set p %Point{ x 3 y 4 } echo [$p magnitude] # 5 echo [$p scale-by 2]~x # 6
# Guard-based dispatch defmethod Point classify {||} when ($self~x == 0 and $self~y == 0) { "origin" } defmethod Point classify {||} when ($self~x == 0) { "y-axis" } defmethod Point classify {||} { "general" }
## Notes
- `$self` refers to the instance
- Guards allow multiple methods with same name — first matching guard wins
- Use `||` for zero-param methods
- Called with `[$instance method-name args...]`
## See Also
`deftype`, `defprotocol`, `defimpl`