Skip to content

Fix record cast with named record type#7185

Open
bentsherman wants to merge 1 commit into
masterfrom
fix-cast-as-record-type
Open

Fix record cast with named record type#7185
bentsherman wants to merge 1 commit into
masterfrom
fix-cast-as-record-type

Conversation

@bentsherman
Copy link
Copy Markdown
Member

This PR fixes the use of casting records to a named record type, for example:

workflow {
    record(id: '1', fastq: file('1.fastq')) as Sample
}

record Sample {
    id: String
    fastq: Path
}

Casting a record to a named record type should be a no-op at runtime. It is only used by the type checker to validate that the record satisfies the requirements of the record type.

If the cast is is not removed at runtime, the record would be cast to an instance of Sample, since record types are compiled to actual classes at runtime with an implicit constructor. This would break the runtime's assumption that all records are RecordMaps.

Signed-off-by: Ben Sherman <bentshermann@gmail.com>
@bentsherman bentsherman requested a review from jorgee May 28, 2026 16:04
@bentsherman bentsherman requested a review from a team as a code owner May 28, 2026 16:04
@netlify
Copy link
Copy Markdown

netlify Bot commented May 28, 2026

Deploy Preview for nextflow-docs-staging canceled.

Name Link
🔨 Latest commit 3f001eb
🔍 Latest deploy log https://app.netlify.com/projects/nextflow-docs-staging/deploys/6a1867911d41470008983f7d

@jorgee
Copy link
Copy Markdown
Contributor

jorgee commented May 28, 2026

In my opinion, this behaviour is strange or at least incomplete; a user could do something like this:

record Sample{
	id:String
	num: Integer
}

workflow{
	def a = record(id: "hello", num: 1) as Sample
	if (a instanceof Sample){
		println("It is a sample")
	}else{
		println("not a sample")
	}
}

You create a record, cast it to Sample and then check the type with instanceof and see that it is not really a Sample. Not sure, if it is possible to replace the instanceof by a method that checks the defined fields instead of the class. At least we should add a note describing it in the docs. (I haven't found anything about it)

@bentsherman
Copy link
Copy Markdown
Member Author

Good catch. I actually meant to implement a compiler transform for that as well, so that sample instanceof Sample becomes something like _instanceof_record(sample, Sample) which performs a runtime check. I guess that fell through the cracks during development

I will try to implement that in this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants