Hi I wanted to share the interesting usage of exte...
# getting-started
g
Hi I wanted to share the interesting usage of extension functions I found. Basically, when I use my domain value in certain contexts, I simply add extension functions to do something with it. Core functionality of the domain type stays in the type itself, but functionality for certain contexts is defined in those contexts. For example, I have a domain type "Apple with method grow()", but in the context of "SellAppleService" that has method "sell(apple)" i define additional methods for the "Apple" type that are important in the context of selling it as extension functions. So i can have "apple.package()", "apple.getFDACertificate()" and so forth. The resulting code of "SellAppleService#sell(apple)" is very clear:
Copy code
sell(Apple: apple) {
  apple.package()
  .doOtherStuff()
  .getFdaCertificate()
  .etc()
}
Curious what you think.
r
I think modifiers in Compose work like this as well.
g
@Robert Jaros what do you mean?
I am not familiar with
Compose
what is it?
r
And here is some docs about compose modifiers and context https://developer.android.com/develop/ui/compose/modifiers#scope-safety
It works like in your example - modifiers are built with a chain of extension functions and some functions are only available in some contexts.
❤️ 1
g
i checked it, briefly, but didn't understand it.
or do you mean how modifiers are built internally
m
My team follows this convention a lot, and I like it. I used to work adjacent to an iOS team that first used Objective-C then Swift. It was quite common in their code to see files named
Apple+SellAppleService.swift
... of course Swift's mechanism for "extending" differs, but the organization seems similar.