Why learning about Fluent Interfaces, I came across this post which states that using set
hints one is mutating the object whereas with
is returing a new object.
I have seen this pattern first hand while using PySpark (Python API for Apache Spark):
# Using "set" to modify exiting object
from pyspark.conf import SparkConf
from pyspark.context import SparkContext
conf = SparkConf()
conf.setMaster("local")
conf.setAppName("My app")
sc = SparkContext(conf=conf)
# Also possible using Fluent Interface
conf = (
SparkConf()
.setMaster("local")
.setAppName("My app")
)
sc = SparkContext(conf=conf)
# Using "with" to return new objects
from pyspark.sql import functions as F
dataframe = (
spark.createDataFrame([], StructType([]))
.withColumn("a", F.rand())
.withColumn("b", F.rand())
.withColumn("c", F.col("a") + F.col("b"))
.withColumnRenamed("c", "result")
)
My question is whether this set
vs with
has a particular name? Or is that just a framework/language-specific pattern?
From the example above it is visible it is a concept independent from Fluent Interfaces themselves (the set
approach can be used perfectly without any Fluent API), however it contributes to the readability.